generated from fastai/nbdev_template
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
228 additions
and
271 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 |
---|---|---|
@@ -0,0 +1,144 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#default_exp build_lib" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#export\n", | ||
"from fastcore.utils import *\n", | ||
"from fastcore.foundation import *\n", | ||
"import pprint" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Parse Open API Spec\n", | ||
"\n", | ||
"This library leverages the [OpenAPI Specification](https://github.com/OAI/OpenAPI-Specification) to create a python client for the GitHub API. The OpenAPI specification contains metadata on all of the endpoints and how to access them properly. Using this metadata, we can construct a python client dynamically that updates automatically along with the OpenAPI Spec. " | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#export\n", | ||
"GH_OPENAPI_URL = 'https://github.com/github/rest-api-description/raw/main/descriptions/api.github.com/api.github.com.json?raw=true'\n", | ||
"_DOC_URL = 'https://docs.github.com/'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#export\n", | ||
"def build_funcs(pre, nm='ghapi/metadata.py', url=GH_OPENAPI_URL, docurl=_DOC_URL):\n", | ||
" \"Build module funcs.py from an Open API spec and optionally filter by a path `pre`\"\n", | ||
" def _get_detls(o):\n", | ||
" data = nested_idx(o, *'requestBody content application/json schema properties'.split()) or {}\n", | ||
" url = o['externalDocs']['url'][len(docurl):]\n", | ||
" return (o['operationId'], o['summary'], url, list(data.keys()))\n", | ||
" \n", | ||
" js = urljson(url)\n", | ||
" paths = {o[len(pre):]:v for o,v in js['paths'].items() if o.startswith(pre)}\n", | ||
" _funcs = [(path, verb) + _get_detls(detls)\n", | ||
" for path,verbs in paths.items() for verb,detls in verbs.items()]\n", | ||
" Path(nm).write_text(\"funcs = \" + pprint.pformat(_funcs, width=360))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"We build the module `metadata.py` with the github OpenAPI spec for only paths beginning with `/repos/{owner}/{repo}`:\n", | ||
"\n", | ||
"```python\n", | ||
"build_funcs(pre='/repos/{owner}/{repo}', nm='ghapi/metadata.py')\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"This python module contains a list of metadata for each endpoint, containing the path, verb, operation id, summary, documentation relative URL, and list of parameters (if any), e.g:" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"('/actions/artifacts/{artifact_id}',\n", | ||
" 'delete',\n", | ||
" 'actions/delete-artifact',\n", | ||
" 'Delete an artifact',\n", | ||
" 'rest/reference/actions#delete-an-artifact',\n", | ||
" [])" | ||
] | ||
}, | ||
"execution_count": null, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from ghapi.metadata import funcs\n", | ||
"funcs[5]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Export -" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"#hide\n", | ||
"from nbdev.export import notebook2script\n", | ||
"notebook2script('build_lib.ipynb')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.