Add gitea router

- added a gitea router
  - /gitea/latest endpoint gives the latest release version of a repo.
This commit is contained in:
Riley Housden 2021-12-02 05:21:39 -05:00
parent b891efe601
commit 88ffd71911
Signed by: InValidFire
GPG key ID: 0D6208F6DF56B4D8
3 changed files with 48 additions and 0 deletions

5
api.py
View file

@ -25,3 +25,8 @@ for m_path in Path("routers").glob("*"):
@api.get("/")
def quack():
return api_data
if __name__ == "__main__":
import uvicorn
api_data["version"] += "-dev"
uvicorn.run(api)

View file

@ -29,6 +29,12 @@ handlers=console,file
qualname=security
propagate=0
[logger_gitea]
level=DEBUG
handlers=console,file
qualname=security
propagate=0
[handler_console]
class=StreamHandler
level=DEBUG

37
routers/gitea.py Normal file
View file

@ -0,0 +1,37 @@
import logging
from typing import Optional
import giteapy
import json
from pathlib import Path
from fastapi import APIRouter, Header, Query, HTTPException
from gitea.gitea import Repository
ROOT_PATH = Path.home().joinpath(".radical_api/gitea")
logger = logging.getLogger("gitea")
router = APIRouter()
@router.get("/gitea/latest")
def get_latest_version(base_url: Optional[str] = Header(None), author: str = Query(None), repo: str = Query(None), pre_releases: bool = Query(False)):
with ROOT_PATH.joinpath("keys.json").open("r") as fp:
data: dict = json.load(fp)
if base_url in data.keys():
config = giteapy.Configuration()
config.api_key['access_token'] = data[base_url]
config.host = base_url + "/api/v1"
api = giteapy.RepositoryApi(giteapy.ApiClient(config))
response = api.repo_list_releases(author, repo)
if len(response) == 0:
raise HTTPException(404, "No releases for given repository.")
for release in response:
release: giteapy.Release
if pre_releases:
return release.name
elif not pre_releases and release.prerelease:
continue
elif not pre_releases and not release.prerelease:
return release.name
else:
raise HTTPException(400, "API cannot access this Gitea instance.")