https://github.com/betofigueiredo/diff_tracer
A FastAPI utility designed to compare two API responses, making it easier to validate behavior and ensure accuracy during refactoring.
https://github.com/betofigueiredo/diff_tracer
fastapi python refactoring
Last synced: 4 months ago
JSON representation
A FastAPI utility designed to compare two API responses, making it easier to validate behavior and ensure accuracy during refactoring.
- Host: GitHub
- URL: https://github.com/betofigueiredo/diff_tracer
- Owner: betofigueiredo
- License: mit
- Created: 2024-11-11T23:06:46.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-14T17:16:47.000Z (over 1 year ago)
- Last Synced: 2025-01-18T10:11:45.841Z (over 1 year ago)
- Topics: fastapi, python, refactoring
- Language: Python
- Homepage:
- Size: 243 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
DIFF TRACER
A FastAPI utility designed to compare two API responses, making it easier to validate behavior and ensure accuracy during refactoring.
> [!WARNING]
> Please keep in mind that Diff Tracer is still under active development
## Installation
```zsh
❯ pip install diff_tracer
```
## Basic Usage
To compare two results:
```python
from diff_tracer import compare_async
@app.get("/")
async def get_data():
return await compare_async(
current_fn=lambda: currentUseCase(), # current function in production
new_fn=lambda: newUseCase(), # new refactored function
percentage=80, # percentage of requests to compare, good to control expensive endpoints
)
```
Active the dashboard endpoint in your main file:
```python
from fastapi import FastAPI
from diff_tracer import init_web_view
app = FastAPI()
init_web_view(app=app, security_token="362a9b3f302542deb3184671bbc3e7da")
```
Access http://localhost:{PORT}/diff-tracer-view/362a9b3f302542deb3184671bbc3e7da to view the dashboard.
Check a full working example on `./diff_tracer/fastapi_example.py`
## Why
I used this aproach while working on a major endpoint refactor at my current company. The endpoint was critical but lacked comprehensive tests, making it challenging to ensure the refactored function behaved identically to the original. While I wrote some tests, I wanted an extra layer of confidence before deploying to production.
## Known issues
- The dashboard endpoint is not secure. You can set a token to make it harder to access, but it's still not secure.
- The files are saved local on your API, so everytime you make a new deploy they will be erased.
## Contributing
For local development just install the libraries and start the FastAPI example file:
```zsh
❯ poetry install
❯ poetry run task start_api
```
Access http://localhost:8000/users to simulate requests.
Access http://localhost:8000/diff-tracer-view/1234 to view de dashboard. 1234 is the default token.
To run the tests:
```zsh
❯ poetry run task test
```
## Thanks to
The code that makes the comparison is from Google Diff, Match and Patch Library written by Neil Fraser Copyright (c) 2006 Google Inc. http://code.google.com/p/google-diff-match-patch/