Coverage for sfkit/api.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-08-07 15:11 -0400

1from io import IOBase 

2 

3import requests 

4 

5from sfkit.utils import constants 

6 

7 

8def website_send_file(file: IOBase, filename: str) -> bool: 

9 files = {"file": (filename, file)} 

10 url = f"{constants.SFKIT_API_URL}/upload_file" 

11 with open(constants.AUTH_KEY, "r") as f: 

12 auth_key = f.readline().rstrip() 

13 

14 headers = { 

15 "Authorization": f"{auth_key}", 

16 # "content-type": "application/json", 

17 } 

18 response = requests.post(url, files=files, headers=headers) 

19 

20 return response.status_code == 200 

21 

22 

23def website_get(request_type: str, params: dict = dict()) -> requests.Response: 

24 url = f"{constants.SFKIT_API_URL}/{request_type}" 

25 

26 with open(constants.AUTH_KEY, "r") as f: 

27 auth_key = f.readline().rstrip() 

28 

29 headers = { 

30 "Authorization": f"{auth_key}", 

31 "content-type": "application/json", 

32 } 

33 return requests.get(url, headers=headers, params=params) 

34 

35 

36def get_doc_ref_dict() -> dict: 

37 response = website_get("get_doc_ref_dict") 

38 return response.json() 

39 

40 

41def get_username() -> str: 

42 response = website_get("get_username") 

43 return response.json().get("username", "") 

44 

45 

46def update_firestore(msg: str) -> bool: 

47 print(f"Updating firestore with msg: {msg}") 

48 response = website_get("update_firestore", params={"msg": msg}) 

49 return response.status_code == 200 

50 

51 

52def create_cp0() -> bool: 

53 response = website_get("create_cp0") 

54 return response.status_code == 200