Python aiohttp.HttpProcessingError() Examples

The following are 20 code examples of aiohttp.HttpProcessingError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module aiohttp , or try the search function .
Example #1
Source File: router_test.py    From aiorest with MIT License 6 votes vote down vote up
def test_dispatch_bad_signature(self):
        def f():
            return {'a': 1, 'b': 2}
        self.server.add_url('get', '/post/{id}', f)

        request = Request('host', aiohttp.RawRequestMessage(
            'GET', '/post/123', '1.1', {}, True, None),
            None, loop=self.loop)

        @asyncio.coroutine
        def go():
            with self.assertRaises(aiohttp.HttpProcessingError) as ctx:
                yield from self.server.dispatch(request)
            self.assertEqual(500, ctx.exception.code)

        self.loop.run_until_complete(go()) 
Example #2
Source File: router_test.py    From aiorest with MIT License 6 votes vote down vote up
def test_dispatch_method_not_allowed(self):
        m = mock.Mock()
        self.server.add_url('post', '/post/{id}', m)
        self.server.add_url('get', '/post/{id}', m)

        @asyncio.coroutine
        def go():
            with self.assertRaises(aiohttp.HttpProcessingError) as ctx:
                request = Request('host', aiohttp.RawRequestMessage(
                    'DELETE', '/post/123', '1.1', {}, True, None),
                    None, loop=self.loop)
                yield from self.server.dispatch(request)
            self.assertEqual(405, ctx.exception.code)
            self.assertEqual((('Allow', 'GET, POST'),), ctx.exception.headers)

        self.assertFalse(m.called)
        self.loop.run_until_complete(go()) 
Example #3
Source File: router_test.py    From aiorest with MIT License 6 votes vote down vote up
def test_dispatch_not_found(self):
        m = mock.Mock()
        self.server.add_url('post', '/post/{id}', m)
        self.server.add_url('get', '/post/{id}', m)

        @asyncio.coroutine
        def go():
            with self.assertRaises(aiohttp.HttpProcessingError) as ctx:
                request = Request('host', aiohttp.RawRequestMessage(
                    'POST', '/not/found', '1.1', {}, True, None),
                    None, loop=self.loop)
                yield from self.server.dispatch(request)
            self.assertEqual(404, ctx.exception.code)

        self.assertFalse(m.called)
        self.loop.run_until_complete(go()) 
Example #4
Source File: flags2_asyncio_executor.py    From notebooks with MIT License 6 votes vote down vote up
def get_flag(base_url, cc):
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    resp = yield from aiohttp.request('GET', url)
    with contextlib.closing(resp):
        if resp.status == 200:
            image = yield from resp.read()
            return image
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers)


# BEGIN FLAGS2_ASYNCIO_EXECUTOR 
Example #5
Source File: flags2_asyncio_executor.py    From example-code with MIT License 6 votes vote down vote up
def get_flag(base_url, cc):
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    resp = yield from aiohttp.request('GET', url)
    with contextlib.closing(resp):
        if resp.status == 200:
            image = yield from resp.read()
            return image
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers)


# BEGIN FLAGS2_ASYNCIO_EXECUTOR 
Example #6
Source File: _tio.py    From RTFMbot with Mozilla Public License 2.0 5 votes vote down vote up
def send(self):
        async with aiohttp.ClientSession() as client_session:
            async with client_session.post(self.backend, data=self.request) as res:
                if res.status != 200:
                    raise aiohttp.HttpProcessingError(res.status)

                data = await res.read()
                data = data.decode('utf-8')
                return data.replace(data[:16], '') # remove token 
Example #7
Source File: router_test.py    From aiorest with MIT License 5 votes vote down vote up
def test_dispatch_with_ending_slash_not_found2(self):
        def f(request):
            return {'a': 1, 'b': 2}
        self.server.add_url('get', '/post/{id}/', f)

        request = Request('host', aiohttp.RawRequestMessage(
            'GET', '/po/123', '1.1', {}, True, None),
            None, loop=self.loop)

        with self.assertRaises(aiohttp.HttpProcessingError) as ctx:
            self.loop.run_until_complete(self.server.dispatch(request))
        self.assertEqual(404, ctx.exception.code) 
Example #8
Source File: router_test.py    From aiorest with MIT License 5 votes vote down vote up
def test_dispatch_with_ending_slash_not_found1(self):
        def f(request):
            return {'a': 1, 'b': 2}
        self.server.add_url('get', '/post/{id}/', f)

        request = Request('host', aiohttp.RawRequestMessage(
            'GET', '/post/123', '1.1', {}, True, None),
            None, loop=self.loop)

        with self.assertRaises(aiohttp.HttpProcessingError) as ctx:
            self.loop.run_until_complete(self.server.dispatch(request))
        self.assertEqual(404, ctx.exception.code) 
