Working with the YSE-PZ API

This is a quick overview of adding or getting data using the YSE-PZ django rest framework API.

Queries through a Web Browser

The above filters can be used to get information through the API. For example, say that you’d like to see the available information for SN 2018gv:

http://127.0.0.1:8000/api/transients/?name=2018gv

This works because the API has a name filter defined above. From there, you can alter the data for this transient through the POST web form on the page.

Say you’d like to find every transient with a right ascension between 0 and 1 degrees, and with a status of Following. The link would be:

http://127.0.0.1:8000/api/transients/?ra_gte=0&ra_lte=1&status_in=Following

Queries using the Python Requests Module

The python requests module is the easiest way to interact with the database through python.

A Simple GET Script

Let’s get the list of fields from the Young Supernova Experiment that are currently being observed (marked as “active”):

import requests
from requests.auth import HTTPBasicAuth
data = requests.get('http://127.0.0.1:8000/api/surveyfieldmsbs/?active=1',
                    auth=HTTPBasicAuth(login,password)).json()
field_list = [data['results'][i]['name'] for i in range(len(data['results']))]

A Simple POST Script

Let’s add a classical observing date:

import requests
from requests.auth import HTTPBasicAuth
import json

# first, we need to locate a classical resource
# let's just grab a recent Shane night
data = requests.get('http://127.0.0.1:8000/api/classicalresources/?telescope_name=Shane',
                    auth=HTTPBasicAuth(login,password)).json()
resource_url = data['results'][0]['url']

# now we can add a resource
# classicalnighttypes/3 in this case is a "full" night, you can grab this info with a similar GET as above
# and just choosing a random obs date
ObsDateInfo = {"resource": resource_url,
               "night_type": "http://127.0.0.1:8000/api/classicalnighttypes/3/",
               "obs_date": "2022-02-15T00:00:00"}
r = requests.post('http://127.0.0.1:8000/api/classicalobservingdates/',
                  json=ObsDateInfo,auth=HTTPBasicAuth(login,password))