Giter VIP home page Giter VIP logo

httputil's Introduction

httputil

Utilities for dealing with HTTP data

Example httputil.read_body_stream() usage:

    import httputil

    with open(http_file_path, 'rb') as fh:
        print(b''.join(httputil.read_body_stream(
            fh, chunked=True, compression=httputil.GZIP))

Example request engines use to implement API clients:

    
    from httputil.request_engines import async
    from httputil.request_engines import sync
    from httputil.request_engines import errors
    
    
    DEF_REQUEST_TIMEOUT = 10
    DEF_CONNECT_TIMEOUT = 3
    DEF_NUM_RETRIES = 3
    
    
    log = logging.getLogger('http_client')
    
    API_BASE_URL = 'http://echo.jsontest.com'
    ECHO_URL = '/key/value/one/two'
    HEADERS = {'Content-Type': 'application/json'}
    
    
    class SyncAPIClient(object):
    
        def __init__(self, connect_timeout=DEF_CONNECT_TIMEOUT,
                     request_timeout=DEF_REQUEST_TIMEOUT,
                     connection_retries=DEF_NUM_RETRIES):
            """Constructor.
            """
    
            self._engine = sync.SyncRequestEngine(
                API_BASE_URL, connect_timeout, request_timeout, connection_retries)
    
        def echo(self):
    
            return self._engine.request(ECHO_URL, headers=HEADERS,
                                        result_callback=lambda res: json.loads(res))
    
    
    class AsyncAPIClient(object):
    
        def __init__(self, connect_timeout=DEF_CONNECT_TIMEOUT,
                     request_timeout=DEF_REQUEST_TIMEOUT,
                     connection_retries=DEF_NUM_RETRIES):
            """Constructor.
            """
    
            self._engine = async.AsyncRequestEngine(
                API_BASE_URL, connect_timeout, request_timeout, connection_retries)
    
        def echo(self):
    
            return self._engine.request(ECHO_URL, headers=HEADERS,
                                        result_callback=lambda res: json.loads(res))
    
    
    @gen.coroutine
    def example_async_client(api_client):
        """Example async client.
        """
    
        try:
            pprint((yield from api_client.echo()))
        except errors.RequestError as exc:
            log.exception('Exception occurred: %s', exc)
    
        yield gen.Task(lambda *args, **kwargs: ioloop.IOLoop.current().stop())
    
    
    def example_sync_client(api_client):
        """Example sync client use with.
        """
    
        try:
            pprint(api_client.echo())
        except errors.RequestError as exc:
            log.exception('Exception occurred: %s', exc)
    
    
    def main():
        """Run the examples.
        """
    
        logging.basicConfig(level=logging.INFO)
    
        example_sync_client(SyncAPIClient())
        example_async_client(AsyncAPIClient())
    
        io_loop = ioloop.IOLoop.current()
        io_loop.start()
    
    
    if __name__ == '__main__':
        main()

httputil's People

Contributors

vkuznet-jnpr avatar vovanec avatar

Watchers

James Cloos avatar Joel Seguillon avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.