Giter VIP home page Giter VIP logo

smart-rest-client's Introduction

Para visualizar o README em português.

Smart REST Client

PyPI latest PyPI Version PyPI License Docs Open Source? Yes!

Python wrapper to perform requests to Rest APIs using objects to request endpoints and their methods

Requirements

  • Python 3.x

How to install

You can get Smart Rest Client by using pip:

$ pip install smart-rest-client

If you want to install it from source, grab the git repository from GitHub and run setup.py:

$ git clone [email protected]:rhenter/smart-rest-client.git
$ cd smart-rest-client
$ python setup.py install

Settings

  • Create a clients.py file in the core folder of your project, if you haven't, created it within your project folder to be simple to be imported from anywhere in the project with the following content:
from smart_rest_client.client import api_client_factory
from smart_rest_client.settings import APIClientSettings

API_CLIENT_SETTINGS = {
    'API': [
      {
          'NAME': 'production',
          'BASE_URL': 'https://example.com',
          'ENDPOINTS': [
              '/v1/order/orders',
              '/v1/user/users',
              ...
          ],
          'AUTHENTICATION_ACCESS_TOKEN': 'TOKEN'
      },
      {
          'NAME': 'localhost',
          'BASE_URL': 'http://localhost:8001',
          'ENDPOINTS': [
              '/v1/order/orders',
              '/v1/user/users',
              ...
          ],
          'AUTHENTICATION_ACCESS_TOKEN': 'TOKEN'
      }
    ]
  }


api_settings = APIClientSettings(API_CLIENT_SETTINGS)
api_client = api_client_factory('production', api_settings)

Note

  • The name of this variable will be the name of the client that you can use throughout your project
  • It is recommended that the production use a set of configurations without configurations.py to change the simple way or the name of the API without the need to create several.
  • In our case, we have the option of "production" and "localhost", the factory generates the customer according to the name used and the parameters identified in it
  • Now we are going to list the data using the normal Django template system

Let's imagine which client is located in a folder called clients on project folder (folder containing the settings.py file)

Usage

For each endpoint the client Factory will create the follow structure:

Example to /user/users/

  • Create:
usage: api_client.user.users.create(data=data)
return: Response of POST of data (dict) to /user/users/
  • List:
usage: api_client.user.users.list()
return: Response of GET to /user/users/
  • Get/Retrieve/Detail:
usage: api_client.user.users.get(id=123)
return: Response of GET to /user/users/123/
  • Update:
usage: api_client.user.users.update(id=123, data=data, partial=False)
return: the response of UPDATE or PATCH of data (dict) to /user/users/123/
  • Delete:
usage: api_client.user.users.delete(id=123)
return: Response of GET to /user/users/123/

Example

  • Import the api_client from your client file
>> from client import api_client
>> result = api_client.user.users.list()
>>
>> # Use the result as object
>> print(result.as_obj())
UserUsers(
    previous=None,
    count=1,
    next=None,
    results=[
        NamelessModel(occupation=None, full_name='Admin System',
            image=None, cpf='', is_superuser=True, cellphone='', email='', sex=None, username='admin', birthdate='09/09/1999',
            logged_as='', id=1, is_temp=False, is_active=True)
    ]
)
>>
>> # Use the result as dict
>> print(result.as_dict())
{'count': 1,
 'next': None,
 'previous': None,
 'results': [{'id': 1,
   'username': 'admin',
   'full_name': 'Admin System',
   'sex': None,
   'birthdate': '09/09/1999',
   'cpf': '',
   'cellphone': '',
   'email': '',
   'image': None,
   'occupation': None,
   'logged_as': '',
   'is_superuser': True,
   'is_active': True,
   'is_temp': False}
  ]
 }

Documentation

Check out the latest smart-rest-client documentation at Github Pages

Contributing

Please send pull requests, very much appreciated.

  1. Fork the repository on GitHub.
  2. Make a branch off of master and commit your changes to it.
  3. Install requirements. pip install -r requirements-dev.txt
  4. Install pre-commit. pre-commit install
  5. Create a Pull Request with your contribution

smart-rest-client's People

Contributors

rhenter avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

luizfonseca06

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.