DS3002 Data Science Systems

Neal Magee, Ph.D.
Solution Architect, Research Computing
University of Virginia

Lab: Github API

Consuming APIs is an important part of retrieving specific, managed data sets. The ability to communicate with remote APIs using authentication and over a variety of methods and endpoints is a critical skill in the data science toolkit.

This lab extends on the hands-on portion of Lecture 04 by asking you to complete two additional tasks:

  1. With a Github Gist you have already created, delete the gist programmatically using Python3.
  2. Build upon the POST method code distributed with Lecture 04 to create a new Github repository in your account using Python3.

You may write and test your Python code in any editor you choose - Visual Studio, Atom, nano, Notepad, PyCharm, Eclipse, etc. There is no expectation that you MUST use any specific editor.

Setup

Delete a Gist

  1. Get the ID of an existing Github gist. This should look something like bb222a9c6366411000485682eacce254
  2. Refer to the documentation link above for Github gist API calls. Look specifically at documentation around the DELETE method.
  3. This documentation will show you the proper ENDPOINT, PARAMETER(S), and any additional required HEADERS. Documentation shows that a second header named 'Accept' with a value of 'application/vnd.github.v3+json' is required. This should be defined as simple key-value pairs, with a comma separation. So the full line defining "headers" for your script should look like this:
    headers = {'Accept': 'application/vnd.github.v3+json', 'Authorization': f'token {token}'}
    
  4. Remember to include an authentication header (using your token) given in the py-post.py example.
  5. Test and iterate on your code, creating and deleting gists easily.
  6. Submit a screenshot of your code to verify completion of this lab.
  7. BONUS: Create a "real" gist (programmatically) with meaningful content, such as a working version of the script you wrote above.

Scroll below to see a working answer.

Create a Repository

Repo API Documentation
  1. Refer to the documentation link above for github repo API calls. Look specifically at documentation around "Create a repository for the authenticated user".
  2. This documentation will show you the proper ENDPOINT, PARAMETER(S), DATA, and any additional required HEADERS. See the note above on additional headers.
  3. Remember to include an authentication header (using your token) given in the py-post.py example.
  4. Test and iterate on your code, creating repositories and manually deleting.
  5. Submit a screenshot of your code to verify completion of this lab.
  6. BONUS: Programmatically DELETE an existing repository using Python3.

Scroll below to see a working answer.

 

Answers

Delete a Gist

Create a Repo