Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Consider using release 3.0 instead.
Reason given by maintainers: was for testing only

lexrpc Circle CI Coverage Status

Python implementation of AT Protocol's XRPC + Lexicon. lexrpc includes a simple XRPC client, server, and Flask web server integration. All three include full Lexicon support for validating inputs, outputs, and parameters against their schemas.

License: This project is placed in the public domain.

Client

The lexrpc client let you call methods dynamically by their NSIDs. To make a call, first instantiate a Client object with the server address and method lexicons, then use method NSIDs to make calls, passing input as a dict and parameters as kwargs:

from lexrpc import Client

lexicons = [...]
client = Client('https://xrpc.example.com', lexicons)
output = client.com.example.my_query({'foo': 'bar'}, param_a=5)

Note that - characters in method NSIDs are converted to _s, eg the call above is for the method com.example.my-query.

Server

To implement an XRPC server, use the Server class. It validates parameters, inputs, and outputs. Use the method decorator to register method handlers and call to call them, whether from your web framework or anywhere else.

from lexrpc import Server

lexicons = [...]
server = Server(lexicons)

@server.method('com.example.my-query')
def my_query_hander(input, **params):
    output = {'foo': input['foo'], 'b': params['param_a'] + 1}
    return output

# Extract nsid and decode query parameters from an HTTP request,
# call the method, return the output in an HTTP response
nsid = request.path.removeprefix('/xrpc/')
input = request.json()
params = server.decode_params(nsid, request.query_params())
output = server.call(input, **params)
response.write_json(output)

Flask server

To serve XRPC methods in a Flask web app, first install the lexrpc package with the flask extra, eg pip install lexrpc[flask]. Then, instantiate a Server and register method handlers as described above. Finally, attach the server to your Flask app with flask_server.init_flask.

from flask import Flask
from lexrpc.flask_server import init_flask

# instantiate a Server like above
server = ...

app = Flask('my-server')
init_flask(server, app)

This configures the Flask app to serve the methods registered with the lexrpc server as per the spec. Each method is served at the path /xrpc/[NSID], procedures via POSTs and queries via GETs. Parameters are decoded from query parameters, input is taken from the JSON HTTP request body, and output is returned in the JSON HTTP response body. The Content-Type response header is set to application/json.

TODO

Release instructions

Here's how to package, test, and ship a new release. (Note that this is largely duplicated in the oauth-dropins readme too.)

  1. Run the unit tests.
    source local/bin/activate.csh
    python3 -m unittest discover
    
  2. Bump the version number in setup.py and docs/conf.py. git grep the old version number to make sure it only appears in the changelog. Change the current changelog entry in README.md for this new version from unreleased to the current date.
  3. Build the docs. If you added any new modules, add them to the appropriate file(s) in docs/source/. Then run ./docs/build.sh. Check that the generated HTML looks fine by opening docs/_build/html/index.html and looking around.
  4. git commit -am 'release vX.Y'
  5. Upload to test.pypi.org for testing.
    python3 -m build
    setenv ver X.Y
    twine upload -r pypitest dist/lexrpc-$ver.tar.gz
    
  6. Install from test.pypi.org.
    cd /tmp
    python3 -m venv local
    source local/bin/activate.csh
    pip3 uninstall lexrpc # make sure we force pip to use the uploaded version
    pip3 install --upgrade pip
    pip3 install -i https://test.pypi.org/simple --extra-index-url https://pypi.org/simple lexrpc==$ver
    deactivate
    
  7. Smoke test that the code trivially loads and runs.
    source local/bin/activate.csh
    python3
    # run test code below
    deactivate
    
    Test code to paste into the interpreter:
    from lexrpc import Server
    
    server = Server([{
        'lexicon': 1,
        'id': 'io.example.ping',
        'defs': {
            'main': {
                'type': 'query',
                'description': 'Ping the server',
                'parameters': {'message': { 'type': 'string' }},
                'output': {
                    'encoding': 'application/json',
                    'schema': {
                        'type': 'object',
                        'required': ['message'],
                        'properties': {'message': { 'type': 'string' }},
                    },
                },
            },
        },
    }])
    
    @server.method('io.example.ping')
    def ping(input, message=''):
        return {'message': message}
    
    print(server.call('io.example.ping', message='hello world')
    
  8. Tag the release in git. In the tag message editor, delete the generated comments at bottom, leave the first line blank (to omit the release "title" in github), put ### Notable changes on the second line, then copy and paste this version's changelog contents below it.
    git tag -a v$ver --cleanup=verbatim
    git push && git push --tags
    
  9. Click here to draft a new release on GitHub. Enter vX.Y in the Tag version box. Leave Release title empty. Copy ### Notable changes and the changelog contents into the description text box.
  10. Upload to pypi.org!
    twine upload dist/lexrpc-$ver.tar.gz
    
  11. Wait for the docs to build on Read the Docs, then check that they look ok.
  12. On the Versions page, check that the new version is active, If it's not, activate it in the Activate a Version section.

Changelog

0.1 - 2022-12-13

Initial release!

Tested interoperability with the lexicon, xprc, and xrpc-server packages in bluesky-social/atproto. Lexicon and XRPC are still very early and under active development; caveat hacker!

Metadata

Release files for lexrpc 0.1a0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lexrpc 0.1a0
File Size Uploaded Provenance Actions
lexrpc-0.1a0.tar.gz 11.5 kB Without provenance Details for lexrpc-0.1a0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lexrpc 0.1a0
File Interpreter ABI Platform Actions
lexrpc-0.1a0-py3-none-any.whl Python 3 none any Details for lexrpc-0.1a0-py3-none-any.whl

Total release size: 21.3 kB

Release files / lexrpc-0.1a0.tar.gz

Download URL lexrpc-0.1a0.tar.gz
Size 11.5 kB
Tags Source
SHA-256 checksum
How to use checksums
fa10b4926a1a9e40920a7269360336b549550fb5cf464cd4633d74d0442f3abb
BLAKE2b-256 checksum
How to use checksums
01d8e36e6168a390d2aa870bdb21655ddaeba5b0ab0377922013861333bd6839
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.8.1 requests/2.27.1 setuptools/65.4.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.15

Release files / lexrpc-0.1a0-py3-none-any.whl

Download URL lexrpc-0.1a0-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ee15cf5b19e9d0de0d54287d67ad4b4d904fb69c52503de915c490f35e9297a
BLAKE2b-256 checksum
How to use checksums
507f981a1d1f8e5bfcb15750bf52a5be7520446bc4cf7ac9eee1c3644751f3e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.8.1 requests/2.27.1 setuptools/65.4.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.15

Security for lexrpc 0.1a0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page