summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md7
-rw-r--r--CONTRIBUTING.md232
-rw-r--r--LICENSE.txt (renamed from LICENSE)1
-rw-r--r--php_http_client/client.php186
-rw-r--r--php_http_client/config.php0
5 files changed, 426 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
index 0000000..06d2ab8
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,7 @@
+# Change Log
+All notable changes to this project will be documented in this file.
+This project adheres to [Semantic Versioning](http://semver.org/).
+
+## [1.0.0] - 2016-02-29
+### Added
+- We are live!
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..4118196
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,232 @@
+Hello! Thank you for choosing to help contribute to the python-http-client. There are many ways you can contribute and help is always welcome.
+
+We use [Milestones](https://github.com/sendgrid/python-http-client/milestones) to help define current roadmaps, please feel free to grab an issue from the current milestone. Please indicate that you have begun work on it to avoid collisions. Once a PR is made, community review, comments, suggestions and additional PRs are welcomed and encouraged.
+
+* [Feature Request](#feature_request)
+* [Submit a Bug Report](#submit_a_bug_report)
+* [Improvements to the Codebase](#improvements_to_the_codebase)
+* [Understanding the Code Base](#understanding_the_codebase)
+* [Testing](#testing)
+* [Testing Multiple Versions of Python](#testing_multiple_versoins_of_python)
+* [Style Guidelines & Naming Conventions](#style_guidelines_and_naming_conventions)
+* [Creating a Pull Request](#creating_a_pull_request)
+
+There are a few ways to contribute, which we'll enumerate below:
+
+<a name="feature_request"></a>
+## Feature Request
+
+If you'd like to make a feature request, please read this section.
+
+The GitHub issue tracker is the preferred channel for library feature requests, but please respect the following restrictions:
+
+- Please **search for existing issues** in order to ensure we don't have duplicate bugs/feature requests.
+- Please be respectful and considerate of others when commenting on issues
+
+<a name="submit_a_bug_report"></a>
+## Submit a Bug Report
+
+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.
+
+Before you decide to create a new issue, please try the following:
+
+1. Check the Github issues tab if the identified issue has already been reported, if so, please add a +1 to the existing post.
+2. Update to the latest version of this code and check if issue has already been fixed
+3. Copy and fill in the Bug Report Template we have provided below
+
+### Please use our Bug Report Template
+
+In order to make the process easier, we've included a sample bug report template (borrowed from [Ghost](https://github.com/TryGhost/Ghost/)). The template uses [GitHub flavored markdown](https://help.github.com/articles/github-flavored-markdown/) for formatting.
+
+```
+Short and descriptive example bug report title
+
+#### Issue Summary
+
+A summary of the issue and the environment in which it occurs. If suitable, include the steps required to reproduce the bug. Please feel free to include screenshots, screencasts, code examples.
+
+
+#### Steps to Reproduce
+
+1. This is the first step
+2. This is the second step
+3. Further steps, etc.
+
+Any other information you want to share that is relevant to the issue being reported. Especially, why do you consider this to be a bug? What do you expect to happen instead?
+
+#### Technical details:
+
+* python-http-client Version: master (latest commit: 2cb34372ef0f31352f7c90015a45e1200cb849da)
+* Python Version: 2.7
+```
+
+<a name="improvements_to_the_codebase"></a>
+## Improvements to the Codebase
+
+We welcome direct contributions to the python-http-client code base. Thank you!
+
+### Development Environment ###
+
+#### Install and run locally ####
+
+##### Prerequisites #####
+
+* Python 2.6 through 3.5
+* There are no external dependencies
+
+##### Initial setup: #####
+
+```
+git clone https://github.com/sendgrid/python-http-client.git
+cd python-http-client
+cp .env_sample .env
+```
+
+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
+
+**/examples**
+
+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.
+
+This allows for the following mapping from a URL to a method chain:
+
+`/api_client/{api_key_id}/version` maps to `client.api_client._(api_key_id).version.<method>()` where <method> is a [HTTP verb](https://github.com/sendgrid/python-http-client/blob/master/client.py#L24).
+
+**congfig.py**
+
+Loads the environment variables.
+
+<a name="testing"></a>
+## Testing
+
+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.
+
+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.
+
+For Python 2.6.*:
+
+`unit2 discover -v`
+
+For Python 2.7.* and up:
+
+`python -m unittest discover -v`
+
+<a name="testing_multiple_versoins_of_python"></a>
+## Testing Multiple Versions of Python
+
+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 shell environment (.profile, .bashrc, etc) after installing tox, you only need to do this once.
+
+```
+pyenv install 2.6.9
+pyenv install 2.7.11
+pyenv install 3.2.6
+pyenv install 3.3.6
+pyenv install 3.4.3
+pyenv install 3.5.0
+python setup.py install
+pyenv local 3.5.0 3.4.3 3.3.6 3.2.6 2.7.8 2.6.9
+pyenv rehash
+````
+
+### Execute: ###
+
+```
+source venv/bin/activate
+tox
+```
+
+<a name="style_guidelines_and_naming_conventions"></a>
+## Style Guidelines & Naming Conventions
+
+Generally, we follow the style guidelines as suggested by the official language. However, we ask that you conform to the styles that already exist in the library. If you wish to deviate, please explain your reasoning.
+
+* [PEP8](https://www.python.org/dev/peps/pep-0008/)
+
+Please run your code through [pyflakes](https://pypi.python.org/pypi/pyflakes), [pylint](https://www.pylint.org/) and [pep8](https://pypi.python.org/pypi/pep8)
+
+### Directory Structure
+
+* `examples` for example calls
+* `tests`, for all tests
+
+## Creating a Pull Request<a name="creating_a_pull_request"></a>
+
+1. [Fork](https://help.github.com/fork-a-repo/) the project, clone your fork,
+ and configure the remotes:
+
+ ```bash
+ # Clone your fork of the repo into the current directory
+ git clone https://github.com/sendgrid/python-http-client
+ # Navigate to the newly cloned directory
+ cd python-http-client
+ # Assign the original repo to a remote called "upstream"
+ git remote add upstream https://github.com/sendgrid/python-http-client
+ ```
+
+2. If you cloned a while ago, get the latest changes from upstream:
+
+ ```bash
+ git checkout <dev-branch>
+ git pull upstream <dev-branch>
+ ```
+
+3. Create a new topic branch (off the main project development branch) to
+ contain your feature, change, or fix:
+
+ ```bash
+ git checkout -b <topic-branch-name>
+ ```
+
+4. Commit your changes in logical chunks. Please adhere to these [git commit
+ message guidelines](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)
+ or your code is unlikely be merged into the main project. Use Git's
+ [interactive rebase](https://help.github.com/articles/interactive-rebase)
+ feature to tidy up your commits before making them public.
+
+4a. Create tests.
+
+4b. Create or update the example code that demonstrates the functionality of this change to the code.
+
+5. Locally merge (or rebase) the upstream development branch into your topic branch:
+
+ ```bash
+ git pull [--rebase] upstream master
+ ```
+
+6. Push your topic branch up to your fork:
+
+ ```bash
+ git push origin <topic-branch-name>
+ ```
+
+7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
+ 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.
+
diff --git a/LICENSE b/LICENSE.txt
index a5e93d2..aaa5589 100644
--- a/LICENSE
+++ b/LICENSE.txt
@@ -19,3 +19,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+
diff --git a/php_http_client/client.php b/php_http_client/client.php
new file mode 100644
index 0000000..7ce72be
--- /dev/null
+++ b/php_http_client/client.php
@@ -0,0 +1,186 @@
+<?php
+class Client {
+
+ function __construct($host, $request_headers= [], $version=null){
+ /*
+ @param host: Base URL for the api. (e.g. https://api.sendgrid.com)
+ @type host: string
+
+ @param request_headers: A dictionary of the headers you want applied on all calls
+ @type request_headers: dictionary
+
+ @param version: The version number of the API.
+ @type integer:
+ */
+
+ $this->host = $host;
+ $this->request_headers = $request_headers;
+ $this->version = $version;
+
+ # _count and _url_path keep track of the dynamically built url
+ $this->_count = 0;
+ $this->_methods = ['delete', 'get', 'patch', 'post', 'put'];
+ $this->_url_path = {};
+ $this->_status_code = null;
+ $this->_response_body = null;
+ $this->_response_headers = null;
+ $this->_response = null;
+ }
+
+ /*
+ Resets the URL builder, so you can make a fresh new dynamic call.
+ */
+ private function _reset(self) {
+ $this->_count = 0
+ $this->_url_path = {}
+ $this->_response = None
+ }
+
+ /**
+ * Takes the method chained call and adds to the url path.
+ * @param name: The name of the method call
+ * @type name: string
+ */
+ private function _add_to_url_path($name) {
+ $this->_url_path[$this->_count] = $name;
+ $this->_count += 1;
+ }
+
+ /**
+ * Subclass this function for your own needs.
+ * Or just pass the version as part of the URL
+ * (e.g. client._('/v3'))
+ * @param url: URI portion of the full URL being requested
+ * @type url: string
+ * @return: string
+ */
+ private function _build_versioned_url($url) {
+ return sprintf("%s/v%d%s", $this->host, $this->_version, $url);
+ }
+
+ /**
+ * Build the final URL to be passed
+ * @param query_params: A dictionary of all the query parameters
+ * @type query_params: dictionary
+ * @return:
+ */
+ private function _build_url($query_params = null) {
+
+ $url = '';
+ $count = 0;
+
+ while ($count < count($this->_url_path)) {
+ $url += sprintf("/%s", $this->_url_path[count]);
+ count+=1;
+ }
+
+ if (isset($query_params)) {
+ $url_values = urlencode(asort(query_params));
+ $url = sprintf('%s?%s', $url, $url_values);
+ }
+
+ if ($this->_version) {
+ $url = $this->_build_versioned_url($url);
+ } else {
+ $url = $this->host + url;
+ }
+
+ return $url;
+ }
+
+ /**
+ * Build the API call's response
+ * :param response: The response object from the API call from urllib
+ * :type response: urllib.Request object
+ */
+ private function _set_response($response) {
+
+ // @todo fix this to handle the response from guzzle
+
+ $this->_status_code = "";
+ $this->_response_body = "";
+ $this->_response_headers = "";
+ }
+ /**
+ * Build the headers for the request
+ * @param request_headers: headers to set for the API call
+ * @type response: dict
+ * @return:
+ */
+ private function _set_headers($request_headers) {
+ $this->request_headers = array_merge($this->request_headers, $request_headers);
+ }
+
+ /**
+ * Make the API call and return the response. This is separated into it's own function, so we can mock it easily for testing.
+ * @param opener:
+ * @type opener:
+ * @param request: url payload to request
+ * @type request: urllib.Request object
+ * @return:
+ */
+ private function _make_request($opener, $request) {
+ //@todo make the call using guzzle, and fix the return value
+
+ return null;
+ }
+
+ /**
+ * Add variable values to the URL
+ * (e.g. /your/api/{variable_value}/call)
+ */
+ public function _($value){
+ $this->_add_to_url_path($value);
+ return $this;
+ }
+
+ /**
+ * Dynamically add method calls to the url, then call a method.
+ * (e.g. client.name.name.method())
+ */
+ public function __call($name, $args){
+
+ if (in_array($name, $this->_methods)) {
+ $this->_make_request();d
+ return $this;
+ }
+
+ $this->cache[$this->count] = $name;
+ $this->count = $this->count + 1;
+ return $this;
+ }
+
+ /**
+ * @return: integer, status code of API call
+ */
+ public function status_code() {
+ return $this->status_code;
+ }
+
+ /**
+ * @return: response from the API
+ */
+ public function response_body() {
+ return $this->response_body;
+ }
+
+ /**
+ * @return: dict of response headers
+ */
+ public function response_headers() {
+ return $this->response_headers;
+ }
+
+ /**
+ * Add the version to the path
+ */
+ public function version($version) {
+ $this->_version = $version;
+ }
+
+ private function _get_version() {
+ return $this->_version;
+ }
+
+}
+?>
diff --git a/php_http_client/config.php b/php_http_client/config.php
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/php_http_client/config.php