Package: hubble-shuttle
Hubble Shuttle 🚀Note: This project is pre-1.0 release. We are still working on integrating it in production
systems here at HubbleHQ, and as a result, the API is not stable and might change before
1.0 release. This also means that not all HTTP features are currently supported (for example,
cookies), but we are actively adding support for these.Hubble Shuttle is an abstraction layer that makes it easy to write REST API clients.It enbraces convention over configuration, and abstracts a lot of common patterns to simplify
writing API clients.For example, writing a simple client for a sample user service is as simple as:classUserAPI(ShuttleAPI):api_endpoint="https://user-service.example.com/"query={"api_key":"my_api_key"}defget_user(self,user_id):# Returns a dict object `{ "id": 123, ... }`returnself.http_get(f"/users/{user_id}").dataShuttle will take care of building the full URL of the resource, inject API keys,
parse the response from the server based on content type, extracts the response based
on content encoding, and handles HTTP and networking errors.Installationpip install hubble_shuttleSupported Python versionsShuttle is build and tested with Python 3.8, and 3.9.Client-level configurationCreating an API client only requires you to extend theShuttleAPIclass, and provide some configuration
parameters:classMyClientAPI(ShuttleAPI):# The root endpoint for our API.api_endpoint="https://myservice/"# Headers to send with every request to the API, for example, authorization headers.headers={"Header-Name":"header value"}# Query parameters to send with every request to the API, for example, API keys.query={"query_param":"param value"}Making HTTP requestsTheShuttleAPIclass provides methods to allow you to make HTTP requests against you API, in
the form ofhttp_Xinstance methods, whereXis the HTTP verb you'd like to use.A simple GET request can be made like so:defget_user(self,user_id):response=self.http_get(f"/users/{id}")returnresponse.dataIf the HTTP server returns a successful (2xx) status code, thehttp_Xmethods will return a response object. If
the server returns a client error (4xx) or server error (5xx), thehttp_Xmethods will raise aHTTPError.When making an individual request, it is possible to override any client-level header or query parameter that
was set on the class. For example:classConflictingHeaderAPI(ShuttleAPI):api_endpoint="https://user-service.example.com/"headers={"Authorization":"Bearer default_api_key"}defget_user_profile(self,user_auth_token):# The request will contain the user_auth_token, overriding the default set at the client levelreturnself.http_get("/me",headers={"Authorization":f"Bearer{user_auth_token}"})Sending a request bodyWhen making a POST, PUT, or PATCH request, it is possible to embed a request body. The request
body will be automatically encoded based on request content type, if it is set. If the content
type isn't explicitely set,application/x-www-form-urlencodedis used as a default.defcreate_user(self):# Sends a POST request to `/users` with body `username=foo&email=foo@example.com`self.http_post("/users",data={"username":"foo","email":"foo@example.com"})You can also send the body in another format if you prefer:defcreate_user(self):# Sends a POST request to `/users` with body `{"username":"foo","email":"foo@example.com"}`self.http_post("/users",content_type="application/json",data={"username":"foo","email":"foo@example.com"})Shuttle supports the following content types for sending a request body:application/x-www-form-urlencoded(default)application/jsonSpecifying an unknown content type will result in aValueErrorbeing raised.If the API you are contacting always expects the request body to be another content type than the
defaultapplication/x-www-form-urlencoded, it is possible to change it for a whole client too:classUserAPI(ShuttleAPI):request_content_type="application/json"defcreate_user(self):# Sends a POST request to `/users` with body `{"username":"foo","email":"foo@example.com"}`self.http_post("/users",data={"username":"foo","email":"foo@example.com"})Response formatThe response object contains information returned by the HTTP backend. You can access:data: the body of the HTTP response, parsed based on its content type. Shuttle supports:text/plain:response.datawill be a Unicode string, containing the response body.application/json:response.datawill be a Python dict or array representing the JSON object.For any other content type, Shuttle will return a binary string containing the raw response body.status_code: the status code from the HTTP response.Error handlingFor any client error (4xx) or server error (5xx) status code, Shuttle will raise an error of typeHTTPErrorinstead
of returning a response object.Depending on the status code, Shuttle will return a subclass ofHTTPErrorto make it easier to respond to specific status
codes. For example, returningNonewhen the server returns a404 (Not found)error is straight-forward:defget_user(self,user_id):try:returnself.http_get(f"/users/{id}").dataexceptNotFoundError:returnNoneThe errors class tree is as follows:HTTPError(representing either 4xx or 5xx HTTP error)HTTPClientError(representing any type of 4xx error)BadRequestError(400 HTTP error)NotFoundError(404 HTTP error)HTTPServerError(representing any type of 5xx error)InternalServerError(500 HTTP error)For non-HTTP error (networking, DNS resolution errors, ...), Shuttle will return anAPIError.All errors are in thehubble_shuttle.exceptionsmodule.ContributingPull requests in Github are accepted and the best way to contribute to Shuttle.TestingWe require pull requests to have comprehensive tests coverage, and for all the tests to be passing.Tests are run against all the supported Python versions, using Docker. You can run the test suite using Docker:# Builds and sets up the Docker containers
make dev-build

# Runs the test suite inside Docker
make dev-testReleasingWhen Shuttle is ready for a new release, the steps to follow to publish the new version of the package are:Merge all pull requests that need to be part of the release, and switch to themainbranch. All releases should be made againstmain.Increase the version number in setup.py. We aim to use semantic versioning as closely as possible.Tag the commit you are packaging Shuttle from in Git, with the following format:release-VERSION.Push the tags to Github.Runmake dev-packageto create the distribution artifacts.Upload the distribution artifacts to pip.