From 9bbd056c1bc6a30059a8ed0a47823c228531e4c0 Mon Sep 17 00:00:00 2001 From: Ushie Date: Sat, 19 Aug 2023 22:35:09 +0300 Subject: [PATCH] feat: info endpoint (#71) Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- api/__init__.py | 3 +- api/info.py | 31 ++++++++++ api/models/info.py | 23 ++++++++ api/models/socials.py | 4 +- config.py | 131 +++++++++++++++++++++++------------------- 5 files changed, 129 insertions(+), 63 deletions(-) create mode 100644 api/info.py create mode 100644 api/models/info.py diff --git a/api/__init__.py b/api/__init__.py index d84b20a..0967429 100644 --- a/api/__init__.py +++ b/api/__init__.py @@ -4,7 +4,8 @@ from sanic import Blueprint from api.github import github from api.ping import ping from api.socials import socials +from api.info import info from api.compat import github as old from api.donations import donations -api = Blueprint.group(ping, github, socials, donations, old, url_prefix="/") +api = Blueprint.group(ping, github, info, socials, donations, old, url_prefix="/") diff --git a/api/info.py b/api/info.py new file mode 100644 index 0000000..18b8375 --- /dev/null +++ b/api/info.py @@ -0,0 +1,31 @@ +""" +This module provides a blueprint for the info endpoint. + +Routes: + - GET /info: Get info about the owner of the API. +""" + +from sanic import Blueprint, Request +from sanic.response import JSONResponse, json +from sanic_ext import openapi + +from api.models.info import InfoResponseModel +from config import api_version, default_info + +info: Blueprint = Blueprint("info", version=api_version) + + +@info.get("/info") +@openapi.definition( + summary="Information about the API", + response=[InfoResponseModel], +) +async def root(request: Request) -> JSONResponse: + """ + Returns a JSONResponse with a dictionary containing info about the owner of the API. + + **Returns:** + - JSONResponse: A Sanic JSONResponse instance containing a dictionary with the info about the owner of the API. + """ + data: dict[str, dict] = {"info": default_info} + return json(data, status=200) diff --git a/api/models/info.py b/api/models/info.py new file mode 100644 index 0000000..d1f3681 --- /dev/null +++ b/api/models/info.py @@ -0,0 +1,23 @@ +from api.models.donations import DonationFields +from api.models.socials import SocialFields +from pydantic import BaseModel + + +class InfoFields(BaseModel): + """ + Implements the fields for a API owner info. + """ + + name: str + about: str + contact: dict[str, str] + socials: SocialFields + donations: DonationFields + + +class InfoResponseModel(BaseModel): + """ + A Pydantic BaseModel that represents a dictionary of info. + """ + + info: InfoFields diff --git a/api/models/socials.py b/api/models/socials.py index 0b43821..1a0929f 100644 --- a/api/models/socials.py +++ b/api/models/socials.py @@ -1,7 +1,7 @@ from pydantic import BaseModel -class SocialField(BaseModel): +class SocialFields(BaseModel): """ Implements the fields for a social network link. """ @@ -15,7 +15,7 @@ class SocialsResponseModel(BaseModel): A Pydantic BaseModel that represents a dictionary of social links. """ - socials: list[SocialField] + socials: list[SocialFields] """ A dictionary where the keys are the names of the social networks, and the values are the links to the profiles or pages. diff --git a/config.py b/config.py index 4b922b5..ffea30c 100644 --- a/config.py +++ b/config.py @@ -1,13 +1,68 @@ +# API Configuration + +backend: str = "github" +redis: dict[str, str | int] = {"host": "localhost", "port": 6379} + +# GitHub Backend Configuration + +owner: str = "ReVanced" +default_repository: str = ".github" + +# API Versioning + +api_version: str = "v2" +openapi_version: str = "2.0.0" +openapi_title: str = "ReVanced API" +openapi_description: str = """ +## The official JSON API for ReVanced Releases 🚀 + +### Links + +- [Changelogs](https://github.com/revanced/) +- [Official links to ReVanced](https://revanced.app) + +### Important Information + +* Rate Limiting - 60 requests per minute +* Cache - 5 minutes + +### Additional Notes + +1. Breaking changes are to be expected +2. Client side caching is advised to avoid unnecessary requests +3. Abuse of the API will result in IP blocks +""" + +# Testing Configuration + +github_testing_repository: str = "revanced-patches" +github_testing_tag: str = "v2.173.0" +apkdl_testing_package: str = "com.google.android.youtube" + +# Old API Configuration + +compat_api_version: str = "v1" +compat_repositories: list = [ + "revanced-patcher", + "revanced-patches", + "revanced-integrations", + "revanced-manager", + "revanced-cli", + "revanced-website", + "revanced-api", + "revanced-releases-api", +] + # Social Links social_links: list[dict[str, str]] = [ - {"name": "website", "url": "https://revanced.app"}, - {"name": "github", "url": "https://github.com/revanced"}, - {"name": "twitter", "url": "https://twitter.com/revancedapp"}, - {"name": "discord", "url": "https://revanced.app/discord"}, - {"name": "reddit", "url": "https://www.reddit.com/r/revancedapp"}, - {"name": "telegram", "url": "https://t.me/app_revanced"}, - {"name": "youtube", "url": "https://www.youtube.com/@ReVanced"}, + {"name": "Website", "url": "https://revanced.app"}, + {"name": "GitHub", "url": "https://github.com/revanced"}, + {"name": "Twitter", "url": "https://twitter.com/revancedapp"}, + {"name": "Discord", "url": "https://revanced.app/discord"}, + {"name": "Reddit", "url": "https://www.reddit.com/r/revancedapp"}, + {"name": "Telegram", "url": "https://t.me/app_revanced"}, + {"name": "YouTube", "url": "https://www.youtube.com/@ReVanced"}, ] # Donation info @@ -58,56 +113,12 @@ links: list[dict[str, str | bool]] = [ }, ] -# API Configuration - -backend: str = "github" -redis: dict[str, str | int] = {"host": "localhost", "port": 6379} - -# GitHub Backend Configuration - -owner: str = "revanced" -default_repository: str = ".github" - -# API Versioning - -api_version: str = "v2" -openapi_version: str = "2.0.0" -openapi_title: str = "ReVanced API" -openapi_description: str = """ -## The official JSON API for ReVanced Releases 🚀 - -### Links - -- [Changelogs](https://github.com/revanced/) -- [Official links to ReVanced](https://revanced.app) - -### Important Information - -* Rate Limiting - 60 requests per minute -* Cache - 5 minutes - -### Additional Notes - -1. Breaking changes are to be expected -2. Client side caching is advised to avoid unnecessary requests -3. Abuse of the API will result in IP blocks -""" - -# Testing Configuration - -github_testing_repository: str = "revanced-patches" -github_testing_tag: str = "v2.173.0" -apkdl_testing_package: str = "com.google.android.youtube" - -# Old API Configuration - -compat_api_version: str = "v1" -compat_repositories: list = [ - "revanced-patcher", - "revanced-patches", - "revanced-integrations", - "revanced-manager", - "revanced-cli", - "revanced-website", - "revanced-releases-api", -] +default_info: dict[str, str | list[str | bool] | bool] = { + "name": owner, + "about": "ReVanced was born out of Vanced's discontinuation and it is our goal to continue the legacy of what Vanced left behind. Thanks to ReVanced Patcher, it's possible to create long-lasting patches for nearly any Android app. ReVanced's patching system is designed to allow patches to work on new versions of the apps automatically with bare minimum maintenance.", + "contact": [ + {"method": "mail", "value": "contact@revanced.app"}, + ], + "socials": social_links, + "donations": {"wallets": wallets, "links": links}, +}