Example #9
Source File: flags2_asyncio.py    From example-code with MIT License 5 votes vote down vote up
def get_flag(session, base_url, cc): # <2>
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    async with session.get(url) as resp:
        if resp.status == 200:
            return await resp.read()
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers) 
Example #10
Source File: flags2_await.py    From example-code with MIT License 5 votes vote down vote up
def get_flag(base_url, cc): # <2>
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    with closing(await aiohttp.request('GET', url)) as resp:
        if resp.status == 200:
            image = await resp.read()
            return image
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers) 
Example #11
Source File: flags2_asyncio.py    From example-code with MIT License 5 votes vote down vote up
def get_flag(base_url, cc): # <2>
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    resp = yield from aiohttp.request('GET', url)
    with contextlib.closing(resp):
        if resp.status == 200:
            image = yield from resp.read()
            return image
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers) 
Example #12
Source File: flags2_await.py    From notebooks with MIT License 5 votes vote down vote up
def get_flag(base_url, cc): # <2>
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    with closing(await aiohttp.request('GET', url)) as resp:
        if resp.status == 200:
            image = await resp.read()
            return image
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers) 
Example #13
Source File: flags2_asyncio.py    From notebooks with MIT License 5 votes vote down vote up
def get_flag(base_url, cc): # <2>
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    resp = yield from aiohttp.request('GET', url)
    with contextlib.closing(resp):
        if resp.status == 200:
            image = yield from resp.read()
            return image
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers) 
Example #14
Source File: flags2_await_executor.py    From concurrency2017 with MIT License 5 votes vote down vote up
def get_flag(client, base_url, cc):
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    async with client.get(url) as resp:
        if resp.status == 200:
            return await resp.read()
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers) 
Example #15
Source File: flags2_await.py    From concurrency2017 with MIT License 5 votes vote down vote up
def get_flag(client, base_url, cc):
    url = '{}/{cc}/{cc}.gif'.format(base_url, cc=cc.lower())
    async with client.get(url) as resp:
        if resp.status == 200:
            return await resp.read()
        elif resp.status == 404:
            raise web.HTTPNotFound()
        else:
            raise aiohttp.HttpProcessingError(
                code=resp.status, message=resp.reason,
                headers=resp.headers) 
Example #16
Source File: mkgroups.py    From hsds with Apache License 2.0 5 votes vote down vote up
def verifyDomain(domain):
    """ create domain if it doesn't already exist
    """
    params = {"host": domain}
    headers = getRequestHeaders()
    client = globals["client"]
    req = getEndpoint() + '/'
    root_id = None
    log.info("GET " + req)
    timeout = config.get("timeout")
    async with client.get(req, headers=headers, timeout=timeout, params=params) as rsp:
        if rsp.status == 200:
            domain_json = await rsp.json()
        else:
            log.info("got status: {}".format(rsp.status))
    if rsp.status == 200:
        root_id = domain_json["root"]
    elif rsp.status == 404:
        # create the domain
        setupDomain(domain)
        async with client.get(req, headers=headers, timeout=timeout, params=params) as rsp:
            if rsp.status == 200:
                domain_json = await rsp.json()
                root_id = domain_json["root"]
            else:
                log.error("got status: {} for GET req: {}".format(rsp.status, req))
                raise HttpProcessingError(code=rsp.status, message="Service error")
    globals["root"] = root_id 
Example #17
Source File: import_ghcn_file.py    From hsds with Apache License 2.0 5 votes vote down vote up
def verifyDomain(domain):
    """ create domain if it doesn't already exist
    """
    params = {"host": domain}
    headers = getRequestHeaders()
    client = globals["client"]
    req = getEndpoint() + '/'
    root_id = None
    log.info("GET " + req)
    globals["request_count"] += 1
    timeout = config.get("timeout")
    async with client.get(req, headers=headers, params=params, timeout=timeout) as rsp:
        if rsp.status == 200:
            domain_json = await rsp.json()
        else:
            log.info("got status: {}".format(rsp.status))
    if rsp.status == 200:
        root_id = domain_json["root"]
    elif rsp.status == 404:
        # create the domain
        log.info("PUT " + req)
        globals["request_count"] += 1
        async with client.put(req, headers=headers, params=params, timeout=timeout) as rsp:
            if rsp.status != 201:
                log.error("got status: {} for PUT req: {}".format(rsp.status, req))
                raise HttpProcessingError(code=rsp.status, message="Unexpected error")
        log.info("GET " + req)
        globals["request_count"] += 1
        async with client.get(req, headers=headers, params=params, timeout=timeout) as rsp:
            if rsp.status == 200:
                domain_json = await rsp.json()
                root_id = domain_json["root"]
            else:
                log.error("got status: {} for GET req: {}".format(rsp.status, req))
                raise HttpProcessingError(code=rsp.status, message="Service error")
    globals["root"] = root_id 
