Gardening App: API Client

Rachela
1 min readAug 20, 2021

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]

--

--

Rachela

I am a librarian and new computer programmer creating an app with Python and Kivy to document the plants in my garden.