-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor requests with specific handlers and remove kit
- Loading branch information
Showing
14 changed files
with
228 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
from .models import (Sensor, Measurement, Kit, Owner, Location, | ||
HardwareInfo, Postprocessing, Data, Device) | ||
from .models import (Sensor, Measurement, Owner, User, Location, | ||
HardwareInfo, Postprocessing, Data, Device, Experiment) | ||
from .handler import HttpHandler | ||
from .device import SCDevice#, get_devices | ||
from .sensor import SCSensor, get_sensors | ||
from .measurement import SCMeasurement, get_measurements | ||
from .sensor import SensorHandler, get_sensors | ||
from .measurement import MeasurementHandler, get_measurements | ||
from .experiment import ExperimentHandler, get_experiments | ||
from .search import search_by_query, global_search | ||
from .user import UserHandler, get_users | ||
|
||
__all__ = [ | ||
"Device", | ||
"Kit", | ||
"Sensor", | ||
"Measurement", | ||
"User", | ||
"Owner", | ||
"Location", | ||
"Data", | ||
"Postprocessing", | ||
"HardwareInfo" | ||
"HardwareInfo", | ||
"Experiment" | ||
] | ||
|
||
__version__ = '1.1.3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .experiment import ExperimentHandler, get_experiments |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from smartcitizen_connector.models import Experiment | ||
from smartcitizen_connector._config import config | ||
from smartcitizen_connector.tools import * | ||
from pydantic import TypeAdapter | ||
from typing import Optional, List | ||
from smartcitizen_connector.handler import HttpHandler | ||
|
||
# TODO - Can this inherit from experiment? | ||
class ExperimentHandler(HttpHandler): | ||
|
||
def __init__(self, id: int = None, **kwargs): | ||
self.id = id | ||
super().__init__(config.EXPERIMENTS_URL) | ||
|
||
if self.id is not None: | ||
r = self.get() | ||
self.model = TypeAdapter(Experiment).validate_python(r.json()) | ||
else: | ||
self.model = Experiment(**kwargs) | ||
|
||
def __getattr__(self, attr): | ||
return self.model.__getattribute__(attr) | ||
|
||
def get_experiments(): | ||
isn = True | ||
result = list() | ||
url = config.EXPERIMENTS_URL | ||
|
||
while isn: | ||
r = get(url) | ||
r.raise_for_status() | ||
# If status code OK, retrieve data | ||
h = process_headers(r.headers) | ||
result += TypeAdapter(List[Experiment]).validate_python(r.json()) | ||
|
||
if 'next' in h: | ||
if h['next'] == url: isn = False | ||
elif h['next'] != url: url = h['next'] | ||
else: | ||
isn = False | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .httphandler import HttpHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
from requests import get, patch, post, delete | ||
from os import environ | ||
from smartcitizen_connector.tools import logger | ||
from typing import Optional | ||
import json | ||
|
||
class HttpHandler: | ||
url: Optional[str] = None | ||
|
||
def __init__(self, path: str): | ||
self.path = path | ||
self.__set_headers__() | ||
if self.id is not None: | ||
self.url = f'{self.path}{self.id}' | ||
|
||
def __set_headers__(self): | ||
|
||
self.headers = { | ||
'Content-type': 'application/json' | ||
} | ||
|
||
if 'SC_BEARER' not in environ: | ||
logger.warning('No Auth Bearer set. Will not be able to POST, PATCH, DELETE. Include it environment variable with SC_BEARER') | ||
return False | ||
|
||
logger.info('Using Auth Bearer') | ||
self.headers['Authorization'] = 'Bearer ' + environ['SC_BEARER'] | ||
|
||
return True | ||
|
||
def get(self): | ||
r = get(self.url) | ||
r.raise_for_status() | ||
return r | ||
|
||
def patch(self, property: str): | ||
r = patch(self.url, | ||
data=self.model.json(include=property, | ||
exclude_none=True), | ||
headers = self.headers | ||
) | ||
r.raise_for_status() | ||
return r | ||
|
||
def post(self): | ||
r = post(self.path, | ||
data=self.model.json(exclude_none=True), | ||
headers = self.headers) | ||
|
||
r.raise_for_status() | ||
return r | ||
|
||
def delete(self): | ||
r = delete(self.url, | ||
headers = self.headers) | ||
|
||
r.raise_for_status() | ||
return r |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .measurement import SCMeasurement, get_measurements | ||
from .measurement import MeasurementHandler, get_measurements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from .models import (Sensor, Measurement, Kit, Owner, Location, Metric, | ||
HardwareInfo, HardwarePostprocessing, Postprocessing, Data, Device, HardwareStatus, Policy) | ||
from .models import (Sensor, Measurement, Owner, User, Location, Metric, | ||
HardwareInfo, HardwarePostprocessing, Postprocessing, | ||
Data, Device, HardwareStatus, Policy, Experiment) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
from .sensor import SCSensor, get_sensors | ||
from .sensor import SensorHandler, get_sensors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .user import UserHandler, get_users |
Oops, something went wrong.