Example #18
Source File: import_ghcn_file.py    From hsds with Apache License 2.0 4 votes vote down vote up
def createGroup(parent_group, group_name):
    """ create a new group and link it to the parent group with 
    link name of group name
    """
    client = globals["client"]
    domain = globals["domain"]
    params = {"host": domain}
    base_req = getEndpoint()
    headers = getRequestHeaders()
    timeout = config.get("timeout")

    # TBD - replace with atomic create & link operation?
    
    # create a new group
    req = base_req + "/groups"
    log.info("POST:" + req)
    globals["request_count"] += 1
    async with client.post(req, headers=headers, params=params, timeout=timeout) as rsp:
        if rsp.status != 201:
            log.error("POST {} failed with status: {}, rsp: {}".format(req, rsp.status, str(rsp)))
            raise HttpProcessingError(code=rsp.status, message="Unexpected error")
        group_json = await rsp.json()
        group_id = group_json["id"]

    # link group to parent
    req = base_req + "/groups/" + parent_group + "/links/" + group_name
    data = {"id": group_id }
    link_created = False
    log.info("PUT " + req)
    globals["request_count"] += 1
    async with client.put(req, data=json.dumps(data), headers=headers, params=params, timeout=timeout) as rsp:
        if rsp.status == 409:
            # another task has created this link already
            log.warn("got 409 in request: " + req)
        elif rsp.status != 201:
            log.error("got http error: {} for request: {}, rsp: {}".format(rsp.status, req, rsp))
            raise HttpProcessingError(code=rsp.status, message="Unexpected error")
        else:
            link_created = True

    if not link_created:
        # fetch the existing link and return the group 
        log.info("GET " + req)
        globals["request_count"] += 1
        async with client.get(req, headers=headers, params=params, timeout=timeout) as rsp:
            if rsp.status != 200:
                log.warn("unexpected error (expected to find link) {} for request: {}".format(rsp.status, req))
                raise HttpProcessingError(code=rsp.status, message="Unexpected error")
            else:
                rsp_json = await rsp.json()
                link_json = rsp_json["link"]
                if link_json["class"] != "H5L_TYPE_HARD":
                    raise ValueError("Unexpected Link type: {}".format(link_json))
                group_id = link_json["id"]
    
    return group_id 
Example #19
Source File: mkgroups.py    From hsds with Apache License 2.0 4 votes vote down vote up
def createGroup():
    """ create a new group and link it to the parent group with 
    link name of group name
    """
    client = globals["client"]
    domain = globals["domain"]
    params = {"host": domain}
    base_req = getEndpoint()
    headers = getRequestHeaders()
  
    # create a new group
    req = base_req + "/groups"
    log.info("POST:" + req)
    globals["grp_request_count"] += 1
    group_name = globals["grp_request_count"]
    timeout = config.get("timeout")
    async with client.post(req, headers=headers, timeout=timeout, params=params) as rsp:
        if rsp.status != 201:
            log.error("POST {} failed with status: {}, rsp: {}".format(req, rsp.status, str(rsp)))
            globals["grp_failed_posts"] += 1
            raise HttpProcessingError(code=rsp.status, message="Unexpected error")
        else:
            globals["group_count"] += 1
            log.info("group_count: {}".format(globals["group_count"]))
        group_json = await rsp.json()
        group_id = group_json["id"]

    # link group to parent
    root_id = globals["root"] 
    group_name = "group_{}".format(group_name)  
    req = base_req + "/groups/" + root_id + "/links/" + group_name
    data = {"id": group_id }
    log.info("PUT " + req)
    globals["lnk_request_count"] += 1
    async with client.put(req, data=json.dumps(data), headers=headers, timeout=timeout, params=params) as rsp:
        if rsp.status == 409:
            # another task has created this link already
            log.warn("got 409 in request: " + req)
        elif rsp.status != 201:
            globals["lnk_failed_posts"] += 1
            log.error("got http error: {} for request: {}, rsp: {}".format(rsp.status, req, rsp))
            raise HttpProcessingError(code=rsp.status, message="Unexpected error")
        else:
            link_created = True
    
    return group_id 
Example #20
Source File: import_ghcn_file.py    From hsds with Apache License 2.0 4 votes vote down vote up
def verifyGroupPath(h5path):
    """ create any groups along the path that doesn't exist
    """
    #print("current task: ", asyncio.Task.current_task())
    client = globals["client"]
    domain = globals["domain"]
    h5path_cache = globals["h5path_cache"]
    params = {"host": domain}
    parent_group = h5path_cache['/']  # start with root
    group_names = h5path.split('/')
    
    headers = getRequestHeaders()
    
    base_req = getEndpoint() + '/groups/'
    next_path = '/'
    timeout = config.get("timeout")

    for group_name in group_names:
        if not group_name:
            continue  # skip empty names
        next_path += group_name
        if not next_path.endswith('/'):
            next_path += '/'  # prep for next roundtrips
        if next_path in h5path_cache:
            # we already have the group id
            parent_group = h5path_cache[next_path]    
            continue
        
        req = base_req + parent_group + "/links/" + group_name
        log.info("GET " + req)
        globals["request_count"] += 1
        async with client.get(req, headers=headers, params=params, timeout=timeout) as rsp:
            if rsp.status == 404:
                parent_group = await createGroup(parent_group, group_name)
            elif rsp.status != 200:
                raise HttpProcessingError(code=rsp.status, message="Unexpected error")
            else:
                rsp_json = await rsp.json()
                link_json = rsp_json["link"]
                if link_json["class"] != "H5L_TYPE_HARD":
                    raise ValueError("Unexpected Link type: {}".format(link_json))
                parent_group = link_json["id"]
                h5path_cache[next_path] = parent_group
    
    return parent_group