Skip to content

Commit 41901d3

Browse files
Merge pull request #53 from misantron/documentation-patch
README and usage example update
2 parents f683c13 + 403af0b commit 41901d3

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

README.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,30 +106,48 @@ Here is a quick example:
106106
```php
107107
// include __DIR__ . '/loader.php';
108108
require 'vendor/autoload.php';
109-
$global_headers = array('Authorization: Basic XXXXXXX');
110-
$client = new SendGrid\Client('base_url', $global_headers);
109+
$apiKey = YOUR_SENDGRID_API_KEY;
110+
$authHeaders = [
111+
'Authorization: Bearer ' . $apiKey
112+
];
113+
$client = new SendGrid\Client('https://api.sendgrid.com', $authHeaders);
114+
$param = 'foo';
111115
$response = $client->your()->api()->_($param)->call()->get();
112-
print $response->statusCode();
113-
print $response->headers();
114-
print $response->body();
116+
117+
var_dump(
118+
$response->statusCode(),
119+
$response->headers(),
120+
$response->body()
121+
);
115122
```
116123

117124
`POST /your/api/{param}/call` with headers, query parameters and a request body with versioning.
118125

119126
```php
120127
// include __DIR__ . '/loader.php';
121128
require 'vendor/autoload.php';
122-
$global_headers = array('Authorization: Basic XXXXXXX');
123-
$client = new SendGrid\Client('base_url', $global_headers);
124-
$query_params = array('hello' => 0, 'world' => 1);
125-
$request_headers = array('X-Test' => 'test');
126-
$data = array('some' => 1, 'awesome' => 2, 'data' => 3);
127-
$response = $client->your()->api()->_($param)->call()->post('data',
128-
'query_params',
129-
'request_headers');
130-
print $response->statusCode();
131-
print $response->headers();
132-
print $response->body();
129+
$apiKey = YOUR_SENDGRID_API_KEY;
130+
$authHeaders = [
131+
'Authorization: Bearer ' . $apiKey
132+
];
133+
$client = new SendGrid\Client('https://api.sendgrid.com', $authHeaders);
134+
$queryParams = [
135+
'hello' => 0, 'world' => 1
136+
];
137+
$requestHeaders = [
138+
'X-Test' => 'test'
139+
];
140+
$data = [
141+
'some' => 1, 'awesome' => 2, 'data' => 3
142+
];
143+
$param = 'bar';
144+
$response = $client->your()->api()->_($param)->call()->post($data, $queryParams, $requestHeaders);
145+
146+
var_dump(
147+
$response->statusCode(),
148+
$response->headers(),
149+
$response->body()
150+
);
133151
```
134152

135153
<a name="usage"></a>

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
"description": "HTTP REST client, simplified for PHP",
44
"type": "library",
55
"version": "3.8.0",
6-
"require-dev": {
7-
"phpunit/phpunit": "~4.4",
8-
"squizlabs/php_codesniffer": "~2.0"
9-
},
106
"homepage": "http://github.com/sendgrid/php-http-client",
117
"keywords": ["SendGrid", "HTTP", "REST", "API", "Fluent"],
128
"license": "MIT",
@@ -23,6 +19,10 @@
2319
"require": {
2420
"php": ">=5.6"
2521
},
22+
"require-dev": {
23+
"phpunit/phpunit": "~4.4",
24+
"squizlabs/php_codesniffer": "~2.0"
25+
},
2626
"autoload": {
2727
"psr-4": {
2828
"SendGrid\\": "lib/"

examples/example.php

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,51 @@
1010
$headers = ['Authorization: Bearer ' . $apiKey];
1111
$client = new SendGrid\Client('https://api.sendgrid.com', $headers, '/v3');
1212

13-
// GET Collection
14-
$query_params = ['limit' => 100, 'offset' => 0];
15-
$request_headers = ['X-Mock: 200'];
16-
$response = $client->api_keys()->get(null, $query_params, $request_headers);
13+
// GET /v3/api_keys - retrieve all API Keys that belong to the user
14+
$queryParams = ['limit' => 100, 'offset' => 0];
15+
$requestHeaders = ['X-Mock: 200'];
16+
$response = $client->api_keys()->get(null, $queryParams, $requestHeaders);
1717
echo $response->statusCode();
1818
echo $response->body();
1919
echo $response->headers();
2020

21-
// GET with auto retry on rate limit
22-
$query_params = ['limit' => 100, 'offset' => 0];
23-
$request_headers = ['X-Mock: 200'];
24-
$retryOnLimit = true;
25-
$response = $client->api_keys()->get(null, $query_params, $request_headers, $retryOnLimit);
26-
echo $response->statusCode();
27-
echo $response->body();
28-
echo $response->headers();
21+
// GET /v3/api_keys - retrieve all API Keys that belong to the user
22+
$queryParams = ['limit' => 100, 'offset' => 0];
23+
$requestHeaders = ['X-Mock: 200'];
24+
$retryOnLimit = true; // with auto retry on rate limit
25+
$response = $client->api_keys()->get(null, $queryParams, $requestHeaders, $retryOnLimit);
2926

30-
// POST
31-
$request_body = [
27+
// POST /v3/api_keys - create a new user API Key
28+
$requestBody = [
3229
'name' => 'My PHP API Key',
3330
'scopes' => [
3431
'mail.send',
3532
'alerts.create',
3633
'alerts.read'
3734
]
3835
];
39-
$response = $client->api_keys()->post($request_body);
40-
echo $response->statusCode();
41-
echo $response->body();
42-
echo $response->headers();
43-
$response_body = json_decode($response->body());
44-
$api_key_id = $response_body->api_key_id;
36+
$response = $client->api_keys()->post($requestBody);
37+
$responseBody = json_decode($response->body(), true);
38+
$apiKeyId = $responseBody['api_key_id'];
4539

46-
// GET Single
47-
$response = $client->version('/v3')->api_keys()->_($api_key_id)->get();
48-
echo $response->statusCode();
49-
echo $response->body();
50-
echo $response->headers();
40+
// GET /v3/api_keys/{api_key_id} - retrieve a single API Key
41+
$response = $client->api_keys()->_($apiKeyId)->get();
5142

52-
// PATCH
53-
$request_body = [
43+
// PATCH /v3/api_keys/{api_key_id} - update the name of an existing API Key
44+
$requestBody = [
5445
'name' => 'A New Hope'
5546
];
56-
$response = $client->api_keys()->_($api_key_id)->patch($request_body);
57-
echo $response->statusCode();
58-
echo $response->body();
59-
echo $response->headers();
47+
$response = $client->api_keys()->_($apiKeyId)->patch($requestBody);
6048

61-
// PUT
62-
$request_body = [
49+
// PUT /v3/api_keys/{api_key_id} - update the name and scopes of a given API Key
50+
$requestBody = [
6351
'name' => 'A New Hope',
6452
'scopes' => [
6553
'user.profile.read',
6654
'user.profile.update'
6755
]
6856
];
69-
$response = $client->api_keys()->_($api_key_id)->put($request_body);
70-
echo $response->statusCode();
71-
echo $response->body();
72-
echo $response->headers();
57+
$response = $client->api_keys()->_($apiKeyId)->put($requestBody);
7358

74-
// DELETE
75-
$response = $client->api_keys()->_($api_key_id)->delete();
76-
echo $response->statusCode();
77-
echo $response->body();
78-
echo $response->headers();
59+
// DELETE /v3/api_keys/{api_key_id} - revoke an existing API Key
60+
$response = $client->api_keys()->_($apiKeyId)->delete();

0 commit comments

Comments
 (0)