Skip to content

README and usage example update #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,48 @@ Here is a quick example:
```php
// include __DIR__ . '/loader.php';
require 'vendor/autoload.php';
$global_headers = array('Authorization: Basic XXXXXXX');
$client = new SendGrid\Client('base_url', $global_headers);
$apiKey = YOUR_SENDGRID_API_KEY;
$authHeaders = [
'Authorization: Bearer ' . $apiKey
];
$client = new SendGrid\Client('https://api.sendgrid.com', $authHeaders);
$param = 'foo';
$response = $client->your()->api()->_($param)->call()->get();
print $response->statusCode();
print $response->headers();
print $response->body();

var_dump(
$response->statusCode(),
$response->headers(),
$response->body()
);
```

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

```php
// include __DIR__ . '/loader.php';
require 'vendor/autoload.php';
$global_headers = array('Authorization: Basic XXXXXXX');
$client = new SendGrid\Client('base_url', $global_headers);
$query_params = array('hello' => 0, 'world' => 1);
$request_headers = array('X-Test' => 'test');
$data = array('some' => 1, 'awesome' => 2, 'data' => 3);
$response = $client->your()->api()->_($param)->call()->post('data',
'query_params',
'request_headers');
print $response->statusCode();
print $response->headers();
print $response->body();
$apiKey = YOUR_SENDGRID_API_KEY;
$authHeaders = [
'Authorization: Bearer ' . $apiKey
];
$client = new SendGrid\Client('https://api.sendgrid.com', $authHeaders);
$queryParams = [
'hello' => 0, 'world' => 1
];
$requestHeaders = [
'X-Test' => 'test'
];
$data = [
'some' => 1, 'awesome' => 2, 'data' => 3
];
$param = 'bar';
$response = $client->your()->api()->_($param)->call()->post($data, $queryParams, $requestHeaders);

var_dump(
$response->statusCode(),
$response->headers(),
$response->body()
);
```

<a name="usage"></a>
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
"description": "HTTP REST client, simplified for PHP",
"type": "library",
"version": "3.8.0",
"require-dev": {
"phpunit/phpunit": "~4.4",
"squizlabs/php_codesniffer": "~2.0"
},
"homepage": "http://github.com/sendgrid/php-http-client",
"keywords": ["SendGrid", "HTTP", "REST", "API", "Fluent"],
"license": "MIT",
Expand All @@ -23,6 +19,10 @@
"require": {
"php": ">=5.6"
},
"require-dev": {
"phpunit/phpunit": "~4.4",
"squizlabs/php_codesniffer": "~2.0"
},
"autoload": {
"psr-4": {
"SendGrid\\": "lib/"
Expand Down
66 changes: 24 additions & 42 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,69 +10,51 @@
$headers = ['Authorization: Bearer ' . $apiKey];
$client = new SendGrid\Client('https://api.sendgrid.com', $headers, '/v3');

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

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

// POST
$request_body = [
// POST /v3/api_keys - create a new user API Key
$requestBody = [
'name' => 'My PHP API Key',
'scopes' => [
'mail.send',
'alerts.create',
'alerts.read'
]
];
$response = $client->api_keys()->post($request_body);
echo $response->statusCode();
echo $response->body();
echo $response->headers();
$response_body = json_decode($response->body());
$api_key_id = $response_body->api_key_id;
$response = $client->api_keys()->post($requestBody);
$responseBody = json_decode($response->body(), true);
$apiKeyId = $responseBody['api_key_id'];

// GET Single
$response = $client->version('/v3')->api_keys()->_($api_key_id)->get();
echo $response->statusCode();
echo $response->body();
echo $response->headers();
// GET /v3/api_keys/{api_key_id} - retrieve a single API Key
$response = $client->api_keys()->_($apiKeyId)->get();

// PATCH
$request_body = [
// PATCH /v3/api_keys/{api_key_id} - update the name of an existing API Key
$requestBody = [
'name' => 'A New Hope'
];
$response = $client->api_keys()->_($api_key_id)->patch($request_body);
echo $response->statusCode();
echo $response->body();
echo $response->headers();
$response = $client->api_keys()->_($apiKeyId)->patch($requestBody);

// PUT
$request_body = [
// PUT /v3/api_keys/{api_key_id} - update the name and scopes of a given API Key
$requestBody = [
'name' => 'A New Hope',
'scopes' => [
'user.profile.read',
'user.profile.update'
]
];
$response = $client->api_keys()->_($api_key_id)->put($request_body);
echo $response->statusCode();
echo $response->body();
echo $response->headers();
$response = $client->api_keys()->_($apiKeyId)->put($requestBody);

// DELETE
$response = $client->api_keys()->_($api_key_id)->delete();
echo $response->statusCode();
echo $response->body();
echo $response->headers();
// DELETE /v3/api_keys/{api_key_id} - revoke an existing API Key
$response = $client->api_keys()->_($apiKeyId)->delete();