Skip to content

Commit 925ed18

Browse files
committed
Merge branch 'env_var_#95' into develop
2 parents f02d018 + 6009b30 commit 925ed18

3 files changed

Lines changed: 98 additions & 37 deletions

File tree

README.md

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'# OSD status dashboard _(wp2.2_dev)_
1+
# OSD status dashboard _(wp2.2_dev)_
22

33
[![Demo backend API](https://img.shields.io/badge/Demo-CLICK%20HERE-red.svg?style=flat)](https://wp22dev.herokuapp.com/)
44
[![Python version](https://img.shields.io/badge/Python-3.10-blue.svg?style=flat&logo=Python&logoColor=white)](https://docs.python.org/3.8/)
@@ -17,19 +17,21 @@ Targeted at hosters of version control platforms (such as [Wikifactory](https://
1717
**Please see the [Install](#install) and [Usage](#usage) sections to get up and running with this tool**. For more details on its background and design considerations, please see the [Background](#background), ~~[Design notes](#design-notes), and [Future work](#future-work) sections. There is also a detailed [step-by-step walkthrough](docs/usage-example.md).~~
1818
## Table of Contents
1919

20-
- [Table of Contents](#table-of-contents)
21-
- [Background](#background)
22-
- [Install](#install)
23-
- [Running from source](#running-from-source)
24-
- [Deploy as container](#deploy-as-container)
25-
- [Usage](#usage)
26-
- [Making requests to the REST API](#making-requests-to-the-rest-api)
27-
- [API response format](#api-response-format)
28-
- [Design notes](#design-notes)
29-
- [Maintainers](#maintainers)
30-
- [Contributing](#contributing)
31-
- [Acknowledgements](#acknowledgements)
32-
- [License](#license)
20+
- [OSD status dashboard _(wp2.2_dev)_](#osd-status-dashboard-wp22_dev)
21+
- [Table of Contents](#table-of-contents)
22+
- [Background](#background)
23+
- [Install](#install)
24+
- [Running from source](#running-from-source)
25+
- [Deploy as container](#deploy-as-container)
26+
- [Usage](#usage)
27+
- [Making requests to the REST API](#making-requests-to-the-rest-api)
28+
- [API response format](#api-response-format)
29+
- [Custom Wikifactory URLs](#custom-wikifactory-urls)
30+
- [Design notes](#design-notes)
31+
- [Maintainers](#maintainers)
32+
- [Contributing](#contributing)
33+
- [Acknowledgements](#acknowledgements)
34+
- [License](#license)
3335

3436
## Background
3537

@@ -259,6 +261,18 @@ Notes:
259261
* For `files_info` above, filetypes are identified by file extensions. The categories and mapping are located in [`oshminer/filetypes.py`](./oshminer/filetypes.py).
260262
* The `license` information and formatting is largely based on that from the GitHub-managed [choosealicense.com repository](https://github.com/github/choosealicense.com), with the exception of some open source hardware licenses which were manually added.
261263

264+
### Custom Wikifactory URLs
265+
266+
By default, this tool will:
267+
268+
1. Identify whether a provided repository URL in the JSON request body as a Wikifactory project if it is under the domain `wikifactory.com`
269+
2. Use the public Wikifactory GraphQL API endpoint at `https://wikifactory.com/api/graphql`
270+
271+
Both can be customised with the following environmental variables:
272+
273+
1. `WIF_BASE_URL` - (default: `wikifactory.com`) The base domain used for pattern-matching and identifying Wikifactory project URLs in the JSON request body in the form of `example.com`. If this is customised, then the requested Wikifactory project URLs passed to this tool should also use that domain instead of `wikifactory.com`. Otherwise, an "Repository URL domain not supported" error will be returned.
274+
2. `WIF_API_URL` - (default: `https://wikifactory.com/api/graphql`) The full URL of the GraphQL API endpoint to make queries regarding Wikifactory projects in the form of `https://example.com[:port]/foo/bar`.
275+
262276
## Design notes
263277

264278
[to be updated]

oshminer/Wikifactory.py

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,28 @@
66
# Python Standard Library imports
77
from datetime import datetime
88
import json
9-
import urllib.parse
9+
import os
10+
import urllib
1011
import sys
1112

1213
# External imports
14+
from fastapi import status
15+
from fastapi.responses import JSONResponse
1316
from gql import gql, Client
1417
from gql.transport.aiohttp import AIOHTTPTransport
1518

1619
# Internal imports
1720
import oshminer.filetypes as filetypes
1821
from oshminer.errors import exceptions
1922

23+
# Set Wikifactory API URL
24+
# Looks for the `WIF_API_URL` environment variable, and if not found, default to:
25+
# https://wikifactory.com/api/graphql
26+
# See:
27+
# https://www.twilio.com/blog/environment-variables-python
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)
30+
2031
async def get_files_info(project: dict, session) -> dict:
2132
# Provide a GraphQL query
2233
query = gql(
@@ -405,28 +416,51 @@ def parse_url(url: str) -> dict:
405416
}
406417
return repo
407418

408-
async def make_Wikifactory_request(url: str, data: list, api_url: str = "https://wikifactory.com/api/graphql") -> str:
409-
print(
410-
f"Constructing and making an API request to Wikifactory for repository {url} for the following data {data}",
411-
file = sys.stderr
412-
)
419+
async def make_Wikifactory_request(url: str, data: list) -> str:
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
413435

414-
# Create a dictionary to hold results from Wikifactory API query
415-
results: dict = {
416-
"repository": str(url),
417-
"platform": "Wikifactory",
418-
"requested_data": {}
419-
}
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+
}
420448

421-
# Select transport with the Wikifactory API endpoint URL
422-
transport = AIOHTTPTransport(url = api_url)
449+
# Select transport with the Wikifactory API endpoint URL
450+
transport = AIOHTTPTransport(url = WIF_API_URL)
423451

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

427-
async with Client(transport = transport, fetch_schema_from_transport = True) as session:
428-
for data_type in data:
429-
query_result: dict = await queries[data_type](space_slug, session)
430-
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)
431459

432-
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+
)

oshminer/supported_platforms.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@
33
# SPDX-FileCopyrightText: 2022 Pen-Yuan Hsing
44
# SPDX-License-Identifier: AGPL-3.0-or-later
55

6+
# Python Standard Library imports
7+
import os
8+
import sys
9+
10+
# Internal imports
611
import oshminer.GitHub
712
import oshminer.Wikifactory
813

14+
# Set Wikifactory base URL for projects
15+
# Looks for the `WIF_BASE_URL` environment variable, and if not found, default to:
16+
# `wikifactory.com`
17+
# See:
18+
# https://www.twilio.com/blog/environment-variables-python
19+
WIF_BASE_URL: str = os.environ.get("WIF_BASE_URL", "wikifactory.com")
20+
print(f"Wikifactory base URL: {WIF_BASE_URL}", file=sys.stderr)
21+
922
# Supported hosting platforms as a dictionary of their domains, which are
1023
# matched to names of internal functions that construct API requests to the
1124
# associated platforms.
12-
# `repo_urls` need to include one of these domains.
25+
# `repo_urls` in the request body needs to include one of these domains.
1326
supported_domains: dict = {
1427
"github.com": oshminer.GitHub.make_GitHub_request,
15-
"wikifactory.com": oshminer.Wikifactory.make_Wikifactory_request
28+
WIF_BASE_URL: oshminer.Wikifactory.make_Wikifactory_request
1629
}

0 commit comments

Comments
 (0)