-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path__init__.py
More file actions
65 lines (52 loc) · 1.8 KB
/
__init__.py
File metadata and controls
65 lines (52 loc) · 1.8 KB
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
"""
This file is part of pysofar: A client for interfacing with Sofar Ocean's Spotter API
Copyright 2019-2024
Sofar Ocean Technologies
Authors: Mike Sosa et al.
"""
import os
import dotenv
import requests
import json
def get_token():
# config values
userpath = os.path.expanduser("~")
environmentFile = os.path.join(userpath, 'sofar_api.env')
dotenv.load_dotenv(environmentFile)
token = os.getenv('WF_API_TOKEN')
_wavefleet_token = token
return _wavefleet_token
def get_endpoint():
_endpoint = os.getenv('WF_URL')
if _endpoint is None:
_endpoint = 'https://api.sofarocean.com/api'
return _endpoint
class SofarConnection:
"""
Base Parent class for connections to the API
Use SofarApi in sofar.py in practice
"""
def __init__(self, custom_token=None):
self._token = custom_token or get_token()
self.endpoint = get_endpoint()
self.header = {'token': self._token, 'Content-Type': 'application/json'}
# Helper methods
def _get(self, endpoint_suffix, params: dict = None):
url = f"{self.endpoint}/{endpoint_suffix}"
if params is None:
response = requests.get(url, headers=self.header)
else:
response = requests.get(url, headers=self.header, params=params)
status = response.status_code
data = response.json()
return status, data
def _post(self, endpoint_suffix, json_data):
response = requests.get(f"{self.endpoint}/{endpoint_suffix}",
json=json_data,
headers=self.header)
status = response.status_code
data = response.json()
return status, data
def set_token(self, new_token):
self._token = new_token
self.header.update({'token': new_token})