-
Notifications
You must be signed in to change notification settings - Fork 226
/
Copy pathHttp.php
148 lines (135 loc) · 4.42 KB
/
Http.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
namespace Braintree;
use Braintree\HttpHelpers\Curl;
use Braintree\HttpHelpers\CurlRequest;
/**
* Braintree HTTP Client
* processes Http requests using curl
*/
class Http
{
protected $_config;
private $_useClientCredentials = false;
// phpcs:ignore PEAR.Commenting.FunctionComment.Missing
public function __construct($config)
{
$this->_config = $config;
}
/*
* DELETE request
*
* @param string $path URL path
* @param object $params optional any addition request parameters
*
* @return array|Exception
*/
public function delete($path, $params = null)
{
$response = $this->_doRequest('DELETE', $path, $this->_buildXml($params));
$responseCode = $response['status'];
if ($responseCode === 200 || $responseCode === 204) {
return true;
} elseif ($responseCode === 422) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($response['status']);
}
}
/*
* GET request
*
* @param string $path URL path
*
* @return array|Exception
*/
public function get($path)
{
$response = $this->_doRequest('GET', $path);
if ($response['status'] === 200) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($response['status']);
}
}
/*
* POST request
*
* @param string $path URL path
* @param object $params optional any addition request parameters
*
* @return array|Exception
*/
public function post($path, $params = null)
{
$response = $this->_doRequest('POST', $path, $this->_buildXml($params));
$responseCode = $response['status'];
if ($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($responseCode);
}
}
/*
* POST request for multi parts to be sent
*
* @param string $path URL path
* @param object $params additional request parameters
* @param object $file to be uploaded
*
* @return array|Exception
*/
public function postMultipart($path, $params, $file)
{
$headers = [
'User-Agent: Braintree PHP Library ' . Version::get(),
'X-ApiVersion: ' . Configuration::API_VERSION
];
$response = $this->_doRequest('POST', $path, $params, $file, $headers);
$responseCode = $response['status'];
if ($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($responseCode);
}
}
/*
* PUT request
*
* @param string $path URL path
* @param object $params optional any addition request parameters
*
* @return array|Exception
*/
public function put($path, $params = null)
{
$response = $this->_doRequest('PUT', $path, $this->_buildXml($params));
$responseCode = $response['status'];
if ($responseCode === 200 || $responseCode === 201 || $responseCode === 422 || $responseCode == 400) {
return Xml::buildArrayFromXml($response['body']);
} else {
Util::throwStatusCodeException($responseCode);
}
}
private function _buildXml($params)
{
return empty($params) ? null : Xml::buildXmlFromArray($params);
}
/*
* Sets internal variable to true
*/
public function useClientCredentials()
{
$this->_useClientCredentials = true;
}
private function _doRequest($httpVerb, $path, $requestBody = null, $file = null, $headers = null)
{
return $this->_doUrlRequest($httpVerb, $this->_config->baseUrl() . $path, $requestBody, $file, $headers);
}
// phpcs:ignore PEAR.Commenting.FunctionComment.Missing
public function _doUrlRequest($httpVerb, $url, $requestBody = null, $file = null, $customHeaders = null)
{
$curlRequest = new CurlRequest($url);
// phpcs:ignore Generic.Files.LineLength
return Curl::makeRequest($httpVerb, $url, $this->_config, $curlRequest, $requestBody, $file, $customHeaders, $this->_useClientCredentials);
}
}