|
| 1 | +# Python Client Boilerplate |
| 2 | + |
| 3 | +Description |
| 4 | + |
| 5 | +By default, all responses will parsed as JSON, and will default to text if not valid JSON. |
| 6 | + |
| 7 | +## System Requirements |
| 8 | +- [git](https://git-scm.com/) |
| 9 | +- [Python3](https://docs.python-guide.org/starting/installation/) |
| 10 | +- [pip](https://pip.pypa.io/en/stable/installing/) - most likely already installed as part of Python |
| 11 | + |
| 12 | +All of these must be available in your `PATH`. To verify things are set up properly, you can run this: |
| 13 | + |
| 14 | +```sh |
| 15 | +git --version |
| 16 | +python3 --version # it's possible your python3 binary is python |
| 17 | +pip3 --version # it's also possible your pip3 binary is pip |
| 18 | +``` |
| 19 | + |
| 20 | +If you have trouble with any of these, learn more about the PATH environment variables and how to fix it here for [windows](https://www.howtogeek.com/118594/how-to-edit-your-system-path-for-easy-command-line-access/) or [mac/linux](http://stackoverflow.com/a/24322978/971592). |
| 21 | + |
| 22 | +## Setup |
| 23 | + |
| 24 | +First, clone the repo: |
| 25 | + |
| 26 | +```sh |
| 27 | +git clone <this repo> |
| 28 | +cd <repo name> |
| 29 | +``` |
| 30 | + |
| 31 | +Python is a little divided on how to manage environments. If you know what you're doing, feel free to use whatever you prefer (`pipenv`, `venv`, global `pip`, etc.). |
| 32 | + |
| 33 | +Otherwise, run these commands: |
| 34 | + |
| 35 | +```sh |
| 36 | +virtualenv venv |
| 37 | +source venv/bin/activate #or venv/bin/activate.fish |
| 38 | +pip3 install -r requirements.txt |
| 39 | +``` |
| 40 | + |
| 41 | +## How to use it |
| 42 | + |
| 43 | +This is just a boilerplate client. At the very minimum, here are the steps required for creating a functional Python client: |
| 44 | +- In exampleClient.py, change the URL near the top to match the API you are calling |
| 45 | +- Set environment variables in whatever context will be running your code for authentication (Basic auth is most common). This could be VS Code, any other IDE, a terminal emulator, or on a hosting provider's site. |
| 46 | + |
| 47 | +Example shell: |
| 48 | +```sh |
| 49 | +export AUTH_TYPE='HTTPBASICAUTH' |
| 50 | +export CLIENT_USER= '[email protected]' |
| 51 | +export CLIENT_PASSWORD='4r3L1ygO0dp45$w0rd!12345' |
| 52 | +``` |
| 53 | + |
| 54 | +Example VSCode: |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +### Simple Test |
| 59 | + |
| 60 | +For a trivial test just to validate the client is working as expected, you can just add in a test case in the `if __name__ == "__main__":` section at the bottom. |
| 61 | + |
| 62 | +Any code included here will run ONLY if the `exampleClient.py` file is run directly. |
| 63 | + |
| 64 | +It can also be helpful to set the environment variable `DEBUG=1` to get debugging output from the HTTP requests. |
| 65 | + |
| 66 | +```py |
| 67 | +if __name == "__main__": |
| 68 | + client = Example() |
| 69 | + users = client.make_request('/api/v1/users') |
| 70 | + logger.info(users) |
| 71 | +``` |
| 72 | + |
| 73 | + |
| 74 | +### Using the client as a module |
| 75 | + |
| 76 | +Most of the time, you'll have a program that is using this client for just calling the API and you'll have other logic held somewhere else. |
| 77 | + |
| 78 | +Assuming this `exampleClient.py` file is in the same directory as your consuming code, you can use the following: |
| 79 | + |
| 80 | +```py |
| 81 | +import Example from exampleClient |
| 82 | + |
| 83 | +example_client = Example() # instantiates a client object - uses environment variables for auth if present |
| 84 | +resources = example.make_request('/api/v1/resources') # will send a GET request to the resources endpoint |
| 85 | +users = example.get_users() # |
| 86 | + |
| 87 | +john = example.get_user(id="12") # returns the user object for John |
| 88 | +``` |
| 89 | + |
| 90 | +## Roadmap |
| 91 | + |
| 92 | +Things I'd like to include: |
| 93 | +- Make Python2/3 compatible |
| 94 | +- Generate a client from the boilerplate given an OpenAPI url |
| 95 | +- HTTP_Method class in place of Strings |
| 96 | +- Handle responses other than JSON more elegantly |
| 97 | +- Handle rate limiting |
| 98 | +- Handle retries on `5XX` status codes |
| 99 | +- Implement a better non-environment-variable based configuration management system |
| 100 | +- Create a `setup.py` |
| 101 | + |
| 102 | +## Common Issues |
| 103 | + |
| 104 | + |
| 105 | +If you are not using a virtual environment of any sort, the default shebang command (`/usr/bin/env python`) might default to `python2`. |
| 106 | + |
| 107 | +You can change this to the absolute path of your `python` binary or update it to explicitly call out `/usr/bin/env python3`. |
0 commit comments