open_fda_drug_label.api

Classes

Drug_Label_Client

OVERVIEW:

Module Contents

class open_fda_drug_label.api.Drug_Label_Client(api_key: str)[source]
OVERVIEW:

The Label Client is an API client class that allows an individual to engage directly with the openFDA Drug/Label API after loading in their API key. Users can use generic_search and search_request in tandem to expedite search-term specific API calls, or use manual_request for non-Drug/label search-specific queries of the broader openFDA API environment.

ATTRIBUTES

api_key (str): OpenFDA API key loaded in by user base_url (str): Base URL for OpenFDA endpoint (shortcut functions) session (requests.Session()): Universal HTTP sesh for API requests

USAGE EXAMPLE:
>>> # User function loads in API key as api_key
>>> client = Drug_Label_Client(api_key)
>>> advil_items = generic_search("brand_name", "Advil")
>>> advil_json = search_request(advil_items, limit=1)
>>> print(advil_json) # prints raw complete json for shortcut query
api_key
base_url = 'https://api.fda.gov/drug/label.json'
session
search_request(search_items, limit: int = 1)[source]
OVERVIEW:

Executes a search based on an inputted list of search_items which contains a list of searches built by generic_search. Returns a raw json with the number of searches equal to limit.

Parameters:
  • search_items (list) – list of fields generated by generic_search

  • limit (int) – defines the number of total drugs contained in json

RETURN VALUE:

returns response.json(), raw json format of openFDA HTTPS request

USAGE EXAMPLE:
>>> client = Drug_Label_Client(api_key)
>>> client.search_request([generic_search("brand_name","Advil")])
>>> # above gives json format of first item search for "Advil"
OVERVIEW:

Generates a generic_search string for input into the search_request function. Takes in a parameter (of any drug) and a particular name for the parameter to search for. For example, searching by parameter “brand_name” and name “Advil” yields all drugs with “Advil” in the “brand_name” field. Exact allows for exact string matching in the search field, and defaults to false.

Multiple generic_search functions can be combined using +AND+ and this is the reason for including all generic_search items in a list when inputting into search_request.

Parameters:
  • parameter (str) – field search function is being performed on

  • name (str) – string to match parameter search function to

  • exact (Bool) – designates whether exact match is required

RETURN VALUE:

search_item (str): string formatted for link insertion

USAGE EXAMPLE:
>>> client = Drug_Label_Client(api_key)
>>> client.generic_search("brand_name","Advil")
>>> # above returns list of drugs w/ "brand_name" containing "Advil"
manual_request(url)[source]
OVERVIEW:

Executes a manual data pull request using a user-provided url.

Parameters:

url (str) – string of user-inputted url.

RETURN VALUE:

response.json() => json response if valid url is enterred.

USAGE EXAMPLE:
>>> client = Drug_Label_Client(api_key)
>>> url = # [user inputted string]
>>> client.manual_request(url)