diff --git a/.gitignore b/.gitignore index c527c3a..bff2d16 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ vendor/ # Environment files .env/*.* +# Docker compose env file +.env # Local configs .php_cs .php_cs.cache diff --git a/README.md b/README.md index a3868b2..d1df90d 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ All updates to this library are documented in our [CHANGELOG](CHANGELOG.md). - [Installation](#installation) - [Quick Start](#quick-start) - [Usage](#usage) +- [Docker](#docker) - [How to Contribute](#contribute) - [Thanks](#thanks) - [About](#about) @@ -155,6 +156,44 @@ with message from CURL on why the request failed. Use the message as a hit to tr - [Usage Examples](USAGE.md) + +# Docker + +This repo comes with a `docker-compose.yml` file to get a development environment up and running quickly. + +## Prerequisites + +- Install [Docker](https://www.docker.com/) on your local machine. + +## Instructions + +Clone this repo to your local machine. + +``` +$ git clone https://github.com/sendgrid/php-http-client.git +``` + +Create a .env file at the root of the repo and add your API key. + +``` +$ cd php-http-client +$ echo "SENDGRID_API_KEY='YOUR_API_KEY'" >> .env +``` + +Create the containers. + +``` +$ docker-compose up -d +``` + +The `examples` directory is now available locally by visiting `localhost:8080`. Run the provided `example.php` file by clicking on the link in the browser or experiment with your own files by adding them to the `examples` folder. + +Destroy the containers. + +``` +$ docker-compose down +``` + ## Environment Variables You can do the following to create a .env file: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..d301541 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: '3' +services: + nginx: + image: nginx:latest + container_name: php-http-client-nginx + ports: + - "8080:80" + volumes: + - ./:/php-http-client + - ./docker/files/default.conf:/etc/nginx/conf.d/default.conf + links: + - php + php: + image: php:7-fpm + container_name: php-http-client-php-fpm + volumes: + - ./:/php-http-client + environment: + SENDGRID_API_KEY: ${SENDGRID_API_KEY} diff --git a/docker/files/default.conf b/docker/files/default.conf new file mode 100644 index 0000000..c2ea241 --- /dev/null +++ b/docker/files/default.conf @@ -0,0 +1,23 @@ +server { + listen 80; + server_name localhost; + + root /php-http-client/examples; + + access_log /var/log/nginx/access.log; + error_log /var/log/nginx/error.log; + + location / { + autoindex on; + } + + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass php:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } +} diff --git a/examples/example.php b/examples/example.php index 107d95a..849aeb7 100644 --- a/examples/example.php +++ b/examples/example.php @@ -15,47 +15,78 @@ $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(); +var_dump($response->statusCode()); +var_dump($response->body()); +// var_dump($response->headers()); +echo '

'; // 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); +var_dump($response->statusCode()); +var_dump($response->body()); +// var_dump($response->headers()); +echo '

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

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

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

'; // 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' + 'api_keys.create', + 'api_keys.delete', + 'api_keys.read', + 'api_keys.update', ] ]; $response = $client->api_keys()->_($apiKeyId)->put($requestBody); +var_dump($response->statusCode()); +var_dump($response->body()); +// var_dump($response->headers()); +echo '

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

'; diff --git a/lib/Client.php b/lib/Client.php index b15595c..6ab545d 100644 --- a/lib/Client.php +++ b/lib/Client.php @@ -370,7 +370,8 @@ private function buildUrl($queryParams = null) */ private function createCurlOptions($method, $body = null, $headers = null) { - $options = [ + $options = array_replace( + [ CURLOPT_RETURNTRANSFER => true, CURLOPT_HEADER => true, CURLOPT_CUSTOMREQUEST => strtoupper($method), @@ -378,18 +379,24 @@ private function createCurlOptions($method, $body = null, $headers = null) CURLOPT_FAILONERROR => false, ] + $this->curlOptions; + $requestHeaders = ['Content-Type: application/json']; + + // Merge client headers + if (isset($this->headers)) { + $requestHeaders = array_merge($requestHeaders, $this->headers); + } + + // Merge request headers if (isset($headers)) { - $headers = array_merge($this->headers, $headers); - } else { - $headers = $this->headers; + $requestHeaders = array_merge($requestHeaders, $headers); } if (isset($body)) { $encodedBody = json_encode($body); $options[CURLOPT_POSTFIELDS] = $encodedBody; - $headers = array_merge($headers, ['Content-Type: application/json']); } - $options[CURLOPT_HTTPHEADER] = $headers; + + $options[CURLOPT_HTTPHEADER] = $requestHeaders; if (class_exists('\\Composer\\CaBundle\\CaBundle') && method_exists('\\Composer\\CaBundle\\CaBundle', 'getSystemCaRootBundlePath')) { $caPathOrFile = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath();