Trying to make session embeddable
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "rapt-cloud-api"
|
name = "rapt-cloud-api"
|
||||||
version = "0.2.1"
|
version = "0.2.2"
|
||||||
description = "Python bindings for the Rapt.io API"
|
description = "Python bindings for the Rapt.io API"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.14"
|
requires-python = ">=3.14"
|
||||||
|
|||||||
23
setup.py
23
setup.py
@@ -1,23 +0,0 @@
|
|||||||
from setuptools import find_packages, setup
|
|
||||||
|
|
||||||
with open("README.md", "r", encoding="utf-8") as fh:
|
|
||||||
long_description = fh.read()
|
|
||||||
|
|
||||||
setup(
|
|
||||||
name='rapt-cloud-api',
|
|
||||||
version='0.2.0',
|
|
||||||
author='Jesper Fussing Mørk',
|
|
||||||
author_email='jfmo@moerks.dk',
|
|
||||||
description='Client Library for the rapt.io api',
|
|
||||||
long_description=long_description,
|
|
||||||
long_description_content_type="text/markdown",
|
|
||||||
url='https://gitea.moerks.dk/jfm/rapt-cloud-api',
|
|
||||||
project_urls={
|
|
||||||
"Bug Tracker": "https://gitea.moerks.dk/jfm/rapt-cloud-api/issues",
|
|
||||||
},
|
|
||||||
license='MIT',
|
|
||||||
package_dir={"": "src"},
|
|
||||||
packages=find_packages(where="src"),
|
|
||||||
install_requires=["loguru", "pydantic", "aiohttp"]
|
|
||||||
)
|
|
||||||
|
|
||||||
@@ -21,8 +21,12 @@ class HttpClientException(ClientException):
|
|||||||
|
|
||||||
|
|
||||||
class Client():
|
class Client():
|
||||||
def __init__(self, username, api_key) -> None:
|
def __init__(self, username, api_key, session=None) -> None:
|
||||||
self.host = "https://api.rapt.io"
|
self.host = "https://api.rapt.io"
|
||||||
|
if session is None:
|
||||||
|
self.session = aiohttp.ClientSession()
|
||||||
|
else:
|
||||||
|
self.session = session
|
||||||
self.username = username
|
self.username = username
|
||||||
self.api_key = api_key
|
self.api_key = api_key
|
||||||
self.token = None
|
self.token = None
|
||||||
@@ -30,15 +34,13 @@ class Client():
|
|||||||
self.expires = None
|
self.expires = None
|
||||||
|
|
||||||
async def get_jwt_token(self):
|
async def get_jwt_token(self):
|
||||||
async with aiohttp.ClientSession() as session:
|
|
||||||
url = "https://id.rapt.io/connect/token"
|
url = "https://id.rapt.io/connect/token"
|
||||||
async with session.post(url, data={
|
async with self.session.post(url, data={
|
||||||
"client_id": "rapt-user",
|
"client_id": "rapt-user",
|
||||||
"grant_type": "password",
|
"grant_type": "password",
|
||||||
"username": self.username,
|
"username": self.username,
|
||||||
"password": self.api_key
|
"password": self.api_key
|
||||||
}) as response:
|
}) as response:
|
||||||
|
|
||||||
response_json = await response.json()
|
response_json = await response.json()
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
self.token = response_json["access_token"]
|
self.token = response_json["access_token"]
|
||||||
@@ -59,9 +61,7 @@ class Client():
|
|||||||
async def get_json(self, url, parameters):
|
async def get_json(self, url, parameters):
|
||||||
headers = await self.get_auth_headers()
|
headers = await self.get_auth_headers()
|
||||||
logger.trace("HEADERS: " + str(headers))
|
logger.trace("HEADERS: " + str(headers))
|
||||||
async with aiohttp.ClientSession() as session:
|
async with self.session.get(url, headers=headers, params=parameters) as response:
|
||||||
async with session.get(url, headers=headers, params=parameters) as response:
|
|
||||||
|
|
||||||
response_json = await response.json()
|
response_json = await response.json()
|
||||||
logger.trace(response.request_info.url)
|
logger.trace(response.request_info.url)
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ from rapt.hydrometer import Hydrometer
|
|||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
async def client(aiohttp_client):
|
async def client(aiohttp_client):
|
||||||
app = web.Application()
|
app = web.Application()
|
||||||
await aiohttp_client(app)
|
session = await aiohttp_client(app)
|
||||||
yield Client("test", "test")
|
yield Client("test", "test", None)
|
||||||
|
|
||||||
async def test_get_hydrometers(client):
|
async def test_get_hydrometers(client):
|
||||||
with aioresponses() as responses:
|
with aioresponses() as responses:
|
||||||
|
|||||||
Reference in New Issue
Block a user