Gardening App: API Client

Since I created a server to host my API queries, I decided to create a separate API Client. This made it so that I did not need to include any of the code dealing with interacting with the API in my Kivy files. To do this I imported aiohttp for the client and ascyncio for the app .py files. Here are some examples:

Client file: garden_api_client.py

import json
import aiohttp
class GardenAPIClient: @staticmethod
async def get_garden_list():
async with aiohttp.ClientSession() as session:
async with session.get(f"{HOST_URL}/gardens") as response:
if response.status == 200:
raw = await response.text()
data = [tuple(item.values()) for item in json.loads(raw)]
return data
else:
return None

Snippet from app file create_garden:

from garden_api_client import GardenAPIClientdef data_for_gardens_list(self):
loop = asyncio.get_event_loop()
garden_data = loop.run_until_complete(GardenAPIClient.get_garden_list())
records = []
for garden in garden_data:
record = {"font_size": "20sp", "text": f"{garden[1]}",
"input_data": garden, "on_press": lambda _garden=garden: self.select_garden(_garden)}
records.append(record)
return [x for x in records]

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store