Skip to content
Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ target/
.env
venv/
.python-version
cleanup.sh
cleanup.sh
.idea
19 changes: 9 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The GitHub issue tracker is the preferred channel for library feature requests,

Note: DO NOT include your credentials in ANY code examples, descriptions, or media you make public.

A software bug is a demonstrable issue in the code base. In order for us to diagnose the issue and respond as quickly as possible, please add as much detail as possible into your bug report.
A software bug is a demonstrable issue in the code base. In order for us to diagnose the issue and respond as quickly as possible, please add as much detail as possible into your bug report.

Before you decide to create a new issue, please try the following:

Expand Down Expand Up @@ -88,9 +88,7 @@ Update your settings in `.env`

##### Execute: #####

```
See the [examples folder](https://github.com/sendgrid/python-http-client/tree/master/examples) to get started quickly.
```

<a name="understanding_the_codebase"></a>
## Understanding the Code Base
Expand All @@ -101,7 +99,7 @@ Working examples that demonstrate usage.

**client.py**

An HTTP client with a fluent interface using method chaining and reflection. By returning self on [__getattr__](https://github.com/sendgrid/python-http-client/blob/master/client.py#L74) and [_()](https://github.com/sendgrid/python-http-client/blob/master/client.py#L70), we can dynamically build the URL using method chaining and [__getattr__](https://github.com/sendgrid/python-http-client/blob/master/client.py#L74) allows us to dynamically receive the method calls to achieve reflection.
An HTTP client with a fluent interface using method chaining and reflection. By returning self on [__getattr__](https://github.com/sendgrid/python-http-client/blob/master/client.py#L74) and [_()](https://github.com/sendgrid/python-http-client/blob/master/client.py#L70), we can dynamically build the URL using method chaining and [__getattr__](https://github.com/sendgrid/python-http-client/blob/master/client.py#L74) allows us to dynamically receive the method calls to achieve reflection.

This allows for the following mapping from a URL to a method chain:

Expand All @@ -114,9 +112,9 @@ Loads the environment variables.
<a name="testing"></a>
## Testing

All PRs require passing tests before the PR will be reviewed.
All PRs require passing tests before the PR will be reviewed.

All test files are in the `[tests](https://github.com/sendgrid/python-http-client/tree/master/tests)` directory.
All test files are in the `[tests](https://github.com/sendgrid/python-http-client/tree/master/tests)` directory.

For the purposes of contributing to this repo, please update the [`test_unit.py`](https://github.com/sendgrid/python-http-client/blob/master/tests/test_unit.py) file with unit tests as you modify the code.

Expand All @@ -131,18 +129,20 @@ For Python 2.7.* and up:
<a name="testing_multiple_versoins_of_python"></a>
## Testing Multiple Versions of Python

All PRs require passing tests before the PR will be reviewed.
All PRs require passing tests before the PR will be reviewed.

### Prequisites: ###

The above local "Initial setup" is complete

* [pyenv](https://github.com/yyuu/pyenv)
* [tox](https://pypi.python.org/pypi/tox)

### Initial setup: ###

Add eval "$(pyenv init -)" to your .profile after installing tox, you only need to do this once.
Add ```eval "$(pyenv init -)"``` to your shell environment (.profile, .bashrc, etc) after installing tox, you only need to do this once.

>>>>>>> Stashed changes
```
pyenv install 2.6.9
pyenv install 2.7.11
Expand Down Expand Up @@ -230,4 +230,3 @@ Please run your code through [pyflakes](https://pypi.python.org/pypi/pyflakes) a
with a clear title and description against the `master` branch. All tests must be passing before we will review the PR.

If you have any additional questions, please feel free to [email](mailto:dx@sendgrid.com) us or create an issue in this repo.

41 changes: 20 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ client = Client(host='base_url', request_headers=global_headers)
client.your.api._(param).call.get()
print response.status_code
print response.response_headers
print response.response_body
print response.response_body
```

`POST /your/api/{param}/call` with headers, query parameters and a request body with versioning.
Expand All @@ -30,12 +30,12 @@ response = client.your.api._(param).call.post(request_body=data,
request_headers=request_headers)
print response.status_code
print response.response_headers
print response.response_body
print response.response_body
```

# Installation

`pip install python_http_client`
`pip install python_http_client`

or

Expand All @@ -57,7 +57,7 @@ path_to_env = os.path.abspath(os.path.dirname(__file__))
python_http_client.Config(path_to_env)
host = os.environ.get('HOST')
api_key = os.environ.get('SENDGRID_API_KEY')
request_headers = {'Authorization': 'Bearer ' + api_key, 'Content-Type': 'application/json'}
request_headers = {'Authorization': 'Bearer {0}'.format(api_key), 'Content-Type': 'application/json'}
version = 3 # note that we could also do client.version(3) to set the version for each endpoint
client = python_http_client.Client(host=host,
request_headers=request_headers,
Expand All @@ -68,13 +68,14 @@ response = client.api_keys.get()

# POST
data = {
'name': 'My API Key',
'scopes': [
'mail.send',
'alerts.create',
'alerts.read'
]
}
"name": "My API Key",
"scopes": [
"mail.send",
"alerts.create",
"alerts.read"
]
}

response = client.api_keys.post(request_body=data)
json_response = json.loads(response.response_body)
api_key_id = json_response['api_key_id']
Expand All @@ -84,18 +85,18 @@ response = client.api_keys._(api_key_id).get()

# PATCH
data = {
'name': 'A New Hope'
}
"name": "A New Hope"
}
response = client.api_keys._(api_key_id).patch(request_body=data)

# PUT
data = {
'name': 'A New Hope',
'scopes': [
'user.profile.read',
'user.profile.update'
]
}
"name": "A New Hope",
"scopes": [
"user.profile.read",
"user.profile.update"
]
}
response = client.api_keys._(api_key_id).put(request_body=data)

# DELETE
Expand Down Expand Up @@ -130,5 +131,3 @@ We were inspired by the work done on [birdy](https://github.com/inueni/birdy) an
python-http-client is guided and supported by the SendGrid [Developer Experience Team](mailto:dx@sendgrid.com).

python-http-client is maintained and funded by SendGrid, Inc. The names and logos for python-http-client are trademarks of SendGrid, Inc.


3 changes: 2 additions & 1 deletion cleanup.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash

rm *.pyc
rm tests/*.pyc
rm python_http_client/*.pyc
rm -rf __pycache__/
rm -rf tests/__pycache__/
rm -rf python_http_client/__pycache__/
rm -rf *.egg-info
rm -rf *.egg-info
21 changes: 11 additions & 10 deletions examples/example.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
import os

if __name__ == '__main__' and __package__ is None:
from os import sys, path
sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
from python_http_client.client import Client
from python_http_client.config import Config


path = os.path.abspath(os.path.dirname(__file__)) + "/.."
Config(path)
local_path = '{0}/..'.format(os.path.abspath(os.path.dirname(__file__)))
Config(local_path)
api_key = os.environ.get('SENDGRID_API_KEY')
request_headers = {
'X-Mock': 200,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + api_key
}
'X-Mock': 200,
'Content-Type': 'application/json',
'Authorization': 'Bearer {0}'.format(api_key)
}
client = Client(host=os.environ.get('MOCK_HOST'),
request_headers=request_headers,
version=3)

request_headers = {'X-Mock': 200}
response = client.version(3).api_keys.get()
print(response.response_headers)
print(response.status_code)
print(response.response_body)

request_headers = {'X-Mock': 200}
request_headers = {
'X-Mock': 200
}
query_params = {'limit': 100}
response = client.api_keys.get(query_params=query_params,
request_headers=request_headers)
Expand Down Expand Up @@ -64,8 +66,7 @@

request_headers = {'X-Mock': 204}
api_key_id = "test_url_param"
response = client.api_keys._(api_key_id).delete(
request_headers=request_headers)
response = client.api_keys._(api_key_id).delete(request_headers=request_headers)
print("\nDELETE Mocked Example")
print(response.response_headers)
print(response.status_code)
Expand Down
72 changes: 36 additions & 36 deletions examples/live_sendgrid_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,70 +2,70 @@
import json
import python_http_client

path = os.path.abspath(os.path.dirname(__file__)) + "/.."
path = '{0}/..'.format(os.path.abspath(os.path.dirname(__file__)))
python_http_client.Config(path)
host = os.environ.get('HOST') # http://api.sendgrid.com
api_key = os.environ.get('SENDGRID_API_KEY')
request_headers = {
'Authorization': 'Bearer ' + api_key,
'Content-Type': 'application/json'
}
'Authorization': 'Bearer {0}'.format(api_key),
'Content-Type': 'application/json'
}
version = 3 # we could also use client.version(3)
client = python_http_client.Client(host=host,
request_headers=request_headers,
version=version)

# GET collection
response = client.api_keys.get()
print response.response_headers
print response.status_code
print response.response_body
print(response.response_headers)
print(response.status_code)
print(response.response_body)

# POST
data = {
"name": "My API Key",
"scopes": [
"mail.send",
"alerts.create",
"alerts.read"
]
}
"name": "My API Key",
"scopes": [
"mail.send",
"alerts.create",
"alerts.read"
]
}
response = client.api_keys.post(request_body=data)
print response.response_headers
print response.status_code
print response.response_body
print(response.response_headers)
print(response.status_code)
print(response.response_body)
json_response = json.loads(response.response_body)
api_key_id = json_response['api_key_id']

# GET single
response = client.api_keys._(api_key_id).get()
print response.response_headers
print response.status_code
print response.response_body
print(response.response_headers)
print(response.status_code)
print(response.response_body)

# PATCH
data = {
"name": "A New Hope"
}
"name": "A New Hope"
}
response = client.api_keys._(api_key_id).patch(request_body=data)
print response.response_headers
print response.status_code
print response.response_body
print(response.response_headers)
print(response.status_code)
print(response.response_body)

# PUT
data = {
"name": "A New Hope",
"scopes": [
"user.profile.read",
"user.profile.update"
]
}
"name": "A New Hope",
"scopes": [
"user.profile.read",
"user.profile.update"
]
}
response = client.api_keys._(api_key_id).put(request_body=data)
print response.response_headers
print response.status_code
print response.response_body
print(response.response_headers)
print(response.status_code)
print(response.response_body)

# DELETE
response = client.api_keys._(api_key_id).delete()
print response.response_headers
print response.status_code
print(response.response_headers)
print(response.status_code)
Loading