Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions bin/get_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import os.path
import logging
import sys
import hashlib

# External modules
import requests
Expand All @@ -37,14 +38,21 @@
TIMED_ENTRIES = ['carbratio', 'sens', 'basal', 'target_low', 'target_high']


def request_headers(token):
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
if token:
headers['api-secret'] = hashlib.sha1(token.encode('utf-8')).hexdigest()
return headers

def get_profiles(nightscout, token):
"""
Get profiles available in nightscout
"""
r_url = nightscout + "/api/v1/profile.json"
if token is not None:
r_url = r_url + "?" + token
r = requests.get(r_url)
r = requests.get(r_url, headers=request_headers(token))
return r.json()


Expand All @@ -53,19 +61,15 @@ def get_current_profile(nightscout, token, profile_name):
Try to get the active profile
"""
r_url = nightscout + "/api/v1/profile.json"
if token is not None:
r_url = r_url + "?" + token
p_list = requests.get(r_url).json()
p_list = requests.get(r_url, headers=request_headers(token)).json()
logging.debug("profile list: %s", p_list)
default_profile = p_list[0]["defaultProfile"]
if profile_name is None:
p_url = (
nightscout +
"/api/v1/treatments.json?find[eventType][$eq]=Profile Switch&count=1"
)
if token is not None:
p_url = p_url + "?" + token
p_switch = requests.get(p_url).json()
p_switch = requests.get(p_url, headers=request_headers(token)).json()
logging.debug("p_switch: %s", p_switch)
if p_switch:
try:
Expand Down