Skip to content

Commit 90dff20

Browse files
committed
Add error handling for custom Wikifactory API URL
It's a set of try/except clauses that are a bit ugly, but seems to work.
1 parent 6e18282 commit 90dff20

1 file changed

Lines changed: 47 additions & 22 deletions

File tree

oshminer/Wikifactory.py

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
from datetime import datetime
88
import json
99
import os
10-
import urllib.parse
10+
import urllib
1111
import sys
1212

1313
# External imports
14+
from fastapi import status
15+
from fastapi.responses import JSONResponse
1416
from gql import gql, Client
1517
from gql.transport.aiohttp import AIOHTTPTransport
1618

@@ -23,8 +25,8 @@
2325
# https://wikifactory.com/api/graphql
2426
# See:
2527
# https://www.twilio.com/blog/environment-variables-python
26-
WIF_API_URL: str = os.environ.get("WIF_API_URL", "https://wikifactory.com/api/graphql")
27-
# print(f"Wikifactory API URL: {WIF_API_URL}", file=sys.stderr)
28+
WIF_API_URL_DEFAULT: str = "https://wikifactory.com/api/graphql"
29+
WIF_API_URL: str = os.environ.get("WIF_API_URL", WIF_API_URL_DEFAULT)
2830

2931
async def get_files_info(project: dict, session) -> dict:
3032
# Provide a GraphQL query
@@ -415,27 +417,50 @@ def parse_url(url: str) -> dict:
415417
return repo
416418

417419
async def make_Wikifactory_request(url: str, data: list) -> str:
418-
print(
419-
f"Constructing and making an API request to Wikifactory for repository {url} for the following data {data}",
420-
file = sys.stderr
421-
)
420+
try:
421+
# First, check if there is a custom Wikifactory API URL and if it works
422+
if WIF_API_URL != WIF_API_URL_DEFAULT:
423+
print(
424+
f"Checking custom Wikifactory API URL: {WIF_API_URL}",
425+
file = sys.stderr
426+
)
427+
try:
428+
api_url_response = urllib.request.urlopen(
429+
WIF_API_URL,
430+
timeout=5
431+
)
432+
except (urllib.error.URLError):
433+
print(f"Unable to reach Wikifactory API at: {WIF_API_URL}", file=sys.stderr)
434+
raise exceptions.BadWIFAPIError
422435

423-
# Create a dictionary to hold results from Wikifactory API query
424-
results: dict = {
425-
"repository": str(url),
426-
"platform": "Wikifactory",
427-
"requested_data": {}
428-
}
436+
print(
437+
f"Constructing and making an API request to Wikifactory for repository {url} for the following data {data}",
438+
file = sys.stderr
439+
)
440+
441+
# If the Wikifactory API is reacheable as tested above, then:
442+
# Create a dictionary to hold results from Wikifactory API query
443+
results: dict = {
444+
"repository": str(url),
445+
"platform": "Wikifactory",
446+
"requested_data": {}
447+
}
429448

430-
# Select transport with the Wikifactory API endpoint URL
431-
transport = AIOHTTPTransport(url = WIF_API_URL)
449+
# Select transport with the Wikifactory API endpoint URL
450+
transport = AIOHTTPTransport(url = WIF_API_URL)
432451

433-
# Get "space" and "slug" components from this repository's URL
434-
space_slug: dict = parse_url(url)
452+
# Get "space" and "slug" components from this repository's URL
453+
space_slug: dict = parse_url(url)
435454

436-
async with Client(transport = transport, fetch_schema_from_transport = True) as session:
437-
for data_type in data:
438-
query_result: dict = await queries[data_type](space_slug, session)
439-
results["requested_data"].update(query_result)
455+
async with Client(transport = transport, fetch_schema_from_transport = True) as session:
456+
for data_type in data:
457+
query_result: dict = await queries[data_type](space_slug, session)
458+
results["requested_data"].update(query_result)
440459

441-
return results
460+
return results
461+
462+
except (exceptions.BadWIFAPIError) as err:
463+
return JSONResponse(
464+
status_code = status.HTTP_400_BAD_REQUEST,
465+
content = f"Error reaching Wikifactory API URL: {WIF_API_URL} {err}"
466+
)

0 commit comments

Comments
 (0)