uitdatabank.uitdatabank module¶
-
class
uitdatabank.uitdatabank.
UiTdatabank
(path_to_settings_file, test=False)¶ Bases:
object
find
Main find method that makes the actual api call. construct_parameters_for_api_call
Validates the query parameter fields against the API allowed fields, and constructs a query that can be send to the API construct_query
Validates a specific type of query against a list of supported fields construct_actor_query
Construct a query for actors construct_production_query
Construct a query for productions construct_event_query
Construct a query for events Very thin wrapper around UiTdatabank API v2, offering a simple way of authorizing and sending REST requests.
Settings for authentication are given in a configuration file, e.g.
[oauth] app_key = BAAC107B-632C-46C6-A254-13BC2CE19C6C app_secret = ec9a0e8c2cdc52886bc545e14f888612 user_token = user_secret = [uitdatabank] url = https://www.uitid.be/uitid/rest/searchv2/search
Parameters: - path_to_settings_file – a file in which the settings, such as oauth credentials and api url, are made explicit
- test – a boolean that can be set to True (default: False) so that only a limited amount of results is returned for test purposes
Returns: an UiTdatabank wrapper that can be used to query the database, whose results will be returned as an
SearchResults
objectYou would simply initialize the class as follows:
>>> udb = UiTdatabank("settings.cfg")
Then, you could search by either constructing the query yourself, e.g. by looking for a concert in the city of Gent:
>>> params = udb.construct_parameters_for_api_call({"q": "concert", "fq": "city:Gent"}) >>> searchresults = udb.find(params)
Or by relying on some of the helper functions, e.g. to look for an event that takes place in Brussels:
>>> params = udb.construct_event_query([("city", "Brussel")]) >>> searchresults = udb.find(params)
Because writing queries like that is tedious, shortcut requests will be collected in a child class
Shortcuts
, for recurring queries.-
construct_actor_query
(key_value_tuples_with_booleans)¶ Construct a query for actors
Parameters: key_value_tuples_with_booleans – a list of fields that is supported in the given type of query Returns: (a string that can be passed to “q” in the api call, a string that can be passed to “fq” in the call), this output fits find()
-
construct_event_query
(key_value_tuples_with_booleans)¶ Construct a query for events
Parameters: key_value_tuples_with_booleans – a list of fields that is supported in the given type of query Returns: (a string that can be passed to “q” in the api call, a string that can be passed to “fq” in the call), this output fits find()
Query for an event that takes place in BXL and that somewhere contains the word ‘jazz’:
>>> q, fq = udb.construct_event_query([("city", "Brussel"), "AND", "jazz"]) >>> params = udb.construct_parameters_for_api_call({"q": q, "fq": fq}) >>> searchresults = udb.find(params)
-
construct_parameters_for_api_call
(kwargs)¶ Validates the query parameter fields against the API allowed fields, and constructs a query that can be send to the API
Parameters: kwargs – a dictionary containing all the parameters for the query Returns: a dictionary of parameters that can be sent to the API by using the find()
method>>> udb.construct_parameters_for_api_call({"q": "city:Brussels", "fq":"type:event", "z": "wrong key"}) ValueError: Not a correct query parameter: z
Note
We are only supporting the following fields: ‘q’, ‘fq’, ‘rows’ and ‘past’. This means that the user can not use start, sort, group, pt, sfield, d, facetField, transform and datetype. Sorting, grouping and transform will be provided in
SearchResults
. datetype-like functionality will be provided inShortcuts
. Geographical search and faceted search are under consideration.
-
construct_production_query
(search_terms_and_booleans)¶ Construct a query for productions
Parameters: search_terms_and_booleans – the search operation in the form of a list of (field, value) tuples, booleans, or full text searches, cf construct_query()
Returns: (a string that can be passed to “q” in the api call, a string that can be passed to “fq” in the call), this output fits find()
-
static
construct_query
(supported_fields, kvs_with_bools)¶ Validates a specific type of query against a list of supported fields
Parameters: - supported_fields – a list of fields that is supported in the given type of query
- kvs_with_bools – a list of (key, value) tuples, potentially with booleans in between, that will be rewritten to a query
Returns: a string that can be passed to “q” in the api call
The kvs_with_bools parameter defines the query, the supported_fields parameter is a list of fields that can be passed to the API
(key, value) tuples define a search for a (field, search term)
>>> udb.construct_query(udb.event_query_fields, [("city", "Gent")]) "city:Gent"
You can put a boolean operator string in between (key, value) tuples
>>> udb.construct_query(udb.event_query_fields, [("city", "Gent"), "AND", ("organisor_label", "Vooruit")]) "city:Gent AND organisor_label:Vooruit"
>>> udb.construct_query(udb.event_query_fields, [("city", "Gent"), "AND", "concert"]) "city:Gent AND concert"
-
find
(parameters)¶ Main find method that makes the actual api call.
Parameters: parameters – the full query, containing the q, fq, etc. fields Returns: An uitdatabank searchresults object One of the examples in the API documentation is to look for items that contain the word “concert” and that take place in Gent:
>>> params = udb.construct_parameters_for_api_call({"q": "concert", "fq": "city:Gent"}) >>> searchresults = udb.find(params)