Today's project will combine our work with the requests library and iterator usage with a new topic, databases. We will use requests to interface with Github's API to retrieve gists and store them in a database that we will then be able to perform searches on.
Your import_gists_to_database
function will take three parameters:
db
: The database object to connect tousername
: The GitHub user whose gists we are going to retrievecommit
(Optional, defaults toTrue
): IfTrue
, automatically commit changes to database
You are going to use the GitHub gists API to retrieve the gists of a given user, insert those gists into a database (schema may be found in the schema.sql
file), and if commit
is True, commit those changes to the database.
Your search_gists
method should take a db_connection
parameter (the database connection), as well as a variable number of keyword arguments to use in the search. This function should ultimately return an iterator that pumps out Gist objects with all the appropriate data filled in. These are the various keyword arguments your function should accept:
gist_id
public
created_on__gt
created_on__gte
created_on__lt
created_on__lte
updated_on__gt
updated_on__gte
updated_on__lt
updated_on__lte
comments
Datetime query parameters: The keyword arguments starting with created
and updated
will be operating such that they will be comparing against the created_at
and updated_at
fields using the corresponding comparison:gt
means greater than, gte
greater than or equal tho, lt
less than, lte
less than or equal to
When comparing dates in your sql statements use the format datetime(attribute) operator datetime(comparing_date)
Remember, the search should return an iterator that pumps out Gist
objects (Gist
definition is in models.py
)