Skip to main content

A generic and simple REST client for your python projects.

Project description

Generic Rest Client

A generic and simple REST client for your python projects.

How to install

pip install generic_rest_client

How to use

Create your custom class

First you will need to create your own class and extend GenericRestClient, for example, lets see our mocky client.

from generic_rest_client.client import GenericRestClient
...
class MockyRestClient(GenericRestClient):
...

Create custom methods

Every endpoint usually behaves differently, so we will create two functions that knows how to handle the requests.

Obtain all comments

This function knows how to obtain all comments, at least you will have to define:

  • Request type: we know it is a GET request.
  • Endpoint name: in this case '/comments'
  • Body params: in this case it is not required
  • Expected http code: We define HTTP 200 as an expected response.

Then we will have:

...
def get_comments(self):
    ...
    endpoint_url = '/comments'

    return self.get_request(
        urljoin(self.base_url, endpoint_url),
        None,
        [200, ]
    )
...

Create a new post

As we said, we create a function that knows what we will need to create a new post. To create a new post at least we will have to define:

  • Request type: we know it is a POST request.
  • Endpoint name: in this case '/posts'
  • Body params: We create a dictionary that contains all the required params.
  • Expected http code: We define HTTP 201 as an expected response.

Then we will have:

...
def new_post(self, params):
    ...
    endpoint_url = '/posts'

    body_params = dict(
        title=params['title'],
        body=params['body'],
    )

    return self.post_request(
        urljoin(self.base_url, endpoint_url),
        body_params,
        [201, ]
    )
...

Update a post

Just like creating a new post, we will create a new function and define:

  • Request type: we know it is a PUT request.
  • Endpoint name: in this case it will be '/posts/{post_id}'.
  • Body params: We create a dictionary that contains all the required params.
  • Expected http code: We define HTTP 200 as an expected response.
...
def update_post(self, post_id, params):
    ...
    endpoint_url = '/posts/{post_id}'.format(
        post_id=post_id,
    )

    body_params = dict(
        title=params['title'],
        body=params['body'],
    )

    return self.put_request(
        urljoin(self.base_url, endpoint_url),
        body_params,
        [200, ]
    )
...

See? it's really simple. Good luck with your projects! :)

If you want, you can check out our examples section.

Licence

MIT License

Copyright (c) 2018 Tpaga.co

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 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.

Our Company

Made with love at Tpaga, come work with us! jobs@tpaga.co

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

generic_rest_client-0.2.1.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

generic_rest_client-0.2.1-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file generic_rest_client-0.2.1.tar.gz.

File metadata

  • Download URL: generic_rest_client-0.2.1.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.7.2 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.5.3

File hashes

Hashes for generic_rest_client-0.2.1.tar.gz
Algorithm Hash digest
SHA256 cee978cef50143c370ccd256760e2196a66611c5d6d024443befc8834a090632
MD5 b88804431e0c00cdba4c97b7b33e157d
BLAKE2b-256 40da67a49ccd69686457166f9c940cf349e3304bd4fd1f0f760caaf1df0cf4c8

See more details on using hashes here.

File details

Details for the file generic_rest_client-0.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: generic_rest_client-0.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.19.1 setuptools/40.7.2 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.5.3

File hashes

Hashes for generic_rest_client-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 389f1955f53090a57e06250a5a76b8808f11a186a3558f8a21de88f55cd3b4a8
MD5 a4ba2817ac09a81ca5b2b35ca1d5e943
BLAKE2b-256 5cbc98aaa4ef2a8a52320df4f8b1a1833bb345e9a1f6d2d71a454a73b74967ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page