From 00100d9e40f241f8db45eebbb43bf8367251cc74 Mon Sep 17 00:00:00 2001 From: nvzard Date: Mon, 30 Oct 2017 00:14:57 +0530 Subject: [PATCH 1/2] Create USAGE.md --- USAGE.md | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 USAGE.md diff --git a/USAGE.md b/USAGE.md new file mode 100644 index 0000000..abf346a --- /dev/null +++ b/USAGE.md @@ -0,0 +1,109 @@ +# Usage + +Usage examples for SendGrid php-http-client + +## Initialization + +``` +// If running this outside of this context, use the following include and +// comment out the two includes below +// require __DIR__ . '/vendor/autoload.php'; +include(dirname(__DIR__) . '/lib/Client.php'); +// This gets the parent directory, for your current directory use getcwd() +$path_to_config = dirname(__DIR__); +$apiKey = getenv('SENDGRID_API_KEY'); +$headers = ['Authorization: Bearer ' . $apiKey]; +$client = new SendGrid\Client('https://api.sendgrid.com', $headers, '/v3'); +``` + +## Table of Contents + +- [GET](#get) +- [DELETE](#delete) +- [POST](#post) +- [PUT](#put) +- [PATCH](#patch) + + +## GET + +#### GET Collection + +``` +$query_params = ['limit' => 100, 'offset' => 0]; +$request_headers = ['X-Mock: 200']; +$response = $client->api_keys()->get(null, $query_params, $request_headers); +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(); +``` + + +## DELETE + +``` +$response = $client->api_keys()->_($api_key_id)->delete(); +echo $response->statusCode(); +echo $response->body(); +echo $response->headers(); +``` + + +## POST + +``` +$request_body = [ + '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; +``` + +## PUT + +``` +$request_body = [ + '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(); +``` + +## PATCH + +``` +$request_body = [ + 'name' => 'A New Hope' +]; +$response = $client->api_keys()->_($api_key_id)->patch($request_body); +echo $response->statusCode(); +echo $response->body(); +echo $response->headers(); +``` From 91f6858b2be970da3927d37ab87b28ebd82fe302 Mon Sep 17 00:00:00 2001 From: nvzard Date: Mon, 30 Oct 2017 00:35:50 +0530 Subject: [PATCH 2/2] Modify README.md to mention USAGE.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 86df68d..1bdbd3a 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ print $response->body(); # Usage -- [Example Code](https://github.com/sendgrid/php-http-client/tree/master/examples) +- [Usage Examples](USAGE.md) # Roadmap