-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #93 from dbutenhof/horreum
Add a Horreum passthrough API
- Loading branch information
Showing
10 changed files
with
226 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
# | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from http import HTTPStatus | ||
from typing import Annotated, Any | ||
|
||
from fastapi.responses import JSONResponse | ||
|
||
from app.services.horreum_svc import HorreumService | ||
from fastapi import APIRouter, Path, Request, Response | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/api/v1/horreum/api/{path:path}") | ||
async def horreum( | ||
request: Request, path: Annotated[str, Path(title="Horreum API path")] | ||
) -> Response: | ||
"""Pass GET API call to Horreum | ||
Makes an authenticated Horreum call using the configured Horreum URL, | ||
username, and password. It passes on the query parameters as well as the | ||
Horreum API path, and returns the status, content, and response headers | ||
to the caller. | ||
Args: | ||
request: Tells FastAPI to show us the full request object | ||
path: A Horreum API path, like /api/test/byName/name | ||
""" | ||
response = HorreumService("horreum").get(path, dict(request.query_params.items())) | ||
return Response( | ||
status_code=response.status_code, | ||
headers=response.headers, | ||
content=response.content, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import sys | ||
from typing import Optional | ||
|
||
import requests | ||
from app import config | ||
from keycloak.keycloak_openid import KeycloakOpenID | ||
|
||
|
||
class HorreumService: | ||
|
||
def __init__(self, configpath="horreum"): | ||
self.cfg = config.get_config() | ||
self.user = self.cfg.get(configpath + ".username") | ||
self.password = self.cfg.get(configpath + ".password") | ||
self.url = self.cfg.get(configpath + ".url") | ||
try: | ||
kc = requests.get(f"{self.url}/api/config/keycloak") | ||
kc.raise_for_status() | ||
except Exception as e: | ||
print(f"Failed {str(e)!r}", file=sys.stderr) | ||
raise | ||
keycloak = kc.json() | ||
self.keycloak = KeycloakOpenID( | ||
keycloak["url"], | ||
client_id=keycloak["clientId"], | ||
realm_name=keycloak["realm"], | ||
) | ||
|
||
def get( | ||
self, path: str, queries: Optional[dict[str, str]] = None | ||
) -> requests.Response: | ||
token = self.keycloak.token(username=self.user, password=self.password)[ | ||
"access_token" | ||
] | ||
return requests.get( | ||
f"{self.url}/api/{path}", | ||
params=queries, | ||
headers={"authorization": f"Bearer {token}"}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.