A simple, elegant collection of Python API wrappers


Keywords
sently, api, wrapper
License
MIT
Install
pip install py-wrapi==0.2.8

Documentation

Py-Wrapi: A simple, elegant collection of API wrappers

Py-Wrapi is a collection of Python API wrappers that I have written over time, with the help of the amazing tapioca library. The APIs available within this collection are those I've encountered in my work, and have helped me abstract a lot of the unnecessary details involved in dealing with the various different APIs.

Currently supported APIs:

Installation

Py-Wrapi is currently supported on Python 2.6, 2.7, 3+ and can be easily installed with the following command:

pip install py-wrapi

User Instructions

First, import the wrapper classes and initialize them.

from py_wrapi import Sently, GSheets
api = Sently(access_token='{any-valid-access-token}')
# and likewise
api = GSheets(client_id='{any-valid-client-id}', client_secret='{any-valid-client-secret}')

For authentication methods of other wrapper objects, view the respective documentation.

The general format to structuring your py-wrapi functions is as follows:

api.function(path_params).method(query_params / body)

Now, let's take a look at the available functions for our API wrappers. For this, we'll be using the info() method.

GSheets.info()
+-----------------+-----------------+---------+--------------------------------+
| function        | path_params     | methods | query_params / body            |
+-----------------+-----------------+---------+--------------------------------+
| get_cells       | spreadsheet_id, | GET     | No request parameters required |
|                 | range           |         |                                |
+-----------------+-----------------+---------+--------------------------------+
| update_cells    | spreadsheet_id, | PUT     | Request body to contain        |
|                 | range           |         | ValueRange object              |
+-----------------+-----------------+---------+--------------------------------+
| get_spreadsheet | spreadsheet_id  | GET     | No request parameters required |
+-----------------+-----------------+---------+--------------------------------+

Let's try getting the values of some Google Sheets cells. The function we're using is get_cells, and we will be required to pass in the spreadsheet_id and range parameters into the function call. The GET method will be used, with no further query parameters required. Do note that we're using A1 notation to indicate the range desired.

api.get_cells(spreadsheet_id = '{your-spreadsheet-id-here}', range = 'A1:B4').get(params = None)

If the request was successful, we should have obtained the values of cells from A1:B4. Now, suppose we then want to update the values of those cells. We will have to use the update_cells method to do so:

api.update_cells(spreadsheet_id = '{your-spreadsheet-id-here}', range = 'A1:B4').put(data = ValueRange)

As you can see, the PUT method requires an addition ValueRange object in the request body. The format of ValueRange, in this case, is defined by Google:

{
  "range": string,
  "majorDimension": enum(Dimension), # default: ROWS
  "values": [
    array
  ],
}

We will then want to check whether the request was successful or not. Let's save the API call in a variable first.

resp = api.update_cells(spreadsheet_id = '{your-spreadsheet-id-here}', range = 'A1:B4').put(ValueRange)

Then, we obtain the response body by entering the following:

_data = resp().data

from pprint import pprint # pretty printing for demo purposes
pprint(_data)

Documentation

For more details on the individual API wrappers, read their respective Py-Wrapi documentation: