Skip to content

Commit

Permalink
Change default Surehub API endpoint and update request headers
Browse files Browse the repository at this point in the history
- Change auto-generated device id

Fix #25
  • Loading branch information
fabieu committed Dec 14, 2023
1 parent 654e139 commit 34978a7
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 44 deletions.
2 changes: 1 addition & 1 deletion sureflap_api/config.py
Expand Up @@ -2,7 +2,7 @@
from dynaconf import Dynaconf, Validator

# Default configuration variables
ENDPOINT = "https://app.api.surehub.io"
ENDPOINT = "https://app-api.production.surehub.io"
LOGLEVEL = "warning"
PORT = 3001

Expand Down
2 changes: 1 addition & 1 deletion sureflap_api/main.py
Expand Up @@ -13,7 +13,7 @@
from sureflap_api.config import settings
from sureflap_api import __version__

# FastAPI configration
# FastAPI configuration
app = FastAPI()


Expand Down
29 changes: 25 additions & 4 deletions sureflap_api/modules/auth.py
@@ -1,5 +1,5 @@
# Built-in modules
import random
from uuid import uuid1
import json

# PyPi modules
Expand All @@ -13,7 +13,28 @@
cache = TTLCache(maxsize=128, ttl=86400)


def getToken() -> str:
def default_headers() -> dict[str, str]:
return {
"Host": "app.api.surehub.io",
"Connection": "keep-alive",
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "en-US,en-GB;q=0.9",
"Content-Type": "application/json",
"Origin": "https://surepetcare.io",
"Referer": "https://surepetcare.io",
"X-Requested-With": "com.sureflap.surepetcare",
"X-Device-Id": str(uuid1()),
}


def auth_headers() -> dict[str, str]:
return default_headers() | {
"Authorization": f"Bearer {get_token()}",
}


def get_token() -> str:
if cache.get("token"):
return cache.get("token")
else:
Expand All @@ -22,10 +43,10 @@ def getToken() -> str:
payload = {
"email_address": settings.EMAIL,
"password": settings.PASSWORD,
"device_id": random.randrange(1000000000, 9999999999)
"device_id": str(uuid1()),
}

response = requests.post(uri, data=payload)
response = requests.post(uri, json=payload, headers=default_headers())

if response.ok:
data = json.loads(response.text)['data']
Expand Down
4 changes: 1 addition & 3 deletions sureflap_api/modules/dashboard.py
Expand Up @@ -13,9 +13,7 @@
def get_dashboard() -> dict:
uri = f"{settings.ENDPOINT}/api/me/start"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(uri, headers=headers)
response = requests.get(uri, headers=auth.auth_headers())

if response.ok:
data = json.loads(response.text)
Expand Down
11 changes: 3 additions & 8 deletions sureflap_api/modules/devices.py
Expand Up @@ -13,9 +13,7 @@
def get_devices() -> dict:
uri = f"{settings.ENDPOINT}/api/device"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(uri, headers=headers)
response = requests.get(uri, headers=auth.auth_headers())

if response.ok:
data = json.loads(response.text)
Expand All @@ -27,10 +25,9 @@ def get_devices() -> dict:
def get_devices_by_id(device_id: int) -> dict:
uri = f"{settings.ENDPOINT}/api/device/{device_id}"

headers = {'Authorization': f'Bearer {auth.getToken()}'}
payload = {'with[]': ['children', 'status', 'control']}

response = requests.get(uri, headers=headers, params=payload)
response = requests.get(uri, headers=auth.auth_headers(), params=payload)

if response.ok:
data = json.loads(response.text)
Expand All @@ -42,8 +39,6 @@ def get_devices_by_id(device_id: int) -> dict:
def set_lock_mode(device_id: int, lock_mode: str) -> dict:
uri = f"{settings.ENDPOINT}/api/device/{device_id}/control"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

# Set lock mode
lock_mode_id = None

Expand All @@ -64,7 +59,7 @@ def set_lock_mode(device_id: int, lock_mode: str) -> dict:
"locking": lock_mode_id
}

response = requests.put(uri, headers=headers, data=data)
response = requests.put(uri, headers=auth.auth_headers(), data=data)

if response.ok:
data = json.loads(response.text)
Expand Down
7 changes: 2 additions & 5 deletions sureflap_api/modules/households.py
Expand Up @@ -13,9 +13,7 @@
def get_households() -> list:
uri = f"{settings.ENDPOINT}/api/household"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(uri, headers=headers)
response = requests.get(uri, headers=auth.auth_headers())

if response.ok:
data = json.loads(response.text)
Expand All @@ -27,10 +25,9 @@ def get_households() -> list:
def get_household_by_id(household_id: int) -> dict:
uri = f"{settings.ENDPOINT}/api/household/{household_id}"

headers = {'Authorization': f'Bearer {auth.getToken()}'}
payload = {'with[]': ['pets', 'users']}

response = requests.get(uri, headers=headers, params=payload)
response = requests.get(uri, headers=auth.auth_headers(), params=payload)

if response.ok:
data = json.loads(response.text)
Expand Down
14 changes: 4 additions & 10 deletions sureflap_api/modules/pets.py
Expand Up @@ -14,9 +14,7 @@
def get_pets_from_household(household_id: int) -> list:
uri = f"{settings.ENDPOINT}/api/household/{household_id}/pet"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(uri, headers=headers)
response = requests.get(uri, headers=auth.auth_headers())

if response.ok:
data = json.loads(response.text)
Expand All @@ -28,10 +26,9 @@ def get_pets_from_household(household_id: int) -> list:
def get_pet(household_id: int, pet_id: int) -> dict:
uri = f"{settings.ENDPOINT}/api/household/{household_id}/pet"

headers = {'Authorization': f'Bearer {auth.getToken()}'}
payload = {'with[]': ['photo', 'position']}

response = requests.get(uri, headers=headers, params=payload)
response = requests.get(uri, headers=auth.auth_headers(), params=payload)

if response.ok:
data = json.loads(response.text)
Expand All @@ -46,9 +43,7 @@ def get_pet(household_id: int, pet_id: int) -> dict:
def get_pet_location(pet_id: int) -> dict:
uri = f"{settings.ENDPOINT}/api/pet/{pet_id}/position"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(uri, headers=headers)
response = requests.get(uri, headers=auth.auth_headers())

if response.ok:
data = json.loads(response.text)
Expand Down Expand Up @@ -103,13 +98,12 @@ def get_pets_location(household_id: int) -> list:
def set_pet_location(pet_id: int, pet_location: request_models.PetLocationSet) -> dict:
uri = f"{settings.ENDPOINT}/api/pet/{pet_id}/position"

headers = {'Authorization': f'Bearer {auth.getToken()}'}
body = {
"where": pet_location.where.value, # 1 = inside, 2 = outside
"since": datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S+00:00")
}

response = requests.post(uri, headers=headers, data=body)
response = requests.post(uri, headers=auth.auth_headers(), data=body)

if response.ok:
data = json.loads(response.text)
Expand Down
4 changes: 1 addition & 3 deletions sureflap_api/modules/timeline.py
Expand Up @@ -15,9 +15,7 @@ def getTimeline(household_id: int) -> str:
uri = f"{settings.ENDPOINT}/api/timeline/household/{household_id}"
result = []

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(uri, headers=headers, params={'page_size': 100})
response = requests.get(uri, headers=auth.auth_headers(), params={'page_size': 100})

if response.ok:
data = json.loads(response.text)
Expand Down
12 changes: 3 additions & 9 deletions sureflap_api/modules/users.py
Expand Up @@ -13,9 +13,7 @@
def get_users_from_household(household_id: int) -> list:
uri = f"{settings.ENDPOINT}/api/household/{household_id}/user"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(uri, headers=headers)
response = requests.get(uri, headers=auth.auth_headers())

if response.ok:
data = json.loads(response.text)
Expand All @@ -28,9 +26,7 @@ def get_users_from_household(household_id: int) -> list:
def get_user(user_id: int) -> dict:
uri = f"{settings.ENDPOINT}/api/user/{user_id}"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(uri, headers=headers)
response = requests.get(uri, headers=auth.auth_headers())

if response.ok:
data = json.loads(response.text)
Expand All @@ -44,9 +40,7 @@ def get_user_photo(user_id: int) -> dict:
userUri = f"{settings.ENDPOINT}/api/user/{user_id}"
photoUri = f"{settings.ENDPOINT}/api/photo/"

headers = {'Authorization': f'Bearer {auth.getToken()}'}

response = requests.get(userUri, headers=headers)
response = requests.get(userUri, headers=auth.auth_headers())

if response.ok:
data = json.loads(response.text)
Expand Down

0 comments on commit 34978a7

Please sign in to comment.