Example #1
0
def run(api_endpoint, api_port, **kwargs):
    exclude_kwargs = {'close_channels'}
    kwargs_client = {
        key: value
        for key, value in kwargs.items() if value and key not in exclude_kwargs
    }
    with Client(**kwargs_client) as client:
        m2mclient = Session(client, api_endpoint, api_port)
        resource = m2mclient.run('doggo.jpg')
        log.info('Response: {}'.format(resource))

        if kwargs['close_channels'] is True:
            for channel in client.channels:
                if channel.state == Channel.State.open:
                    channel.close_channel()
Example #2
0
def run(key_path, key_password_path, resource):
    # create the client
    with Client(key_path=key_path,
                key_password_path=key_password_path) as client:
        m2mclient = Session(client, 'localhost', 5000)

        # Get the resource. If payment is required, client will attempt to create
        # a channel or will use existing one.
        status, headers, body = m2mclient.run(resource)
        if status == requests.codes.OK:
            if re.match('^text\/', headers['Content-Type']):
                logging.info(
                    "got the resource %s type=%s\n%s" %
                    (resource, headers.get('Content-Type', '???'), body))
            else:
                logging.info("got the resource %s type=%s" %
                             (resource, headers.get('Content-Type', '???')))
        else:
            logging.error("error getting the resource. code=%d body=%s" %
                          (status, body.decode().strip()))