Skip to content

Commit f125a18

Browse files
committed
A bit of prose docs on retrieving resources.
1 parent 2e62ad3 commit f125a18

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ def setup(app):
105105
"gh": (str(HOMEPAGE) + "/%s", None),
106106
"github": (str(GITHUB) + "/%s", None),
107107
"hatch": ("https://hatch.pypa.io/latest/%s", None),
108+
"httpx": ("https://www.python-httpx.org/%s", None),
108109
}
109110

110111
# -- Options for the linkcheck builder ---------------------------------------

docs/external-retrieval.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
==================
2+
External Retrieval
3+
==================
4+
5+
`Registry` objects represent collections of in-memory resources (e.g. in the case of JSON Schema registries, they represent collections of in-memory JSON Schemas).
6+
7+
Occasionally one wishes to dynamically fetch resources from some other location.
8+
We'll refer to resources not present in-memory as "external resources"
9+
10+
The JSON Schema specifications generally discourage implementations from automatically retrieving network resources [#]_, but if you are in a situation where you wish to do so, or if you wish alternatively to load resources from a database, or from the filesystem, you can do so via the ``retrieve`` argument to `Registry` objects.
11+
12+
Here's an example of how to automatically retrieve external references by downloading them from their URI via :httpx:`httpx </>`, shown by automatically retrieving one of the JSON Schema metaschemas from the network:
13+
14+
.. code:: python
15+
16+
from referencing import Registry, Resource
17+
import httpx
18+
19+
20+
def retrieve_via_httpx(uri):
21+
response = httpx.get(uri)
22+
return Resource.from_contents(response.json())
23+
24+
25+
registry = Registry(retrieve=retrieve_via_httpx)
26+
resolver = registry.resolver()
27+
print(resolver.lookup("https://json-schema.org/draft/2020-12/schema"))
28+
29+
.. [#] Often for good security reasons. See :kw:`schema-references`.

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ Contents
88
:glob:
99
:maxdepth: 2
1010

11+
external-retrieval
1112
schema-packages
1213
api

docs/spelling_wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
autodetecting
22
deserialized
33
discoverability
4+
filesystem
5+
metaschemas
46
referenceable
57
resolvers
68
runtime

0 commit comments

Comments
 (0)