-
Notifications
You must be signed in to change notification settings - Fork 1
/
MakesRequests.php
163 lines (140 loc) · 3.4 KB
/
MakesRequests.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
namespace SmsGatewayBundle;
trait MakesRequests
{
/**
* Holds the arguments for the body.
*
* @var array
*/
protected $body = [];
/**
* Holds the username and password for requests.
*
* @var array
*/
protected $auth = [];
/**
* Has the call been built yet.
*
* @var bool
*/
protected $callBuilt = false;
/**
* Builds the API call address.
*
* @param $url
*/
protected function buildCall($url)
{
if (!$this->callBuilt) {
$this->apiBase .= $url;
$this->callBuilt = true;
}
}
/**
* Builds a URL.
*
* @param array $segments
*
* @return string
*/
protected function buildUrl(array $segments = [])
{
//Get the base URL and add a ?
$url = $this->apiBase.'?';
if (isset($this->apiEnding)) {
$segments = array_merge($segments, $this->apiEnding);
}
foreach ($segments as $key => $value) {
$url = $url."$key=$value&";
}
//Remove the final &
$url = substr($url, 0, -1);
return $url;
}
/**
* Builds the body part of the request and adds it to the body array.
*
* @param array|string $values
* @param null $key
*/
public function buildBody($values, $key = null)
{
if (is_array($values)) {
$this->body = array_merge($this->body, $values);
} else {
$this->body[$key] = $values;
}
}
/**
* Returns the body array.
*
* @return array
*/
protected function getBody()
{
return $this->body;
}
/**
* Sets the username for auth.
*
* @param string $username
*/
public function setUser($username)
{
$this->auth['username'] = $username;
}
/**
* Sets the password for auth.
*
* @param string $password
*/
public function setPassword($password)
{
$this->auth['password'] = $password;
}
/**
* Returns the auth array for requests. If not set, will return null.
*
* @return array|null
*/
protected function getAuth()
{
if (isset($this->auth['username']) && isset($this->auth['password'])) {
return [$this->auth['username'], $this->auth['password']];
}
return null;
}
/**
* Creates and sends a POST request to the requested URL.
*
* @return mixed
*/
protected function postRequest()
{
$response = $this->client->post($this->buildUrl(),
[
'auth' => $this->getAuth(),
'form_params' => $this->getBody(),
]);
if ($response->getStatusCode() != 201 && $response->getStatusCode() != 200) {
$this->throwNotSentException('Unable to request from API.');
}
return $response;
}
/**
* Creates and sends a GET request to the requested URL.
*
* @return mixed
*/
protected function getRequest()
{
$url = $this->buildUrl($this->getBody());
$response = $this->client->get($url, ['auth' => $this->getAuth()]);
if ($response->getStatusCode() != 201 && $response->getStatusCode() != 200) {
$this->throwNotSentException('Unable to request from API.');
}
return $response;
}
}