Code Monkey home page Code Monkey logo

dialpad-python-sdk's Introduction

Python Dialpad API Client

A python wrapper around the Dialpad REST API

This document describes the installation, usage, and development practices of this python library. For information about the API itself, head on over to our API Documentation page!

Installation

Just use everyone's favourite python package installer: pip

pip install python-dialpad

Usage

The Short Version

TL;DR, this library provides a DialpadClient class, which can be instantiated with an API token and a dialpad URL.

Once a DialpadClient object has been constructed, it can be used to call our API endpoints:

from dialpad import DialpadClient

dp_client = DialpadClient(sandbox=True, token='API_TOKEN_HERE')

print(dp_client.user.get(user_id='1234567'))

Client Constructor Arguments

  • token (required) The API token that will be used to authenticate API requests.
  • sandbox (optional) If the sandbox argument is set to True, then API calls will be routed to https://sandbox.dialpad.com.
  • base_url (optional) Routes requests to a specific url.

API Resources

In general, each resource that we support in our public API will be exposed as properties of the client object. For example, the User resource can be accessed using the user property (as demonstrated above).

Each of these resource properties will expose related HTTP methods as methods of that resource property.

For example, GET /api/v2/users/{id} translates to dp_client.user.get('the_user_id').

API Responses

In cases where our API responds with a single JSON object, the client method will return a Python dict (as demonstrated above)

In cases where our API responds with a paginated list of many JSON objects, the client method will return an iterator which will lazily request the next page as the iterator is iterated upon.

from dialpad import DialpadClient

dp_client = DialpadClient(sandbox=True, token='API_TOKEN_HERE')

for user in dp_client.user.list():
  print(user)

Development

Testing

That's right, the testing section is first in line! Before you start diving in, let's just make sure your environment is set up properly, and that the tests are running buttery-smooth.

Assuming you've already cloned the repository, all you'll need to do is install tox, and run the command against the appropriate environment.

  • Install the tox package.

    $ pip install tox
  • Run the tests

    $ tox

    Optionaly, you can specify an environment to run the tests against. For eg:

    $ tox -e py3

That was easy :)

Neato!

Adding New Resources

Most of the changes to this library will probably just be adding support for additional resources and endpoints that we expose in the API, so let's start with how to add a new resource.

Each resource exposed by this library should have its own python file under the dialpad/resources directory, and should define a single class that inherits from DialpadResource.

The class itself should set the _resource_path class property to a list of strings such that '/api/v2/' + '/'.join(_resource_path) corresponds to the API path for that resource.

Once the _resource_path is defined, the resource class can define instance methods to expose functionality related to the resource that it represents, and can use the self.request helper method to make authenticated requests to API paths under the _resource_path. For example, if _resource_path is set to ['users'], then calling self.request(method='POST') would make a POST request to /api/v2/users. (A more precise description of the request method is given in the following section)

With that in mind, most methods that the developer chooses to add to a resource class will probably just be a very thin method that passes the appropriate arguments into self.request, and returns the result.

The request Helper Method

self.request is a helper method that handles the details of authentication, response parsing, and pagination, such that the caller only needs to specify the API path, HTTP method, and request data. The method arguments are as follows:

  • path (optional) Any additional path elements that should be added after the _resource_path
  • method (optional, default: 'GET') The HTTP method
  • data (optional) A python dict defining either the query params or the JSON payload, depending on which HTTP method is specified
  • headers (optional) Any additional headers that should be included in the request (the API key is automatically included)

If the request succeeds, then self.request will either return a python dict, or an iterator of python dicts, depending on whether the server responds with a pagenated response. Pagenated responses will be detected automatically, so the caller does not need to worry about it.

If the request fails, then a requests.HTTPError exception will be raised, and it'll be up to the consumer of this library to deal with it ๐Ÿ˜Ž

The resources/__init__.py File

When a new file is added to the resources directory, a new import statement should also be added to __init__.py to expose the newly defined resource class as a direct property of the resources module.

DialpadClient Resource Properties

In addition to adding the new class to the __init__.py file, the new resource class should also be added as a cached property of the DialpadClient class.

Recap

To add a new resource to this client library, simply:

  • Create a new file under the resources directory
  • Define a new subclass of DialpadResource within said file
  • Expose methods related to that resource as methods on your new class
  • Add a new import statement in resources/__init__.py
  • Add a new property to the DialpadClient class

dialpad-python-sdk's People

Contributors

dp-rufus avatar jakedialpad avatar tinvaan avatar yusufertekin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dialpad-python-sdk's Issues

corrupt cursor in contacts list

We were not able to use the python sdk to pull the full contact list. It would error in a bad iterator on the second page of results from the rest api.
We resolved the issue by adding a limit param to the list call and then running the request once and then removing the limit param and the full request succeeded.
It seem the cursor on your server was somehow corrupt and then when a new one was issued it resolved itself.
The error was persistent over multiple days until we added that limit param.

Installation fails on Windows

When installing v2.2.2, getting an UnicodeDecodeError exception. This is probably addressed in #22 which is waiting to be merged.

Collecting python-dialpad==2.2.0
  Downloading python-dialpad-2.2.0.tar.gz (24 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  ร— Getting requirements to build wheel did not run successfully.
  โ”‚ exit code: 1
  โ•ฐโ”€> [20 lines of output]
      Traceback (most recent call last):
        File "c:\root\.venv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in <module>
          main()
        File "c:\root\.venv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "c:\root\.venv\lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
        File "C:\Users\user\AppData\Local\Temp\pip-build-env-evf6l3se\overlay\Lib\site-packages\setuptools\build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
        File "C:\Users\user\AppData\Local\Temp\pip-build-env-evf6l3se\overlay\Lib\site-packages\setuptools\build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "C:\Users\user\AppData\Local\Temp\pip-build-env-evf6l3se\overlay\Lib\site-packages\setuptools\build_meta.py", line 480, in run_setup
          super(_BuildMetaLegacyBackend, self).run_setup(setup_script=setup_script)
        File "C:\Users\user\AppData\Local\Temp\pip-build-env-evf6l3se\overlay\Lib\site-packages\setuptools\build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 15, in <module>
        File "<string>", line 8, in readme
        File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\encodings\cp1255.py", line 23, in decode
          return codecs.charmap_decode(input,self.errors,decoding_table)[0]
      UnicodeDecodeError: 'charmap' codec can't decode byte 0x9f in position 5080: character maps to <undefined>
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

Best Practice for Transcript Retrieval

Hi!

Apologies in advance for creating this as a Git issue - I just figured this may be the easiest way to get some technical guidance from your team...

We're currently working on an implementation using the Dialpad API (and preferably through the Python SDK) where we need to consistently/constantly retrieve all of the transcripts of all conversations of the workforce at the company.

So far, when reading through API documentation, it appears that the best way to do this would be to:

  1. Submit a POST request to the /statistics endpoint for a given date range.
  2. Poll for completion of the asynchronous statistics job via a GETrequest to the /statistics endpoint.
  3. Upon completion of the request, retrieve all available call_id values.
  4. Submit a GET request to the transcript endpoint specifying the call_id in the URL.

Is this the best way of performing this process? The portion of this process that seems like it could be potentially brittle is the asynchronous + polling nature of the statistics requests.

We know that there is also a webhook/event driven mechanism, but are trying to avoid hosting additional infrastructure for the sake of identifying relevant call_ids.

TLDR: What is the best way for us to retrieve call_ids for a given date range WITHOUT hosting additional infrastructure?

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.