Skip to content
Snippets Groups Projects
Commit 0e299440 authored by David Trattnig's avatar David Trattnig
Browse files

refact: consolidate SimpleApi name with cached API

parent d5463dc4
No related branches found
No related tags found
1 merge request!20Preparations for ORM-less scheduling
......@@ -42,7 +42,7 @@ frequent basis:
Basic GET usage:
>>> from api import SimpleApi
>>> from api import SimpleRestApi
>>> r = api.get("https://aura.radio/foo.json")
>>> r.response.status_code
200
......@@ -65,14 +65,14 @@ from aura_engine.base.lang import DotDict
from aura_engine.base.utils import SimpleUtil as SU
class SimpleApi:
class SimpleRestApi:
"""
Simple wrapper on `requests` to deal with REST APIs.
Use it for services which do not want to deal with exception
handling but with results only.
SimpleApi has implicit logging of invalid states at logs
SimpleRestApi has implicit logging of invalid states at logs
to the `engine` logger by default.
"""
......@@ -164,7 +164,7 @@ class SimpleApi:
if value is None:
del data[key]
elif isinstance(value, dict):
SimpleApi.clean_dictionary(self, value)
SimpleRestApi.clean_dictionary(self, value)
return data
def serialize_json(self, data: dict, clean_data=True) -> str:
......@@ -221,9 +221,9 @@ class SimpleApi:
"""
json_data = None
if not headers:
headers = SimpleApi.default_headers
headers = SimpleRestApi.default_headers
response = requests.get(url, headers=headers)
if headers.get("content-type") == SimpleApi.CONTENT_JSON:
if headers.get("content-type") == SimpleRestApi.CONTENT_JSON:
json_data = self.deserialize_json(response)
return DotDict({"response": response, "json": json_data})
......@@ -235,7 +235,7 @@ class SimpleApi:
Args:
url (str): The URL of the request
data (dict): Data payload for request body
headers (dict, optional): Optional headers, defaults to `SimpleApi.default_headers`
headers (dict, optional): Optional headers, defaults to `SimpleRestApi.default_headers`
Returns:
{
......@@ -246,7 +246,7 @@ class SimpleApi:
"""
if not headers:
headers = SimpleApi.default_headers
headers = SimpleRestApi.default_headers
body: str = self.serialize_json(data)
return requests.put(url, data=body, headers=headers)
......@@ -258,7 +258,7 @@ class SimpleApi:
Args:
url (str): The URL of the request
data (dict): Data payload for request body
headers (dict, optional): Optional headers, defaults to `SimpleApi.default_headers`
headers (dict, optional): Optional headers, defaults to `SimpleRestApi.default_headers`
Returns:
{
......@@ -269,7 +269,7 @@ class SimpleApi:
"""
if not headers:
headers = SimpleApi.default_headers
headers = SimpleRestApi.default_headers
body: str = self.serialize_json(data)
return requests.put(url, data=body, headers=headers)
......@@ -290,11 +290,11 @@ class SimpleCachedRestApi:
"""
cache_location: str
simple_api: SimpleApi
simple_api: SimpleRestApi
logger = None
def __init__(self, simple_api: SimpleApi, cache_location: str, logger_name="engine"):
def __init__(self, simple_api: SimpleRestApi, cache_location: str, logger_name="engine"):
if cache_location[-1] != "/":
cache_location += "/"
cache_location += "api/"
......
......@@ -24,7 +24,7 @@ Update the clock information stored in Engine API.
import logging
from datetime import datetime, timedelta
from aura_engine.base.api import SimpleApi
from aura_engine.base.api import SimpleRestApi
from aura_engine.base.config import AuraConfig
from aura_engine.base.lang import DotDict
from aura_engine.resources import ResourceUtil
......@@ -46,7 +46,7 @@ class ClockInfoHandler:
"""
self.logger = logging.getLogger("engine")
self.config = AuraConfig.config()
self.api = SimpleApi()
self.api = SimpleRestApi()
self.engine = engine
if not self.engine:
......
......@@ -25,7 +25,7 @@ import logging
import queue
import threading
from aura_engine.base.api import SimpleApi
from aura_engine.base.api import SimpleRestApi
from aura_engine.base.config import AuraConfig
from aura_engine.base.lang import private
from aura_engine.base.utils import SimpleUtil as SU
......@@ -68,7 +68,7 @@ class ApiFetcher(threading.Thread):
"""
self.config = AuraConfig.config()
self.logger = logging.getLogger("engine")
self.api = SimpleApi()
self.api = SimpleRestApi()
self.url_api_timeslots = self.config.get("api_steering_calendar")
self.url_api_playlist = self.config.get("api_tank_playlist")
self.queue = queue.Queue()
......
......@@ -21,7 +21,7 @@ import os
import unittest
from unittest import mock
from aura_engine.base.api import SimpleApi, SimpleCachedRestApi
from aura_engine.base.api import SimpleCachedRestApi, SimpleRestApi
from aura_engine.base.config import AuraConfig
......@@ -37,7 +37,7 @@ class TestCachedApi(unittest.TestCase):
# Mock
#
# Mock `requests.get` in `SimpleApi`
# Mock `requests.get` in `SimpleRestApi`
def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
......@@ -64,7 +64,7 @@ class TestCachedApi(unittest.TestCase):
def setUp(self):
self.config = AuraConfig()
cache_location = self.config.get("cache_dir")
self.api = SimpleCachedRestApi(SimpleApi(), cache_location)
self.api = SimpleCachedRestApi(SimpleRestApi(), cache_location)
#
# Test Cases
......
......@@ -22,7 +22,7 @@ from unittest import mock
import requests
from aura_engine.base.api import SimpleApi
from aura_engine.base.api import SimpleRestApi
from aura_engine.base.config import AuraConfig
......@@ -38,7 +38,7 @@ class TestApi(unittest.TestCase):
# Mock
#
# Mock `requests.get` in `SimpleApi`
# Mock `requests.get` in `SimpleRestApi`
def mocked_requests_get(*args, **kwargs):
class MockResponse:
def __init__(self, json_data, status_code):
......@@ -66,7 +66,7 @@ class TestApi(unittest.TestCase):
def setUp(self):
self.config = AuraConfig()
self.api = SimpleApi()
self.api = SimpleRestApi()
#
# Tests
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment