Ejemplo n.º 1
0
def describe_response(
    status: typing.Union[int, HTTPStatus],
    description: str = "",
    *,
    content: typing.Union[typing.Type[BaseModel], type, dict] = None,
    headers: dict = None,
    links: dict = None,
) -> typing.Callable[[T], T]:
    """
    describe a response in HTTP view function

    https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#responseObject
    """
    status = int(status)
    if not description:
        description = HTTPStatus(status).description

    def decorator(func: T) -> T:
        if not hasattr(func, "__responses__"):
            responses: typing.Dict[int, typing.Dict[str, typing.Any]] = {}
            setattr(func, "__responses__", responses)
        else:
            responses = getattr(func, "__responses__")

        if (content is None or isinstance(content, dict)
                or (not isinstance(content, GenericType) and isclass(content)
                    and issubclass(content, BaseModel))):
            real_content = content
        else:
            real_content = create_model(
                f"ParsingModel[{display_as_type(content)}]",
                __root__=(content, ...))

        responses[status] = {
            "description": description,
            "content": real_content,
            "headers": headers,
            "links": links,
        } | F(lambda d: {k: v
                         for k, v in d.items() if v})

        return func

    return decorator
Ejemplo n.º 2
0
    def get(self):
        http_status = 200

        try:
            http_status = int(request.args.get('http_status', 200))
        except Exception:
            pass

        try:
            desc = HTTPStatus(http_status).name
        except Exception as e:
            desc = str(e)

        r = make_response('', http_status)

        r.headers['X-HTTPStatus-Code'] = http_status
        r.headers['X-HTTPStatus-Text'] = desc

        return r
Ejemplo n.º 3
0
    def serialize(self):
        rv = dict(self.payload or ())

        if self.status_code:
            rv['status_code'] = str(self.status_code)
            rv['reply'] = HTTPStatus(self.status_code).phrase

        if self.message:
            rv['message'] = self.message

        if hasattr(self, 'chain'):
            rv['chain'] = self.chain

        if hasattr(self, 'error'):
            rv['error'] = self.error
            rv['error_message'] = self.error_msg
            rv['traceback'] = self.traceback
        rv['timestamp'] = self.timestamp.strftime('%Y/%m/%d %H:%M:%S')
        return rv
Ejemplo n.º 4
0
Archivo: app.py Proyecto: otyec/zth_git
def Rendeles():
    try:
        rendeles = request.args.get('rendeles')
    except:
        return HTTPStatus(400).description

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    host = "127.0.0.1"
    port = 3223
    s.connect((host, port))

    s.send(rendeles.encode('ascii'))

    data = s.recv(1024)

    s.close()
    return Response(str(data.decode('ascii')),
                    status=200,
                    mimetype='http/text')
Ejemplo n.º 5
0
    def test_verificationoption_without_phone_number(self,
                                                     request_code_by_pds_mock,
                                                     _):
        """ Test that verificationoption template is rendered properly
        without sms"""

        headers = {'Content-type': 'application/json', 'cookie': None}

        with self.client as c:
            with c.session_transaction() as session:
                session["email"] = common.USER_DETAILS.get("email")

        request_code_by_pds_mock.return_value = constants.OTP_REQUEST_SUCCESS
        result = self.client.post('/verificationoption',
                                  data=json.dumps(common.USER_DETAILS),
                                  headers=headers)

        assert HTTPStatus(result.status_code) == HTTPStatus.FOUND
        assert "enteryourcode" in result.headers['Location']
Ejemplo n.º 6
0
    async def send(data):
        if data["type"] == "http.response.start":
            protocol = "HTTP/1.1"
            status = HTTPStatus(data["status"])

            status_line = f"{protocol} {status.value} {status.phrase}"
            headers = [status_line]
            for header in data["headers"]:
                key, value = header[0].decode(), header[1].decode()
                headers.append(f"{key}: {value}")
            headers.append("")

            writer.writelines([f"{line}\r\n".encode() for line in headers])
            await writer.drain()
        elif data["type"] == "http.response.body":
            writer.writelines([data["body"], "\r\n".encode()])
            await writer.drain()
        else:
            raise Exception("Not implemented")
Ejemplo n.º 7
0
            def dump_wrapper(*args, **kwargs):
                response = func(*args, **kwargs)

                if response is None:
                    if model is not None:
                        raise ValueError(
                            "Response cannot not be None with HTTP status %d" %
                            code)
                    return flask.Response(status=code)
                elif isinstance(response, flask.Response) or model is None:
                    return response
                elif isinstance(response, tuple):
                    response, _code = response
                else:
                    _code = code

                if HTTPStatus(_code) is code:
                    response = model.dump(response).data
                return response, _code
Ejemplo n.º 8
0
    def test_verificationoption_without_phone_number(self, _):
        """ Test that verificationoption template is rendered properly
        without sms"""

        headers = {'Content-type': 'application/json', 'cookie': None}

        with self.client as c:
            with c.session_transaction() as session:
                session["email"] = common.USER_DETAILS.get("email")

        result = self.client.post('/verificationoption',
                                  data=json.dumps(common.USER_DETAILS),
                                  headers=headers)

        assert HTTPStatus(result.status_code) == HTTPStatus.OK
        assert common.USER_DETAILS['sms'] not in str(result.data)
        assert 'wrong_value_with_randomxxxx' not in str(result.data)
        assert common.USER_DETAILS['email'] in str(result.data)
        assert 'phone number has' not in str(result.data)
        assert 'email address has' in str(result.data)
Ejemplo n.º 9
0
    def test_given_invalid_submission_payload_response_with_error(self):
        test_session_id = "test.session.id"

        auth_response = AuthResponse(status=HTTPStatus(200))
        auth_response.session_id = test_session_id

        self.mock_auth.login = MagicMock(return_value=auth_response)

        biostudies = BioStudies("url", "username", "password")
        expected_error_message = "Submission validation errors."
        biostudies.api.create_submission = MagicMock(
            side_effect=RestErrorException(expected_error_message,
                                           HTTPStatus.BAD_REQUEST))

        with self.assertRaises(RestErrorException) as context:
            biostudies.send_submission(
                TestBioStudiesService.__create_submission())

        self.assertEqual(HTTPStatus.BAD_REQUEST, context.exception.status_code)
        self.assertEqual(expected_error_message, context.exception.message)
Ejemplo n.º 10
0
    def test_given_valid_submission_payload_response_with_accession(self):
        test_session_id = "test.session.id"

        auth_response = AuthResponse(status=HTTPStatus(200))
        auth_response.session_id = test_session_id

        self.mock_auth.login = MagicMock(return_value=auth_response)

        bst_accession_id = 'bst123'

        response = ResponseObject()
        response.json = {'accno': bst_accession_id}

        biostudies = BioStudies("url", "username", "password")
        biostudies.api.create_submission = MagicMock(return_value=response)

        accession_from_response = biostudies.send_submission(
            TestBioStudiesService.__create_submission())

        self.assertEqual(bst_accession_id, accession_from_response)
Ejemplo n.º 11
0
 def on_web_request_end(self,
                        app: AppT,
                        request: web.Request,
                        response: Optional[web.Response],
                        state: Dict,
                        *,
                        view: web.View = None) -> None:
     """Web server finished working on request."""
     status_code = HTTPStatus(response.status if response else 500)
     time_start = state['time_start']
     time_end = self.time()
     latency_end = time_end - time_start
     state.update(
         time_end=time_end,
         latency_end=latency_end,
         status_code=status_code,
     )
     deque_pushpopmax(self.http_response_latency, latency_end,
                      self.max_avg_history)
     self.http_response_codes[status_code] += 1
Ejemplo n.º 12
0
 def on_web_request_end(self,
                        app: AppT,
                        request: web.Request,
                        response: Optional[web.Response],
                        state: Dict,
                        *,
                        view: web.View = None) -> None:
     """Web server finished working on request."""
     status_code = HTTPStatus(
         response.status if response is not None else 500)
     time_start = state["time_start"]
     time_end = self.time()
     latency_end = time_end - time_start
     state.update(
         time_end=time_end,
         latency_end=latency_end,
         status_code=status_code,
     )
     self.http_response_latency.append(latency_end)
     self.http_response_codes[status_code] += 1
Ejemplo n.º 13
0
    def _request(self,
                 method: str,
                 url: str,
                 fields: Optional[Mapping[str, str]] = None,
                 headers: Mapping[str, str] = None,
                 body: Union[bytes, str, None] = None):
        headers = headers if headers else {}
        if method in ('PATCH', 'POST', 'PUT'):
            headers = {
                **self.base_headers, 'Content-Type': self.API_CONTENT_TYPE,
                **headers
            }
            body = body.encode(self.API_BODY_ENCODING) if isinstance(
                body, str) else body
        else:
            headers = {**self.base_headers, **headers}

        response = self.http.request(method,
                                     self._resolve_url(url),
                                     fields=fields,
                                     headers=headers,
                                     body=body)
        content_type = ContentType.from_response(response)

        if content_type.is_json():
            content = json.loads(
                response.data.decode(
                    content_type.charset(
                        default=self.API_BODY_ENCODING).lower()))
        else:
            content = response.data.decode(
                content_type.charset(default=self.API_BODY_ENCODING).lower())

        self._logger.debug(
            'GitHub API request: %s <%s>\nGitHub API response (status: %s; content type: %s):\n%s',
            method, url, HTTPStatus(response.status), content_type, content)

        if response.status >= 400:
            raise GitHubClientError(response.status, response.reason, content)

        return content
    def test_flash_errors(self, flash_mock, choice_option_mock, _, **kwargs):
        """ Test errors are flashed when setpreference not validated """

        form_mock = MagicMock()

        form_mock.validate_on_submit.return_value = False
        form_mock.radio.data = "Yes"
        form_mock.errors = ['sample_fot_tests']

        choice_option_mock.return_value = form_mock
        flash_mock.return_value = "_"

        mock = kwargs['mock']
        mock.get(self.app.config['PREFERENCE_RESULT_URL'],
                 text=get_preference_results_callback_inactive)
        mock.post(self.app.config['SET_PREFERENCE_URL'], text="{}")

        result = self.client.post('/setyourpreference')

        assert HTTPStatus(result.status_code) == HTTPStatus.OK
        flash_mock.assert_called()
Ejemplo n.º 15
0
 def _write_response(self, bolt_resp: BoltResponse, resp: Response):
     resp.body = bolt_resp.body
     status = HTTPStatus(bolt_resp.status)
     resp.status = str(f"{status.value} {status.phrase}")
     resp.set_headers(bolt_resp.first_headers_without_set_cookie())
     for cookie in bolt_resp.cookies():
         for name, c in cookie.items():
             expire_value = c.get("expires")
             expire = (datetime.strptime(expire_value,
                                         "%a, %d %b %Y %H:%M:%S %Z")
                       if expire_value else None)
             resp.set_cookie(
                 name=name,
                 value=c.value,
                 expires=expire,
                 max_age=c.get("max-age"),
                 domain=c.get("domain"),
                 path=c.get("path"),
                 secure=True,
                 http_only=True,
             )
Ejemplo n.º 16
0
    async def http_dog(self, ctx: commands.Context, code: int) -> None:
        """Sends an embed with an image of a dog, portraying the status code."""
        embed = discord.Embed(title=f'**Status: {code}**')
        url = HTTP_DOG_URL.format(code=code)

        try:
            HTTPStatus(code)
            async with self.bot.http_session.get(url, allow_redirects=False) as response:
                if response.status != 302:
                    embed.set_image(url=url)
                else:
                    raise NotImplementedError

        except ValueError:
            embed.set_footer(text='Inputted status code does not exist.')

        except NotImplementedError:
            embed.set_footer(text='Inputted status code is not implemented by httpstatusdogs.com yet.')

        finally:
            await ctx.send(embed=embed)
Ejemplo n.º 17
0
    async def _do_req(self, path: str, *, method: str = 'GET',
                      data: Optional[Any] = None):
        """
        Performs a request against the instance eureka server.
        :param path: URL Path, the hostname is prepended automatically
        :param method: request method (put/post/patch/get/etc)
        :param data: Optional data to be sent with the request, must
                     already be encoded appropriately.
        :return: optional[dict[str, any]]
        """
        url = self._eureka_url + path
        logger.debug('Performing %s on %s with payload: %s', method, path,
                     data)

        async with self._session.request(method, url, data=data) as resp:
            if 400 <= resp.status < 600:
                # noinspection PyArgumentList
                raise EurekaException(HTTPStatus(resp.status),
                                      await resp.text())
            logger.debug('Result: %s', resp.status)
            return await resp.json()
Ejemplo n.º 18
0
 def _get_error_message(response: Response) -> str:
     error_message = 'Unknown error'
     try:
         error_json = response.json()
         if 'errors' in error_json:
             if isinstance(error_json['errors'], list):
                 err = error_json['errors'][0]
                 error_message = err.get('message')
         elif 'error' in error_json:
             error_message = error_json['error']
         elif 'message' in error_json:
             error_message = error_json['message']
         elif 'errorMessage' in error_json:
             error_message = error_json['errorMessage']
         elif response.status_code == 401:
             error_message = 'Unauthorized: Access is denied due to invalid credentials'
         else:
             error_message = HTTPStatus(response.status_code).phrase
         return error_message
     except:
         return response.text or error_message
Ejemplo n.º 19
0
def _json_exception(status: int, exception: web.HTTPException,
                    url: URL) -> str:
    """Convert an HTTP exception into a problem detailed JSON object.

    The problem details are in accordance with RFC 7807.
    (https://tools.ietf.org/html/rfc7807)

    :param status: Status code of the HTTP exception
    :param exception: Exception content
    :param url: Request URL that caused the exception
    :returns: Problem detail JSON object as a string
    """
    body = json.dumps({
        "type": "about:blank",
        # Replace type value above with an URL to
        # a custom error document when one exists
        "title": HTTPStatus(status).phrase,
        "detail": exception.reason,
        "instance": url.path,  # optional
    })
    return body
Ejemplo n.º 20
0
 def _do_req(self,
             path: str,
             *,
             method: str = 'GET',
             data: Optional[Dict] = None):
     """
     Performs a request against the instance eureka server.
     :param path: URL Path, the hostname is prepended automatically
     :param method: request method (put/post/patch/get/etc)
     :param data: Optional data to be sent with the request, must
                  already be encoded appropriately.
     :return: optional[dict[str, any]]
     """
     url = self._eureka_url + path
     logger.debug('Performing %s on %s with payload: %s', method, path,
                  data)
     resp = requests.request(method, url, json=data or {})
     if 400 <= resp.status_code < 600:
         raise EurekaException(HTTPStatus(resp.status_code), resp.text)
     logger.debug('Result status: %s', resp.status_code)
     return resp.text
Ejemplo n.º 21
0
 def name_of(cls, value):
     """
     >>> FeedResponseStatus.name_of(200)
     'OK'
     >>> FeedResponseStatus.name_of(-200)
     'RSSANT_CONNECTION_ERROR'
     >>> FeedResponseStatus.name_of(-999)
     'RSSANT_E999'
     """
     if value > 0:
         try:
             return HTTPStatus(value).name
         except ValueError:
             # eg: http://huanggua.sinaapp.com/
             # ValueError: 600 is not a valid HTTPStatus
             return f'HTTP_{value}'
     else:
         try:
             return 'RSSANT_' + FeedResponseStatus(value).name
         except ValueError:
             return f'RSSANT_E{abs(value)}'
Ejemplo n.º 22
0
def handle_new_case_triage_etl(data: Dict[str, Any],
                               _: ContextType) -> Tuple[str, HTTPStatus]:
    """This function is triggered when a file is dropped in the
    `{project_id}-case-triage-data` bucket. If the file matches `etl_*.csv`,
    then it makes a request to import the CSV to Cloud SQL.
    """
    project_id = os.environ.get(GCP_PROJECT_ID_KEY)
    if not project_id:
        logging.error(
            "No project id set for call to update auth0 users, returning.")
        return "", HTTPStatus.BAD_REQUEST

    filename = data["name"]
    if not filename.startswith("etl_") or not filename.endswith(".csv"):
        logging.info("Ignoring file %s", filename)
        return "", HTTPStatus.OK

    import_url = _APP_ENGINE_IMPORT_CASE_TRIAGE_ETL_CSV_TO_SQL_URL.format(
        project_id, filename)
    import_response = make_iap_request(import_url, IAP_CLIENT_ID[project_id])
    return "", HTTPStatus(import_response.status_code)
Ejemplo n.º 23
0
    async def get_file(self, url: str, filename: str,
                       session: aiohttp.ClientSession) -> None:
        """Download a single file.
        
        Parameters
        ----------
        url: str, required
            URL path to file.
        filename: str, required
            Local filename used to write downloaded file. This will save the file 
            to self.output_directory/filename
        session: aiohttp.ClientSession, required
            Session object used for retrieval.
            
        Returns
        -------
        None
        """
        # Retrieve a single file
        async with session.get(url, ssl=self.ssl_context,
                               timeout=900) as response:
            # Warn if unable to locate file
            if response.status != HTTPStatus.OK:
                status = HTTPStatus(response.status_code)
                message = (f"HTTP Status: {status.value}" +
                           f" - {status.phrase}" +
                           f" - {status.description}\n" + f"{response.url}")
                warnings.warn(message, RuntimeWarning)
                return

            # Construct output file path
            output_file = self.output_directory / filename

            # Stream download
            async with aiofiles.open(output_file, 'wb') as fo:
                while True:
                    chunk = await response.content.read(1024)
                    if not chunk:
                        break
                    await fo.write(chunk)
Ejemplo n.º 24
0
def process_openweathermap(params, cfg) -> Response:
    """
    Process one of api.openweathermap.org method call as specified in {cfg} dict + passing {params} in request.
    Return actual status line, specified headers and content 'as is' from provider
    """
    headers = json.loads(cfg.get('request_headers',
                                 '{}'))  # '{"Connection": "close"}'
    # openweather supports head, get and post http methods, let's use get one
    with requests.get(
            url=cfg['request_url_path'],
            params=params,
            headers=headers,
            timeout=float(
                cfg.get('request_time_out', 0)
            )  # https://docs.python-requests.org/en/master/user/quickstart/#timeouts
    ) as response:
        logging.debug('request url %s headers: %s', response.request.url,
                      response.request.headers)
        response_status = status_line(HTTPStatus(response.status_code))
        response_headers_list_relay = [
            header.strip().title()
            for header in cfg.get('response_headers_list_relay', '').split(',')
        ]
        response_headers = dict((k, v) for k, v in response.headers.items()
                                if k in response_headers_list_relay)
        response_content = response.content
        response_headers['Server'] = response_headers.get(
            'Server', APP_SERVER_NAME)
        response_headers['Date'] = response_headers.get(
            'Date', gmtime_string())
        response_headers['Content-Type'] = response_headers.get(
            'Content-Type',
            response.headers.get(
                'Content-Type',
                f'application/json; charset={APP_ENC_HTML_DEFAULT}'))
        response_headers['Content-Length'] = response_headers.get(
            'Content-Length', str(len(response_content)))
        response_headers['Connection'] = response_headers.get(
            'Connection', 'close')
        return Response(response_status, response_headers, response_content)
Ejemplo n.º 25
0
def my_orders(request):
    headers = {
        'Authorization': f'Token {settings.ORDER_SERVICE_AUTHTOKEN}',
        'Content-type': 'application/json'
    }
    get_order_endpoint = f'/api/customer/{request.user.id}/orders/get/'
    service_url = f'{settings.ORDER_SERVICE_BASEURL}{get_order_endpoint}'

    response = requests.get(service_url, headers=headers)
    if HTTPStatus(response.status_code) is HTTPStatus.OK:
        request_data = json.loads(response.text)
        print('request data')
        from pprint import pprint
        pprint(request_data)
        context = {'orders': request_data}
    else:
        messages.add_message(
            request, messages.ERROR,
            ('Unfortunately we could not receive your orders.',
             ' Try again later.'))
        context = {"orders": []}
    return render(request, 'main/my-orders.html', context)
