Code Review
  • 05 Apr 2024
  • 3 Minutes to read
  • Dark
    Light

Code Review

  • Dark
    Light

Article summary

Python Code Exercise

The following endpoint should be utilized to complete this exercise:

https://aiopsdemo.pythonanywhere.com

The objective of the exercise is to write a simple and concise python script or library that is able to read and write to this API. We have developed the following imaginary use-case for this API.

 The API interacts with data from a simple employee database. The employees are having a weight loss competition and would like to write a script, that when given a name in plain text, the script will get the employee's current weight from the database, subtract one pound from it, and write the latest weight along with a current timestamp. Because health data is a sensitive topic, this API is protected by a token based security method. You will be provided with a client ID and secret and must first request a JSON Web Token from the API, this will be encrypted using the CLIENT_ID (No need to decrypt the token, you will just attach it to your next request's headers). Using this token, you will then request the employee's info, and write the new weight back to the API. Here is a specific case we would like to see you complete:

  1. Use a Python virtual environment (https://docs.python.org/3/library/venv.html), the only two libraries required are:
    1. requests (https://pypi.org/project/requests/).
    2. datetime (https://docs.python.org/3/library/datetime.html).
  2. Try to adhere to PEP-8 formatting (https://peps.python.org/pep-0008/).
  3. Harold has lost a pound and would like to update his weight.
  4. Harold passes his name to the script.
  5. Request an API token using the Token Request below. Attach the token to the header of any of the following requests. Here are the client credentials:
    1. CLIENT_ID = 70d8b282-9f71-4c72-aa9e-4f271b77f3fa
    2. CLIENT_SECRET = fdc2b87c-128b-48c3-b114-5915cfc01775
  6. Fetch the employee list using Employee List below and get Harold's employee ID.
  7. Use Harold's ID to get his latest weight information using Employee Info below.
  8. Decrement his weight by one pound.
  9. Write the new weight and current timestamp to Harold's information using the Update Employee Weight request below.
  10. Ensure you receive a success message after updating the weight.

You can also be creative in your solution, if you would like to write a library or class to handle each function that would be good. You are not limited to the use case above, we just want to see how you interact with the data available.

Use the following endpoints documentation:


Take notice of the allowed methods for each endpoint, ensure you are using the correct method when making a request.

Token Request

Allowed method: [POST]

URL: https://aiopsdemo.pythonanywhere.com/token

Expected json contents:

Hint: If using python's requests library, make sure to use the json attribute e.g. response = requests.post("https://aiopsdemo.pythonanywhere.com/token", json=<YOUR_CONTENTS>)

{
"client_id": "70d8b282-9f71-4c72-aa9e-4f271b77f3fa",
"client_secret": "fdc2b87c-128b-48c3-b114-5915cfc01775",
}

Expected json response:

{
"token": "<ENCRYPTED_TOKEN>"
}

This will be your credentials to interact with the API, attach the token to the header of future requests to gain access. The token will expire after 60 seconds so please request a new one as needed.


Employee List

Allowed method: [GET]

URL: https://aiopsdemo.pythonanywhere.com/employees

Expected header contents:

Please attach this to the header of the request and insert the received token in place of <YOUR_TOKEN>. Hint: If using python's requests library, make sure to use the headers attribute e.g. requests.post(url, headers=<YOUR_HEADER>)

{
"Authorization": "Bearer <ENCRYPTED_TOKEN>",
}

Expected json response:

[
{"id":1, "name":Employee1},
{"id":2, "name":Employee2},
{"id":3, "name":Employee3},
...
]

Employee Info

Allowed method: [GET]

URL: https://aiopsdemo.pythonanywhere.com/employees/<int>

Where <int> is the ID of the employee

Expected header contents:

Please attach this to the header of the request and insert the received token in place of <YOUR_TOKEN>.

{
"Authorization": "Bearer <ENCRYPTED_TOKEN>",
}

Expected json response:

{
"id": 1,
"timestamp": <LAST_TIMESTAMP>,
"weight": <CURRENT_WEIGHT>
}

Update Employee Weight

Allowed method: [PUT]

URL: https://aiopsdemo.pythonanywhere.com/employees/<int>/update_weight

Where <int> is the ID of the employee

Expected header contents:

Please attach this to the header of the request and insert the received token in place of <YOUR_TOKEN>.

{
"Authorization": "Bearer <ENCRYPTED_TOKEN>",
}

Expected json request contents:

{
"value": <UPDATED_WEIGHT>,
"timestamp": <CURRENT_DATETIME>, // Please use "%m/%d/%y %H:%M:%S" to format the datetime string
}

Hint: If using python's requests library, make sure to use the json attribute e.g. requests.post(url, json=<YOUR_CONTENTS>)

Expected json response:

{
"success": <SUCCESS_MESSAGE>,
}

Extra Credit

Our current front end runs on Django. Try to create a simple Django app that the employees can use to modify their entries. Feel free to add more functionality than the initial problem calls for (employee list etc). 

Here are some helpful links: