Package: rest-tools
Python REST toolsA collection of tools to help interacting with JSON REST APIs.InstallFrom Pypipip install rest-toolsRationaleI was writing a lot of code to speak to different API, and while most of them used common interfaces (REST verbs, JSON data...)
almost every API had different ways to handle authentication and pagination. Since I ended up writing a "common ground" method
to access these APIs, I made it into a package.In this package you'll find a common function that just mounts various parts into the actual request (using, not surprisingly,requests), and some other basic api client for the services I came across.Common clientEvery function uses a common client with basic support for JSON body request/response and query parameters. Things get a bit more useful when you use a pre-baked client for a popular service.# commonfromrest_tools.commonimportcommon_clientresult=common_client(method="get",base_url="https://example.com",path="/api/v1/things",parameters={'page':1,'per_page':10})# wordpressfromrest_tools.wordpressimportget_wordpress_clientwp_client=get_wordpress_client(base_url="https://wp.example.com/wp-json",api_key="api_key",api_secret="api_secret")wp_client("get","/wp/v2/post",resource="posts")Once you set up the client with thebase_urland the auth parameters, you'll be able to query the resources in your Wordpress installation. Infact, if yougeta resource like your blog's posts and include the name of the resource (resource="posts"), the client will automatically issue a number of requests to fetch all the resources from the paginated endpoint.All of the clients share the same API; minor variations are detailed later on.get_<service>_client(base_url, **auth_details)base_url: if the service is in cloud (SaaS), this info is already configured.**auth_details: every client does this differently (see details)<service>_client(method, path="/", parameters=None, url=None, data=None, resource=None)method: one of the common http verbs: get, post, put, patch, delete...path: always starts with a/.parameters: this dictionary becomes?search=foo&exclude=bar.url: if specified, path and parameters will be ignored (this should be a full URL, eg.https://example.com/api/v1/things).data: the optional body content of a POST/PATCH/PUT request. It will be encoded as JSON according to the service.resource: this could be a bool or a string, depending on how the service responds. It is used to pull all the resources from a paginated endpoint.fromrest_tools.exampleimportget_example_clientexample_client=get_example_client(base_url="https://www.example.com/api/v1",api_key="apikey",api_secret="apisecret")foo_things=example_client(method="get",path="/things",parameters={'search':'foo','exclude':'bar'},url=None,data=None,resource="things")ClientsAll these clients are packaged and documented (in code).Canvasget_canvas_client(access_token, base_url)access_token: A personal access_token from your user profile page (see:https://canvas.instructure.com/doc/api/file.oauth.html#manual-token-generation)base_url: Your Canvas canonical URL (e.g.https://your-institution.instructure.com)ClickUpget_clickup_client(api_token, base_url)api_token: A personal token from theAppssection in your User Settings (see:https://jsapi.apiary.io/apis/clickup20/introduction/authentication/personal-token.html).base_url: the default should be changed only if asked from ClickUpDirectusget_directus_client(token, base_url)token: Astatic tokenfor the user (see:https://docs.directus.io/reference/authentication/#access-tokens)base_url: the complete base URL of your Directus instance (eg.:https://directus.example.com- no trailing slash)Eventbriteget_eventbrite_client(token, base_url)token: A token from your developer profile page (see:https://www.eventbrite.com/platform/api#/introduction/authentication).base_url: the default should be changed only if asked from EventbriteFusionauthget_fusionauth_client(api_key, base_url)api_key: the api key, see:https://fusionauth.io/docs/v1/tech/apis/authentication/#api-key-authenticationbase_url: the url of your Fusionauth instanceLivestormget_livestorm_client(apikey, base_url)apikey: A token from the Account Settings > Integrations page
(see:https://developers.livestorm.co/docs/authorization)base_url: the default should be changed only if asked from LivestormMailchimpget_mailchimp_client(access_token, base_url)access_token: A apikey from your developer profile page (see:https://mailchimp.com/developer/marketing/docs/fundamentals/#authenticate-with-an-api-key).base_url: the default should be changed only if asked from MailchimpPrestashopget_prestashop_client(access_key, base_url)Before being able to access the REST API, please follow theinstructionsto enable the Webservice.access_key: A access_key for the Webservice of your Prestashop instance (see:https://devdocs.prestashop.com/1.7/webservice/tutorials/creating-access/).base_url: the installation path of your Prestashop installation (eg.:https://store.example.com/api)Wordpressget_wordpress_client(api_key, api_secret, base_url)This client requires theJWT Auth plugintogenerate key pair.api_key: Key pair, keyapi_secret: Key pair, secret.base_url: The installation path of your WP installation; please include/wp-jsonat the end.Pipedriveget_pipedrive_client(api_token, domain)This client requires apersonal API tokenand thecompany domain.api_token: A personal API tokendomain: The company domain name assigned (es.:yourcompanywill becomehttps://yourcompany.pipedrive.com)