Ejemplo n.º 26
0
    def on_click(self):
        self.statusBar().showMessage('Connecting to Elastic Server')

        result = kibana_api.authenticate(self.useredit.text(), self.passedit.text(), self.ipedit.text())

        if not isinstance(result, str):
            if result != HTTPStatus.OK.value:
                error = HTTPStatus(result)
                self.statusBar().showMessage("Error " + str(error.value) + ', ' + error.phrase)
                return
        else:
            QMessageBox().critical(self, "Error", result, QMessageBox.Ok)
            return

        self.statusBar().showMessage('Connected')

        # Write yml file
        self.write_yaml()

        # sleep(0.5)
        self.board = ManageBoard()
        self.close()
    def test_render_set_preferences_again_when_active(self, choice_option_mock,
                                                      _, **kwargs):
        """ Test rerender setpreference when active """

        form_mock = MagicMock()

        form_mock.validate_on_submit.return_value = False

        choice_option_mock.return_value = form_mock

        with self.client as c:
            with c.session_transaction() as session:
                session['pds_opted_out'] = 'active'

        headers = {
            'Content-type': 'application/json',
            'cookie': common.SESSION_ID
        }
        result = self.client.post('/setyourpreference', headers=headers)

        assert HTTPStatus(result.status_code) == HTTPStatus.OK
        assert form_mock.radio.data == 'No'
Ejemplo n.º 28
0
    def test_your_details_review_post_fail(self, _, __, **kwargs):
        """ Test your_details_review page re rendered when post to PDS_SEARCH_URL unsuccessful"""

        common.registerExceptionHandlers(self.app)
        mock = kwargs['mock']
        adapter = mock.post(
            self.app.config['PDS_SEARCH_URL'], text=common.pds_search_callback)
        headers = {'Content-type': 'application/json',
                   'cookie': common.SESSION_ID}
        # headers={'Content-type': 'application/json', 'cookie': None}

        with self.client as c:
            with c.session_transaction() as session:
                session["first_name"] = "error"

        result = self.client.post('/yourdetailsreview',
                                  data=json.dumps(common.USER_DETAILS),
                                  headers=headers)

        assert HTTPStatus(result.status_code) == HTTPStatus.FOUND
        assert '/genericerror' in result.headers['Location']
        assert mock.called_once is True
    def test_render_set_preferences_again_when_get_preference_empty(
            self, choice_option_mock, _):
        """ Test rerender setpreference when empty preference """

        form_mock = MagicMock()

        form_mock.validate_on_submit.return_value = False

        choice_option_mock.return_value = form_mock

        with self.client as c:
            with c.session_transaction() as session:
                session['pds_opted_out'] = constants.GET_PREFERENCE_EMPTY

        headers = {
            'Content-type': 'application/json',
            'cookie': common.SESSION_ID
        }
        result = self.client.post('/setyourpreference', headers=headers)

        assert HTTPStatus(result.status_code) == HTTPStatus.OK
        assert form_mock.radio.data not in ('Yes', 'No')
def problems_middleware(request, handler):
    try:
        response = yield from handler(request)
    except ProblemException as exc:
        response = problem(status=exc.status,
                           detail=exc.detail,
                           title=exc.title,
                           type=exc.type,
                           instance=exc.instance,
                           headers=exc.headers,
                           ext=exc.ext)
    except (werkzeug_HTTPException, _HttpNotFoundError) as exc:
        response = problem(status=exc.code,
                           title=exc.name,
                           detail=exc.description)
    except web.HTTPError as exc:
        if exc.text == "{}: {}".format(exc.status, exc.reason):
            detail = HTTPStatus(exc.status).description
        else:
            detail = exc.text
        response = problem(status=exc.status, title=exc.reason, detail=detail)
    except (
            web.HTTPException,  # eg raised HTTPRedirection or HTTPSuccessful
            asyncio.CancelledError,  # skipped in default web_protocol
    ):
        # leave this to default handling in aiohttp.web_protocol.RequestHandler.start()
        raise
    except asyncio.TimeoutError as exc:
        # overrides 504 from aiohttp.web_protocol.RequestHandler.start()
        logger.debug('Request handler timed out.', exc_info=exc)
        response = _generic_problem(HTTPStatus.GATEWAY_TIMEOUT, exc)
    except Exception as exc:
        # overrides 500 from aiohttp.web_protocol.RequestHandler.start()
        logger.exception('Error handling request', exc_info=exc)
        response = _generic_problem(HTTPStatus.INTERNAL_SERVER_ERROR, exc)

    if isinstance(response, ConnexionResponse):
        response = yield from AioHttpApi.get_response(response)
    return response