Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

"""LICENSE 

Copyright 2019 Hermann Krumrey <hermann@krumreyh.com> 

 

This file is part of bundesliga-tippspiel-reminder (btr). 

 

btr is free software: you can redistribute it and/or modify 

it under the terms of the GNU General Public License as published by 

the Free Software Foundation, either version 3 of the License, or 

(at your option) any later version. 

 

btr is distributed in the hope that it will be useful, 

but WITHOUT ANY WARRANTY; without even the implied warranty of 

MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

GNU General Public License for more details. 

 

You should have received a copy of the GNU General Public License 

along with btr. If not, see <http://www.gnu.org/licenses/>. 

LICENSE""" 

 

import json 

import requests 

from base64 import b64encode 

from typing import Dict, Any, Optional 

 

 

def api_request( 

endpoint: str, 

method: str, 

params: Dict[Any, Any], 

api_key: Optional[str] = None 

) -> Dict[Any, Any]: 

""" 

Sends a request to the API 

:param endpoint: The endpoint to send the request to 

:param method: The HTTP method to use 

:param params: The parameters to use 

:param api_key: The API key to use 

:return: The response 

""" 

url = "https://hk-tippspiel.com/api/v2/" + endpoint 

 

headers = {} # type: Dict[str, str] 

if api_key is not None: 

encoded = b64encode(api_key.encode("utf-8")).decode("utf-8") 

headers = { 

"Authorization": "Basic {}".format(encoded) 

} 

 

return json.loads(requests.request( 

method, 

url, 

headers=headers, 

json=params, 

).text) 

 

 

def api_is_authorized(api_key: Optional[str]) -> bool: 

""" 

Checks whether or not an API key is authorized 

:param api_key: The API key to check 

:return: True if authorized, False otherwise 

""" 

if api_key is None: 

return False 

else: 

resp = api_request("authorize", "get", {}, api_key) 

return resp["status"] == "ok"