Skip to content

feat: Add docker compose file for a containerized development environment #80

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

Closed
wants to merge 10 commits into from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ vendor/
# Environment files
.env/*.*

# Docker compose env file
.env
# Local configs
.php_cs
.php_cs.cache
Expand Down
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

<a name="docker"></a>
# 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:
Expand Down
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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}
23 changes: 23 additions & 0 deletions docker/files/default.conf
Original file line number Diff line number Diff line change
@@ -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;
}
}
43 changes: 37 additions & 6 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<br><br>';

// 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 '<br><br>';

// 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 '<br><br>';

// 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 '<br><br>';

// 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 '<br><br>';

// 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 '<br><br>';

// 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 '<br><br>';
19 changes: 13 additions & 6 deletions lib/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,26 +370,33 @@ 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),
CURLOPT_SSL_VERIFYPEER => $this->verifySSLCerts,
CURLOPT_FAILONERROR => false,
] + $this->curlOptions;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing closing parenthesis.


$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();
Expand Down