Skip to content

Commit 77dded1

Browse files
committed
Minor simplification to the docs structure.
Fold retrieve documentation into the intro.
1 parent 029a8b5 commit 77dded1

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

docs/changes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
v0.27.4
6+
-------
7+
8+
* Minor simplification to the docs structure.
9+
510
v0.27.3
611
-------
712

docs/external-retrieval.rst

Lines changed: 0 additions & 29 deletions
This file was deleted.

docs/index.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ In other words, a way for e.g. JSON Schema tooling to resolve the ``$ref`` keywo
77
:hidden:
88

99
intro
10-
external-retrieval
1110
schema-packages
11+
compatibility
1212
api
1313
changes
14-
compatibility

docs/intro.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ You could also confirm your resource is in the registry if you'd like, via `refe
5353

5454
{'type': 'integer'}
5555

56+
5657
Populating Registries
5758
---------------------
5859

@@ -202,3 +203,36 @@ which now allows us to use the URI we associated with our third resource to retr
202203
If you have more than one resource to add, you can use `Registry.with_resources` (with an ``s``) to add many at once, or, if they meet the criteria to use ``@``, you can use ``[one, two, three] @ registry`` to add all three resources at once.
203204

204205
You may also want to have a look at `Registry.with_contents` for a further method to add resources to a registry without constructing a `Resource` object yourself.
206+
207+
208+
Dynamically Retrieving Resources
209+
--------------------------------
210+
211+
Sometimes one wishes to dynamically retrieve or construct `Resource`\ s which *don't* already live in-memory within a `Registry`.
212+
This might be resources retrieved dynamically from a database, from files somewhere on disk, from some arbitrary place over the internet, or from the like.
213+
We'll refer to such resources not present in-memory as *external resources*.
214+
215+
The ``retrieve`` argument to ``Registry`` objects can be used to configure a callable which will be used anytime a requested URI is *not* present in the registry, thereby allowing you to retrieve it from whichever location it lives in.
216+
Here's an example of automatically retrieving external references by downloading them via :httpx:`httpx </>`, illustrated by then automatically retrieving one of the JSON Schema metaschemas from the network:
217+
218+
.. code:: python
219+
220+
from referencing import Registry, Resource
221+
import httpx
222+
223+
224+
def retrieve_via_httpx(uri):
225+
response = httpx.get(uri)
226+
return Resource.from_contents(response.json())
227+
228+
229+
registry = Registry(retrieve=retrieve_via_httpx)
230+
resolver = registry.resolver()
231+
print(resolver.lookup("https://json-schema.org/draft/2020-12/schema"))
232+
233+
.. note::
234+
235+
In the case of JSON Schema, the specifications generally discourage implementations from automatically retrieving these sorts of external resources over the network due to potential security implications.
236+
See :kw:`schema-references` in particular.
237+
238+
`referencing` will of course therefore not do any such thing automatically, and this section generally assumes that you have personally considered the security implications for your own use case.

0 commit comments

Comments
 (0)