Pytest async tests and fixtures

Async tests and fixtures Pytest already has the official pytest-asyncio plugin which allows you to write async tests and fixtures. According to pytest-asyncio docs, you can have an async test with an async fixture like this: from typing import Any, AsyncGenerator import pytest import pytest_asyncio from httpx import AsyncClient @pytest_asyncio.fixture async def client() -> AsyncGenerator[AsyncClient, Any]: async with AsyncClient() as c: yield c @pytest.mark.asyncio async def test_api_call(client: AsyncClient) -> None: response = await client....

June 29, 2023 · 2 min · 363 words · Amin
HTTP Persistent Connection

Python requests Connection Pool

Summary Recently I was working on a project that integrated with some internal and external APIs using HTTP requests. You probably have heard about or worked with the requests library in Python which is probably the de-facto HTTP client package which is much easier to work with compared to the built-in HTTP module of Python. The previous implementation in our project was using the requests library and for each new request it did something like requests....

February 16, 2023 · 4 min · 719 words · Amin

Pydantic Settings with AWS Secrets Manager

Intro If you are already familiar with Pydantic, one of the useful components of Pydantic is the Settings Management. This will allow you to read settings variables from different sources and parse and validate them into class(es) using Pydantic. Let’s see a minimal example. First we need to set up the variable: $ export API_KEY=xxx And then we can read it into the Settings class with: from pydantic import BaseSettings class Settings(BaseSettings): api_key: str print(Settings()....

January 26, 2023 · 5 min · 1002 words · Amin
AWS Lambgda

FastAPI AWS Lambda deployment

Intro From AWS website: AWS Lambda is a serverless, event-driven compute service that lets you run code for virtually any type of application or backend service without provisioning or managing servers. Even though it’s not required to use a web framework like FastAPI or Django to start using AWS Lambda, often it is the case you have an existing project or you want to use a framework for useful features....

November 1, 2022 · 4 min · 699 words · Amin
JSON Logging

Python JSON Logging

Use-case It’s very common for projects to do JSON logging if you are working with third-party tools or open-source projects like Logstash to process your logs. These tools usually need more complex filtering on the structured data, so using JSON is preferred there. We also wanted to integrate with a third-party tool at work and we needed to add the JSON formatting logs in our projects. I will not go into the details of why or why not you should decide JSON logging, as each approach will have it’s pros and cons....

September 14, 2022 · 3 min · 484 words · Amin