Server Client + API: Python Implementation for the Cron Job

Server + API Architecture Scenario

This document is part of the Server + API Architecture Scenario and it explains how to implement the server process in Python. Please refer to the Server + API Architecture Scenario document for information on the implemented solution.

Full source code for the Python implementation of the server process can be found in this GitHub repository.

Get an Access Token

In order to make the HTTP request to the Auth0 /oauth/token API endpoint we will use the libraries json, urllib and urllib2.

This is our sample implementation:

To test this modify your code to print the access_token variable and run the process using python

Invoke the API

The steps we follow in our implementation are:

  • Build a JSON object containing timesheet data and assign it to a timesheet variable.
  • Add the API URL and the timesheet variable contents to the request body using urllib2.Request.
  • Add the Authorization header to the request.
  • Set the Content-Type header to application/json.
  • Invoke the API using urllib2.urlopen and add some error handling. Retrieve the response using json.loads and print it in the console.

This is our sample implementation (some code is omitted for brevity):

To test this make sure your API is running and run the process using python

That's it! You are done!