Skip to main content

A library to serialize and deserialize Thrift values.

Project description

build coverage docs

thriftrw is a Python library to serialize and deserialize Thrift types.

Documentation is available on Read The Docs.

Features

  • No code generation. The .thrift files are parsed and compiled in-memory at runtime.

  • No assumptions about how bytes are sent or received. The library concerns itself with serialization and deserialization only.

  • Supports Python 2 and 3.

  • Forward and cyclic references in types.

Example

Given the .thrift file,:

// blog.thrift

typedef string PostUUID

typedef binary RichText

union Body {
    1: string plainText
    2: RichText richText
}

struct PostDetails {
    1: required string author
    2: required string subject
    3: required Body body
}

exception UnauthorizedRequestError {
}

service BlogService {
    PostUUID newPost(1: PostDetails post)
        throws (1: UnauthorizedRequestError unauthorized);
}

You can use the library to send and receive requests and responses like so,

# client.py

import thriftrw

blog = thriftrw.load('blog.thrift')
BlogService = blog.BlogService

def new_post():
    post = blog.PostDetails(
        author='...',
        subject='...',
        body=blog.Body(plainText='Hello, world!')
    )

    request = BlogService.newPost.request(post)
    payload = blog.dumps(request)

    # send_to_server is implemented by the user.
    response_payload = send_to_server(payload)
    response = blog.loads(BlogService.newPost.response, response_payload)
    if response.unauthorized is not None:
        raise response.unauthorized
    else:
        return response.success
# server.py

import thriftrw

blog = thriftrw.load('blog.thrift')
BlogService = blog.BlogService

# The user's server handler calls handle_new_post with the payload.
def handle_new_post(request_payload):
    request = blog.loads(BlogService.newPost.request, request_payload)
    if request.post.author != 'admin':
        response = BlogService.newPost.response(
            unauthorized=blog.UnauthorizedRequestError()
        )
    else:
        # create_post is implemented by the user.
        post_uuid = create_post(request.post)
        response = BlogService.newPost.response(success=post_uuid)

    return blog.dumps(response)

Caveats

  • Only the Thrift Binary protocol is supported at this time.

  • Message wrappers for Thrift calls and responses are not supported at this time.

License

Copyright (c) 2015 Uber Technologies, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

thriftrw-0.3.3.tar.gz (33.9 kB view details)

Uploaded Source

Built Distribution

thriftrw-0.3.3-py2-none-any.whl (72.8 kB view details)

Uploaded Python 2

File details

Details for the file thriftrw-0.3.3.tar.gz.

File metadata

  • Download URL: thriftrw-0.3.3.tar.gz
  • Upload date:
  • Size: 33.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for thriftrw-0.3.3.tar.gz
Algorithm Hash digest
SHA256 201a35aaac2676b3f6dcde3cb33b51e8f342984f2bfee5b855fd25632201b7b7
MD5 fbc573335b0cf649cd1756b4a3cafc59
BLAKE2b-256 db2aea256caec9f9951b019c4bb24e40d18d47b55faa29e02c2cf5a2b02b1908

See more details on using hashes here.

Provenance

File details

Details for the file thriftrw-0.3.3-py2-none-any.whl.

File metadata

File hashes

Hashes for thriftrw-0.3.3-py2-none-any.whl
Algorithm Hash digest
SHA256 0d5fd1894af7c915cea95f86b5d4ef16be5daa69db52b7568e42071728789ff7
MD5 484a974b52acbcff86e8808a01c5469a
BLAKE2b-256 10dccf317ee120988b50e0ead971804c2092787a44e8121c396c7e701655a6bc

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page