Skip to content

Commit 28ed882

Browse files
author
Matt Bernier
authored
Merge pull request #55 from misantron/code-climate-config
Code climate config
2 parents 73ec1d8 + bb44496 commit 28ed882

File tree

5 files changed

+122
-63
lines changed

5 files changed

+122
-63
lines changed

.codeclimate.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
engines:
2+
duplication:
3+
enabled: true
4+
config:
5+
languages:
6+
- "php"
7+
phpcodesniffer:
8+
enabled: true
9+
config:
10+
file_extensions: "php"
11+
ratings:
12+
paths:
13+
- "**.php"
14+
exclude_paths:
15+
- "examples/**/*"
16+
- "test/**/*"
17+
- "vendor/**/*"

lib/Client.php

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
/**
44
* HTTP Client library
55
*
6-
* PHP version 5.4
7-
*
86
* @author Matt Bernier <[email protected]>
97
* @author Elmer Thomas <[email protected]>
108
* @copyright 2016 SendGrid
@@ -16,8 +14,16 @@
1614
namespace SendGrid;
1715

1816
/**
19-
* Quickly and easily access any REST or REST-like API.
20-
*/
17+
* Quickly and easily access any REST or REST-like API.
18+
*
19+
* @method Response get($body = null, $query = null, $headers = null)
20+
* @method Response post($body = null, $query = null, $headers = null)
21+
* @method Response patch($body = null, $query = null, $headers = null)
22+
* @method Response put($body = null, $query = null, $headers = null)
23+
* @method Response delete($body = null, $query = null, $headers = null)
24+
*
25+
* @method Client version($value)
26+
*/
2127
class Client
2228
{
2329
/** @var string */
@@ -30,32 +36,33 @@ class Client
3036
protected $path;
3137
/** @var array */
3238
protected $curlOptions;
33-
/** @var array */
34-
private $methods;
3539
/** @var bool */
36-
private $retryOnLimit;
40+
protected $retryOnLimit;
41+
42+
/**
43+
* These are the supported HTTP verbs
44+
*
45+
* @var array
46+
*/
47+
private $methods = ['get', 'post', 'patch', 'put', 'delete'];
3748

3849
/**
3950
* Initialize the client
4051
*
41-
* @param string $host the base url (e.g. https://api.sendgrid.com)
42-
* @param array $headers global request headers
43-
* @param string $version api version (configurable)
44-
* @param array $path holds the segments of the url path
45-
* @param array $curlOptions extra options to set during curl initialization
46-
* @param bool $retryOnLimit set default retry on limit flag
52+
* @param string $host the base url (e.g. https://api.sendgrid.com)
53+
* @param array $headers global request headers
54+
* @param string $version api version (configurable)
55+
* @param array $path holds the segments of the url path
4756
*/
48-
public function __construct($host, $headers = null, $version = null, $path = null, $curlOptions = null, $retryOnLimit = false)
57+
public function __construct($host, $headers = [], $version = '/v3', $path = [])
4958
{
5059
$this->host = $host;
51-
$this->headers = $headers ?: [];
60+
$this->headers = $headers;
5261
$this->version = $version;
53-
$this->path = $path ?: [];
54-
$this->curlOptions = $curlOptions ?: [];
55-
// These are the supported HTTP verbs
56-
$this->methods = ['delete', 'get', 'patch', 'post', 'put'];
62+
$this->path = $path;
5763

58-
$this->retryOnLimit = $retryOnLimit;
64+
$this->curlOptions = [];
65+
$this->retryOnLimit = false;
5966
}
6067

6168
/**
@@ -91,28 +98,39 @@ public function getPath()
9198
}
9299

93100
/**
94-
* @return array
101+
* Set extra options to set during curl initialization
102+
*
103+
* @param array $options
104+
*
105+
* @return Client
95106
*/
96-
public function getCurlOptions()
107+
public function setCurlOptions(array $options)
97108
{
98-
return $this->curlOptions;
109+
$this->curlOptions = $options;
110+
111+
return $this;
99112
}
100113

101114
/**
102-
* Make a new Client object
103-
*
104-
* @param string $name name of the url segment
105-
*
106-
* @return Client object
107-
*/
108-
private function buildClient($name = null)
115+
* Set default retry on limit flag
116+
*
117+
* @param bool $retry
118+
*
119+
* @return Client
120+
*/
121+
public function setRetryOnLimit($retry)
109122
{
110-
if (isset($name)) {
111-
$this->path[] = $name;
112-
}
113-
$client = new Client($this->host, $this->headers, $this->version, $this->path, $this->curlOptions);
114-
$this->path = [];
115-
return $client;
123+
$this->retryOnLimit = $retry;
124+
125+
return $this;
126+
}
127+
128+
/**
129+
* @return array
130+
*/
131+
public function getCurlOptions()
132+
{
133+
return $this->curlOptions;
116134
}
117135

118136
/**
@@ -135,10 +153,10 @@ private function buildUrl($queryParams = null)
135153
* Make the API call and return the response. This is separated into
136154
* it's own function, so we can mock it easily for testing.
137155
*
138-
* @param string $method the HTTP verb
139-
* @param string $url the final url to call
140-
* @param array $body request body
141-
* @param array $headers any additional request headers
156+
* @param string $method the HTTP verb
157+
* @param string $url the final url to call
158+
* @param array $body request body
159+
* @param array $headers any additional request headers
142160
* @param bool $retryOnLimit should retry if rate limit is reach?
143161
*
144162
* @return Response object
@@ -147,13 +165,18 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
147165
{
148166
$curl = curl_init($url);
149167

150-
curl_setopt_array($curl, [
151-
CURLOPT_RETURNTRANSFER => true,
152-
CURLOPT_HEADER => 1,
153-
CURLOPT_CUSTOMREQUEST => strtoupper($method),
154-
CURLOPT_SSL_VERIFYPEER => false,
155-
CURLOPT_FAILONERROR => false,
156-
] + $this->curlOptions);
168+
$options = array_merge(
169+
[
170+
CURLOPT_RETURNTRANSFER => true,
171+
CURLOPT_HEADER => 1,
172+
CURLOPT_CUSTOMREQUEST => strtoupper($method),
173+
CURLOPT_SSL_VERIFYPEER => false,
174+
CURLOPT_FAILONERROR => false,
175+
],
176+
$this->curlOptions
177+
);
178+
179+
curl_setopt_array($curl, $options);
157180

158181
if (isset($headers)) {
159182
$this->headers = array_merge($this->headers, $headers);
@@ -179,7 +202,7 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
179202

180203
$response = new Response($statusCode, $responseBody, $responseHeaders);
181204

182-
if ($statusCode == 429 && $retryOnLimit) {
205+
if ($statusCode === 429 && $retryOnLimit) {
183206
$headers = $response->headers(true);
184207
$sleepDurations = $headers['X-Ratelimit-Reset'] - time();
185208
sleep($sleepDurations > 0 ? $sleepDurations : 0);
@@ -201,7 +224,15 @@ public function makeRequest($method, $url, $body = null, $headers = null, $retry
201224
*/
202225
public function _($name = null)
203226
{
204-
return $this->buildClient($name);
227+
if (isset($name)) {
228+
$this->path[] = $name;
229+
}
230+
$client = new static($this->host, $this->headers, $this->version, $this->path);
231+
$client->setCurlOptions($this->curlOptions);
232+
$client->setRetryOnLimit($this->retryOnLimit);
233+
$this->path = [];
234+
235+
return $client;
205236
}
206237

207238
/**

lib/Response.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
/**
44
* HTTP Client library
55
*
6-
* PHP version 5.4
7-
*
86
* @author Matt Bernier <[email protected]>
97
* @author Elmer Thomas <[email protected]>
108
* @copyright 2016 SendGrid
@@ -89,7 +87,7 @@ public function headers($assoc = false)
8987
private function prettifyHeaders($headers)
9088
{
9189
if (!is_array($headers)) {
92-
throw new \InvalidArgumentException('$headers should be array');
90+
throw new \InvalidArgumentException('Headers should be an array');
9391
}
9492

9593
return array_reduce(

test/unit/ClientTest.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ protected function setUp()
2020
'Content-Type: application/json',
2121
'Authorization: Bearer SG.XXXX'
2222
];
23-
$this->client = new MockClient($this->host, $this->headers, '/v3', null, null);
23+
$this->client = new MockClient($this->host, $this->headers);
2424
}
2525

2626
public function testConstructor()
@@ -30,12 +30,14 @@ public function testConstructor()
3030
$this->assertAttributeEquals('/v3', 'version', $this->client);
3131
$this->assertAttributeEquals([], 'path', $this->client);
3232
$this->assertAttributeEquals([], 'curlOptions', $this->client);
33-
$this->assertAttributeEquals(['delete', 'get', 'patch', 'post', 'put'], 'methods', $this->client);
33+
$this->assertAttributeEquals(false, 'retryOnLimit', $this->client);
34+
$this->assertAttributeEquals(['get', 'post', 'patch', 'put', 'delete'], 'methods', $this->client);
3435
}
3536

3637
public function test_()
3738
{
38-
$client = new MockClient($this->host, $this->headers, '/v3', null, ['foo' => 'bar']);
39+
$client = new MockClient($this->host, $this->headers, '/v3');
40+
$client->setCurlOptions(['foo' => 'bar']);
3941
$client = $client->_('test');
4042

4143
$this->assertAttributeEquals(['test'], 'path', $client);
@@ -79,34 +81,35 @@ public function testGetHeaders()
7981
$client = new Client('https://localhost:4010', ['Content-Type: application/json', 'Authorization: Bearer SG.XXXX']);
8082
$this->assertSame(['Content-Type: application/json', 'Authorization: Bearer SG.XXXX'], $client->getHeaders());
8183

82-
$client2 = new Client('https://localhost:4010', null);
84+
$client2 = new Client('https://localhost:4010');
8385
$this->assertSame([], $client2->getHeaders());
8486
}
8587

8688
public function testGetVersion()
8789
{
88-
$client = new Client('https://localhost:4010', null, '/v3');
90+
$client = new Client('https://localhost:4010', [], '/v3');
8991
$this->assertSame('/v3', $client->getVersion());
9092

91-
$client = new Client('https://localhost:4010', null, null);
92-
$this->assertSame(null, $client->getVersion());
93+
$client = new Client('https://localhost:4010');
94+
$this->assertSame('/v3', $client->getVersion());
9395
}
9496

9597
public function testGetPath()
9698
{
97-
$client = new Client('https://localhost:4010', null, null, ['/foo/bar']);
99+
$client = new Client('https://localhost:4010', [], null, ['/foo/bar']);
98100
$this->assertSame(['/foo/bar'], $client->getPath());
99101

100-
$client = new Client('https://localhost:4010', null, null, null);
102+
$client = new Client('https://localhost:4010');
101103
$this->assertSame([], $client->getPath());
102104
}
103105

104106
public function testGetCurlOptions()
105107
{
106-
$client = new Client('https://localhost:4010', null, null, null, [CURLOPT_PROXY => '127.0.0.1:8080']);
108+
$client = new Client('https://localhost:4010');
109+
$client->setCurlOptions([CURLOPT_PROXY => '127.0.0.1:8080']);
107110
$this->assertSame([CURLOPT_PROXY => '127.0.0.1:8080'], $client->getCurlOptions());
108111

109-
$client = new Client('https://localhost:4010', null, null, null, null);
112+
$client = new Client('https://localhost:4010');
110113
$this->assertSame([], $client->getCurlOptions());
111114
}
112115
}

test/unit/ResponseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,14 @@ public function testAssociativeHeaders()
4848

4949
$this->assertEquals(['Content-Type' => 'text/html', 'Status' => 'HTTP/1.1 200 OK'], $response->headers(true));
5050
}
51+
52+
/**
53+
* @expectedException \InvalidArgumentException
54+
* @expectedExceptionMessage Headers should be an array
55+
*/
56+
public function testHeadersWithInvalidValue()
57+
{
58+
$response = new Response(null, null, false);
59+
$response->headers(true);
60+
}
5161
}

0 commit comments

Comments
 (0)