A secure, containerized FastAPI application providing token-based authentication and a suite of RESTful endpoints for news aggregation and delivery.
This backend service offers:
-
OAuth2 authentication with JWT tokens.
-
Dockerized environment for consistent deployment.
-
PEP-8 compliance with integrated linting tools.
-
Automated testing using
pytestandcoverage. -
Comprehensive API with endpoints for token generation, news retrieval, and more.
-
Docker
-
Docker Compose
-
Unix-like shell environment (e.g., Bash)
-
Clone the Repository
git clone https://github.com/NumanIbnMazid/blockstack-newsapi && cd blockstack-newsapi
-
Configure Environment Variables
Create a
.envfile in the root directory usingenv.sample: -
Build and Run the Docker Containers
docker compose build && docker-compose up -dThe application will be accessible at
http://localhost:8000.
A run.sh script is provided to automate linting and testing within the Docker container.
chmod +x run.sh && ./run.shThis script performs the following:
-
Builds and starts the Docker containers.
-
Runs
flake8for PEP-8 compliance checks. -
Executes tests using
pytestwith coverage reporting.
docker compose builddocker compose up -ddocker compose down -vSend a POST request to the /token endpoint with your client_id and client_secret:
curl -X POST http://localhost:8000/token \ -F 'client_id=your_client_id' \ -F 'client_secret=your_client_secret'Response:
{
"access_token": "your_jwt_token",
"token_type": "bearer"
}Include the obtained token in the Authorization header:
curl -H "Authorization: Bearer your_jwt_token" http://localhost:8000/protected-endpointA detailed FAST API provided API Documentation can be found at http://127.0.0.1:8000/docs.
-
Description: Generates a JWT access token using OAuth2 client credentials grant.
-
Parameters:
-
client_id(form data) -
client_secret(form data)
-
-
Response:
-
access_token: JWT token -
token_type: "bearer"
-
-
Description: Retrieves a paginated list of news articles from NewsAPI.
-
Headers:
Authorization: Bearer token
-
Query Parameters:
-
page: Page number (optional) -
page_size: Number of articles per page (optional)
-
-
Response:
- List of news articles in JSON format.
-
Description: Fetches the latest news from NewsAPI and saves the top 3 articles into the database.
-
Headers:
Authorization: Bearer token
-
Response:
- Details of the saved articles in JSON format.
-
Description: Fetches top headlines by country from NewsAPI.
-
Parameters:
country_code: Two-letter country code (e.g., 'us', 'de').
-
Headers:
Authorization: Bearer token
-
Response:
- List of top headlines for the specified country in JSON format.
-
Description: Fetches top headlines by source from NewsAPI.
-
Parameters:
source_id: News source identifier (e.g., 'bbc-news').
-
Headers:
Authorization: Bearer token
-
Response:
- List of top headlines for the specified source in JSON format.
-
Description: Fetches top headlines from NewsAPI filtered by various criteria.
-
Headers:
Authorization: Bearer token
-
Query Parameters:
-
country: Two-letter country code (optional). -
source: News source identifier (optional). -
category: News category (e.g., 'business', 'technology') (optional).
-
-
Response:
- List of top headlines matching the specified filters in JSON format.
To further enhance the codebase:
-
Database Integration: Transition from SQLite storage to a robust database system like PostgreSQL for efficient data management.
-
Logging: Integrate a structured logging system to facilitate debugging and monitoring.