commit b49750de949ee800658b3334a62d4c13bdc82403 Author: Théophile Bastian Date: Tue Sep 29 08:26:46 2020 +0200 Initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..90e373e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__ +venv +*.egg-info diff --git a/README.md b/README.md new file mode 100644 index 0000000..ad77985 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# WiFi SNCF API + +This package provides an asyncio-enabled, pythonic wrapper around the +"SNCF_WIFI_INOUI" API. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..aa983e7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +aiohttp +aiodns diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..0af431f --- /dev/null +++ b/setup.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 + +from setuptools import setup, find_packages + + +def parse_requirements(): + reqs = [] + with open("requirements.txt", "r") as handle: + for line in handle: + reqs.append(line) + return reqs + + +setup( + name="wifi_sncf_api", + version="0.1", + description="API de wifi.sncf", + author="tobast", + author_email="contact@tobast.fr", + license="LICENSE", + packages=find_packages(), + include_package_data=True, + long_description=open("README.md").read(), + install_requires=parse_requirements(), +) diff --git a/test.py b/test.py new file mode 100644 index 0000000..9cb76c4 --- /dev/null +++ b/test.py @@ -0,0 +1,20 @@ +import asyncio +import logging +import train_api + +logger = logging.getLogger(__name__) + + +async def mainloop(): + async with train_api.ApiSession() as api: + details = await api.details() + logger.info("Details: <%s>", details) + + +def main(): + logging.basicConfig(level=logging.DEBUG) + asyncio.get_event_loop().run_until_complete(mainloop()) + + +if __name__ == "__main__": + main() diff --git a/wifi_sncf/__init__.py b/wifi_sncf/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wifi_sncf/train_api.py b/wifi_sncf/train_api.py new file mode 100644 index 0000000..d3da3da --- /dev/null +++ b/wifi_sncf/train_api.py @@ -0,0 +1,45 @@ +import asyncio +import aiohttp +import json + + +class ApiSession: + ENDPOINT = "https://wifi.sncf/router/api" + + def __init__(self): + self.session = aiohttp.ClientSession() + + async def __aenter__(self): + return self + + async def __aexit__(self, *args): + await self.session.close() + + async def _json_api_request(self, endpoint): + url = self.ENDPOINT + endpoint + async with self.session.get(url) as response: + return await response.json() + + def details(self): + return self._json_api_request("/train/details") + + def gps(self): + return self._json_api_request("/train/gps") + + def graph(self): + return self._json_api_request("/train/graph") + + def coverage(self): + return self._json_api_request("/train/coverage") + + def connection_status(self): + return self._json_api_request("/connection/status") + + def connection_statistics(self): + return self._json_api_request("/connection/statistics") + + def modules(self): + return self._json_api_request("/configuration/modules") + + def bar(self): + return self._json_api_request("/bar/attendance")