Esempio n. 1
0
    def request(
        self,
        method: typing.Union[str, typing.Callable],
        url: typing.Optional[typing.Union[str, typing.Pattern]] = None,
        status_code: typing.Optional[int] = None,
        content: typing.Optional[ContentDataTypes] = None,
        content_type: typing.Optional[str] = None,
        headers: typing.Optional[HeaderTypes] = None,
        pass_through: bool = False,
        alias: typing.Optional[str] = None,
    ) -> RequestPattern:
        """
        Adds a request pattern with given mocked response details.
        """
        headers = Headers(headers or {})
        if content_type:
            headers["Content-Type"] = content_type

        response = ResponseTemplate(status_code, headers, content)
        pattern = RequestPattern(
            method,
            url,
            response,
            pass_through=pass_through,
            alias=alias,
            base_url=self._base_url,
        )

        self.add(pattern)

        return pattern
Esempio n. 2
0
 def _generate_authorization_headers(self, request: Request) -> Headers:
     date = self._get_formatted_date()
     digest = self._compute_digest(request)
     string = self._compose_string(request, date, digest)
     signature = self._sign_string(string)
     authorization_header = self._compose_authorization_header(signature)
     headers = {'Host': request.url.host, 'Date': date, 'Digest': digest, 'Authorization': authorization_header}
     return Headers(headers)
Esempio n. 3
0
 def __init__(self):
     self.client = AsyncClient()
     self.client.headers = Headers({
         "User-Agent":
         "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 "
         "Safari/537.36"
     })
     self.disabled_until: Optional[datetime.datetime] = None
Esempio n. 4
0
    def test_last_modified_is_not_used_when_cache_control_present(self):
        headers = {
            "Date": self.now,
            "Last-Modified": self.week_ago,
            "Cache-Control": "private",
        }

        assert self.heuristic.update_headers(Headers(headers), 200) == {}
Esempio n. 5
0
 def test_last_modified_is_used_when_cache_control_public(self):
     headers = {
         "Date": self.now,
         "Last-Modified": self.week_ago,
         "Cache-Control": "public",
     }
     modified = self.heuristic.update_headers(Headers(headers), 200)
     assert ["expires"] == list(modified.keys())
     assert datetime(*parsedate(modified["expires"])
                     [:6]) > datetime.now()  # type: ignore
Esempio n. 6
0
 def __init__(self, token: str):
     self.client = AsyncClient(timeout=10)
     self.client.headers = Headers({
         "Accept":
         "application/json",
         "Authorization":
         f"Bearer {token}",
         "User-Agent":
         "holo observatory bot/1.0.0"
     })
Esempio n. 7
0
    async def request(
            self, method: str, path: str, headers: typing.Union[Headers, dict],
            result_body: io.BytesIO, body: typing.Union[io.BytesIO, bytes] = None) -> Headers:
        """Returns the headers from the server after making the given request
        at the given path using the given headers. The body of the response is
        written to the result body.

        Arguments:
        - `method (str)`: The HTTP verb to perform, uppercased, e.g., 'GET'
        - `path (str)`: The path within the host, e.g., /api/foo
        - `headers (Headers, dict)`: The headers to send with case-insensitive
          keys.
        - `result_body (io.BaseIO)`: The IO to write the body from the server;
          nothing is written if the server does not provide a body.
        - `body (bytes, io.BaseIO, None)`: The bytes to send to the server in
           the body, specified either as bytes (which are wrapped with BytesIO)
           or just an bytes-based io object.

        Returns:
        - `headers (Headers)`: The returned headers and trailers from the server.
        """
        if not self.opened:
            await self.open()

        if not isinstance(headers, Headers):
            headers = Headers(headers)

        itr = self.rconn.request(method, self.host, path, headers.multi_items(), body)

        raw_headers = await itr.__anext__()
        response_headers = Headers(raw_headers)

        chunk = await itr.__anext__()
        while chunk is not None:
            result_body.write(chunk)
            chunk = await itr.__anext__()

        raw_trailers = await itr.__anext__()
        response_trailers = Headers(raw_trailers)
        for key, val in response_trailers.items():
            response_headers[key] = val

        return response_headers
Esempio n. 8
0
 async def request(
     self,
     method: bytes,
     url: typing.Tuple[bytes, bytes, int, bytes],
     headers: typing.List[typing.Tuple[bytes, bytes]],
     stream: ContentStream,
     timeout: typing.Dict[str, typing.Optional[float]] = None,
 ) -> typing.Tuple[bytes, int, bytes, typing.List[typing.Tuple[
         bytes, bytes]], ContentStream]:
     headers = Headers()
     body = JSONStream({"ok": "ok"})
     return b"HTTP/1.1", 200, b"OK", headers, body
Esempio n. 9
0
 def __init__(
     self,
     status_code: typing.Optional[int] = None,
     headers: typing.Optional[HeaderTypes] = None,
     content: typing.Optional[ContentDataTypes] = None,
     context: typing.Optional[Kwargs] = None,
 ) -> None:
     self.http_version = 1.1
     self.status_code = status_code or 200
     self.context = context if context is not None else {}
     self._headers = Headers(headers or {})
     self._content = content if content is not None else b""
Esempio n. 10
0
async def create_session(session: AsyncClient, token: str):
    data = {TIMESTAMP: datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
    resp = await session.post(constants.BASE_URL +
                              "/api/portal/CreateSession2",
                              headers=Headers({
                                  **bearer_header(token),
                                  **{
                                      "Content-Type": "application/json"
                                  }
                              }),
                              json=data)

    return resp.json()['BrowserSessionId']
Esempio n. 11
0
    def __init__(self) -> None:
        """Define DOI credentials and config.

        :param state: can be publish, register, hide or draft.
        """
        self.rems_api = environ.get("REMS_API", "")
        self.rems_user = environ.get("REMS_USER", "")
        self.rems_key = environ.get("REMS_KEY", "")
        self.config = CONFIG_INFO["rems"]
        self.headers = Headers({
            "Content-Type": "application/json",
            "Accept": "application/json",
            "x-rems-api-key": self.rems_key,
            "x-rems-user-id": self.rems_user,
        })
Esempio n. 12
0
 def __init__(self,
              key_id: str,
              rsa_key: RSAPrivateKey,
              staging: bool = False,
              **kwargs) -> None:
     auth = SatispayAuth(key_id, rsa_key)
     headers = kwargs.get('headers', Headers())
     headers.update({'Accept': 'application/json'})
     if staging:
         base_url = URL('https://staging.authservices.satispay.com')
     else:
         base_url = URL('https://authservices.satispay.com')
     super().__init__(auth=auth,
                      headers=headers,
                      base_url=base_url,
                      **kwargs)
Esempio n. 13
0
    def _inject_headers(self, headers: dict):
        # need to coerce to str
        headers = {k: str(v) for k, v in headers.items()}
        headers.update(
            {"date": get_utc_datetime().strftime("%a, %d %b %y %T %z")})

        # inject user information to request headers
        user = context_user()
        headers.update({
            settings.INTERNAL_REQUEST_USER_HEADER.lower():
            to_header_value(user)
        })
        # inject correlation_id
        correlation_id = context_correlation_id()
        headers.update(
            {settings.REQUEST_ID_HEADER_FIELD.lower(): correlation_id})

        return Headers(headers)
Esempio n. 14
0
    def _fetch_stored_request(self, method, path):
        cache_key = self._cache_key(method, path)
        with self.cache.transact():
            cached_at = self.cache.get(cache_key + b'-cached-at')
            if cached_at is None:
                return (None, None, None)

            header = self.cache.get(cache_key + b'-header')
            if header is None:
                self.cache.delete(cache_key + b'-cached-at')
                return (None, None, None)

            body = self.cache.get(cache_key + b'-body')
            if body is None:
                self.cache.delete(cache_key + b'-cached-at')
                self.cache.delete(cache_key + b'-header')
                return (None, None, None)

            return (cached_at, Headers(header), body)
Esempio n. 15
0
    async def create_draft_doi(self, user: str,
                               inbox_path: str) -> Union[Dict, None]:
        """Create an auto-generated draft DOI.

        We are using just the prefix for the DOI so that it will be autogenerated.
        """
        dataset = generate_dataset_id(user, inbox_path, self.ns_url)
        suffix = shortuuid.uuid(name=dataset)[:10]
        doi_suffix = f"{suffix[:4]}-{suffix[4:]}"

        headers = Headers({"Content-Type": "application/json"})
        draft_doi_payload = {
            "data": {
                "type": "dois",
                "attributes": {
                    "doi": f"{self.doi_prefix}/{doi_suffix}"
                }
            }
        }
        async with AsyncClient() as client:
            response = await client.post(self.doi_api,
                                         auth=(self.doi_user, self.doi_key),
                                         json=draft_doi_payload,
                                         headers=headers)
        doi_data = None
        if response.status_code == 201:
            draft_resp = response.json()
            _doi = draft_resp["data"]["attributes"]["doi"]
            _suffix = draft_resp["data"]["attributes"]["suffix"]
            LOG.debug(f"DOI draft created and response was: {draft_resp}")
            LOG.info(f"DOI draft created with doi: {_doi}.")
            doi_data = {
                "suffix": _suffix,
                "fullDOI": _doi,
                "dataset": f"{self.ns_url}/{_suffix.lower()}",
            }
        else:
            LOG.error(
                f"DOI API create draft request failed with code: {response.status_code}"
            )
            doi_data = self._check_errors(response, doi_suffix)

        return doi_data
Esempio n. 16
0
def failure_response():
    return Response(
        status_code=404,
        request=Request(url="https://leomenezessz.github.io/pyppium/",
                        method="get"),
        headers=Headers({
            "cache-control": "no-cache",
            "content-type": "application/json; charset=utf-8",
            "date": "Fri, 04 Sep 2020 06:35:18 GMT",
            "server": "nginx",
            "status": "404 Not Found",
            "vary": "Origin",
            "x-powered-by": "Phusion Passenger",
            "x-request-id": "632eeff4-a6f4-4016-82aa-75d10805f149",
            "x-runtime": "0.093190",
            "x-ua-compatible": "IE=Edge,chrome=1",
            "content-length": "36",
            "connection": "keep-alive",
        }),
    )
Esempio n. 17
0
 def test_production(self, key_id, rsa_key,
                     create_payment_production_signature):
     route = respx.post(
         'https://authservices.satispay.com/g_business/v1/payments')
     body_params = {
         'callback_url': 'https://test.test?payment_id={uuid}',
         'expiration_date': '2019-03-18T16:10:24.000Z',
         'external_code': 'test_code',
         'metadata': {
             'metadata': 'test'
         }
     }
     headers = Headers({'Idempotency-Key': 'test_idempotency_key'})
     satispaython.create_payment(key_id, rsa_key, 100, 'EUR', body_params,
                                 headers)
     assert route.called
     assert route.call_count == 1
     request = route.calls.last.request
     assert request.method == 'POST'
     assert json.loads(request.content.decode()) == {
         'flow': 'MATCH_CODE',
         'amount_unit': 100,
         'currency': 'EUR',
         'callback_url': 'https://test.test?payment_id={uuid}',
         'expiration_date': '2019-03-18T16:10:24.000Z',
         'external_code': 'test_code',
         'metadata': {
             'metadata': 'test'
         }
     }
     assert request.headers['Idempotency-Key'] == 'test_idempotency_key'
     assert request.headers['Accept'] == 'application/json'
     assert request.headers['Content-Type'] == 'application/json'
     assert request.headers['Host'] == 'authservices.satispay.com'
     assert request.headers['Date'] == 'Mon, 18 Mar 2019 15:10:24 +0000'
     assert request.headers[
         'Digest'] == 'SHA-256=dOjZtX6Has9wFZQDmriLhIfThHD11nuxFZNIjp7FwR0='
     assert request.headers['Authorization'] == f'Signature keyId="{key_id}", ' \
                                                f'algorithm="rsa-sha256", ' \
                                                f'headers="(request-target) host date digest", ' \
                                                f'signature="{create_payment_production_signature}"'
Esempio n. 18
0
    def __init__(self, **kwargs):
        data = {
            'body': {},
            'headers': {},
            'path': {},
            'query': {},
        }
        data['header'] = data['headers']
        skip_validation = kwargs.pop('esi_skip_validation', False)
        errors = {}
        for param in self.params:
            value = kwargs.pop(param.safe_name)
            if value == param.default:
                continue
            try:
                param.validate(value)
            except ValidationError as e:
                e.update_error_dict(errors, param.safe_name)
            data[param.part][param.name] = value
        if errors and not skip_validation:
            raise ValidationError(errors)

        cls = self.__class__
        body = None
        if data['body']:
            body = list(data['body'].values())[0]

        self.method = cls.method.upper()
        self.url = URL(self.path.format(**data['path']),
                       allow_relative=True,
                       params=data['query'])
        self.headers = Headers(data['header'])
        self.stream = encode(body, None, None)
        self.timer = ElapsedTimer()
        self.prepare()
        # Clear out headers we don't need (These will be set by the session)
        for key in ["User-Agent"]:
            if key in self.headers:
                del self.headers[key]
Esempio n. 19
0
async def app(scope, receive, send):
    request = Request(scope, receive)

    user_key = request.headers.get('api_key')
    if not USERS_CACHE.get(user_key):
        USERS_CACHE[user_key] = await auth(user_key)

    headers = (tuple(["session_id",
                      USERS_CACHE[user_key]]), *request.headers.items())
    print(headers)

    async with httpx.AsyncClient() as client:
        url = TARGET_SERVER + request.url.path
        r = await client.request(request.method,
                                 url,
                                 headers=Headers(headers),
                                 data=await request.body())

    response = UJSONResponse(r.json())
    #response = Response(content=r.content, headers=r.headers, status_code=r.status_code, media_type="application/json")
    result = await response(scope, receive, send)
    return result
Esempio n. 20
0
 async def create_payment(self,
                          amount_unit: int,
                          currency: str,
                          body_params: Optional[dict] = None,
                          headers: Optional[Headers] = None) -> Response:
     target = URL('/g_business/v1/payments')
     try:
         headers.update({'Content-Type': 'application/json'})
     except AttributeError:
         headers = Headers({'Content-Type': 'application/json'})
     try:
         body_params.update({
             'flow': 'MATCH_CODE',
             'amount_unit': amount_unit,
             'currency': currency
         })
     except AttributeError:
         body_params = {
             'flow': 'MATCH_CODE',
             'amount_unit': amount_unit,
             'currency': currency
         }
     return await self.post(target, json=body_params, headers=headers)
Esempio n. 21
0
 def _only_if_cached_failure(self, method, path, headers, request_body,
                             body):
     return Headers(((b':status', b'504'), ))
Esempio n. 22
0
 def test_last_modified_is_not_used_when_status_is_unknown(self):
     headers = {"Date": self.now, "Last-Modified": self.week_ago}
     status = 299
     assert self.heuristic.update_headers(Headers(headers), status) == {}
Esempio n. 23
0
 def test_last_modified_is_used(self):
     headers = {"Date": self.now, "Last-Modified": self.week_ago}
     modified = self.heuristic.update_headers(Headers(headers), 200)
     assert ["expires"] == list(modified.keys())
     assert datetime(*parsedate(modified["expires"])
                     [:6]) > datetime.now()  # type: ignore
Esempio n. 24
0
 def test_expires_is_not_replaced_when_present(self):
     headers = {"Expires": self.day_ahead}
     assert self.heuristic.update_headers(Headers(headers), 200) == {}
Esempio n. 25
0
from inspect import iscoroutinefunction
from json import JSONDecodeError
from typing import Optional

import aiohttp
from fastjsonschema.exceptions import JsonSchemaException
# noinspection PyPackageRequirements
from httpcore import TimeoutException
from httpx import AsyncClient, HTTPError, Headers, NetworkError

from .blivedm import BLiveClient
from .schemas import room_info_schema

http = AsyncClient(headers=Headers({
    'User-Agent':
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) '
    'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
}))


class LiveStatus(Enum):
    DISABLED = 0
    LIVE = 1
    PREPARE = 2


@dataclass
class LiveRoom:
    room_id: int
    title: str
    uid: int
Esempio n. 26
0
 def test_expiry_is_no_more_than_twenty_four_hours(self):
     headers = {"Date": self.now, "Last-Modified": self.year_ago}
     modified = self.heuristic.update_headers(Headers(headers), 200)
     assert ["expires"] == list(modified.keys())
     assert self.day_ahead == modified["expires"]
Esempio n. 27
0
def test_header_does_not_exist():
    headers = Headers({"foo": "bar"})
    with pytest.raises(KeyError):
        del headers["baz"]
Esempio n. 28
0
 def from_text(cls, text):
     headers = Headers()
     for line in text.splitlines():
         k, v = line.split(": ", 1)
         headers[k] = v
     return cls.from_headers(headers)
Esempio n. 29
0
    async def request(self,
                      method,
                      path,
                      params=None,
                      headers=None,
                      data=None,
                      json=None) -> ResponseData:
        """Makes a request using the given method to the given path.

        Arguments:
        - `method (str)`: The HTTP verb (uppercased)
        - `path (str)`: The path to make the request to, must just be the
          path and must be prefixed with a forward slash (e.g., /api)
        - `params (dict, None)`: If specified the keys and values must be
          strings and they will be injected into the query arguments.
        - `headers (dict, Headers, None)`: If specified these headers will be
          sent alongside the request.
        - `data (io.BytesIO, bytes, str, None)`: Sent in the body of the
          request. If specified as a BytesIO like object the data will be
          streamed across the connection, which is very suitable for uploading
          files. If specified as a string it is encoding using utf-8. Unlike
          requests we do not support `application/x-www-form-urlencoded` style
          requests natively.
        - `json (any)`: If not `None` then `data` is ignored, and `json`
          will be serialized as json to text and then treated as if it were
          the value of `data`.

        Returns:
        - `response (ResponseData)`: The result from the server, fully loaded
          in memory.
        """
        if params is not None:
            base_path, _, current_args = path.partition('?')
            query_args = dict(
                arg.split('=') for arg in current_args.split('&') if arg)
            query_args = {**query_args, **params}
            path = base_path + '?' + urlencode(query_args)

        content_type_hint = None
        if json is not None:
            data = jsonlib.dumps(json).encode('utf-8')
            content_type_hint = 'application/json; charset=utf-8'

        if isinstance(data, str) and content_type_hint is None:
            data = data.encode('utf-8')
            content_type_hint = 'text/plain; charset=utf-8'

        if headers is None:
            headers = {}

        if content_type_hint is not None and headers.get(
                'content-type') is None:
            headers['content-type'] = content_type_hint

        headers = {**self.default_headers, **headers}

        result_body = io.BytesIO()
        response_headers = await self.h2conn.request(method, path, headers,
                                                     result_body, data)

        return ResponseData(self.h2conn.host, path, method, Headers(headers),
                            response_headers, result_body.getvalue())
Esempio n. 30
0
    async def request(self,
                      method: str,
                      path: str,
                      headers: typing.Union[Headers, dict],
                      result_body: io.BytesIO,
                      body: typing.Union[io.BytesIO, bytes] = None) -> Headers:
        """See `h2client.http2_connection.HTTP2Connection#request`"""
        if not isinstance(headers, Headers):
            headers = Headers(headers)

        request_directives = self._get_cache_directives(headers)
        only_if_cached = 'only-if-cached' in request_directives

        if 'no-store' in request_directives:
            if only_if_cached:
                return self._only_if_cached_failure(method, path, headers,
                                                    result_body, body)

            return await self._no_store_request(method, path, headers,
                                                result_body, body)

        if 'no-cache' in request_directives:
            if only_if_cached:
                return self._only_if_cached_failure(method, path, headers,
                                                    result_body, body)

            return await self._no_cache_request(method, path, headers,
                                                result_body, body)

        cached_at, cached_headers, cached_body = self._fetch_stored_request(
            method, path)

        if cached_at is None:
            if only_if_cached:
                return self._only_if_cached_failure(method, path, headers,
                                                    result_body, body)

            return await self._no_cache_request(method, path, headers,
                                                result_body, body)

        time_since_cached = time.time() - cached_at
        cached_headers['age'] = str(time_since_cached)
        cached_directives = self._get_cache_directives(cached_headers)

        if 'immutable' in cached_directives:
            if cached_body:
                result_body.write(cached_body)
            return cached_headers

        must_revalidate = 'must-revalidate' in cached_directives
        max_age = int(cached_directives.get('max-age', '0'))
        if 'max-age' in request_directives:
            max_age = min(max_age, int(request_directives['max-age']))

        stale_while_revalidate = int(
            cached_directives.get('stale-while-revalidate', '0'))
        stale_if_error = int(cached_directives.get('stale-if-error', '0'))

        if 'max-stale' in request_directives:
            max_stale = int(request_directives['max-stale'])
            stale_while_revalidate = min(stale_while_revalidate, max_stale)
            stale_if_error = min(stale_if_error, max_stale)

        if 'min-fresh' in request_directives:
            max_age = max(max_age - int(request_directives['min-fresh']), 0)
            stale_while_revalidate = 0
            stale_if_error = 0

        if time_since_cached < max_age:
            if must_revalidate:
                self._revalidate_request(method, path, headers, body)
            if cached_body:
                result_body.write(cached_body)
            return cached_headers

        if time_since_cached < max_age + stale_while_revalidate:
            if (must_revalidate
                    or max_age < time_since_cached) and not only_if_cached:
                self._revalidate_request(method, path, headers, body)
            if cached_body:
                result_body.write(cached_body)
            return cached_headers

        if only_if_cached:
            return self._only_if_cached_failure(method, path, headers,
                                                result_body, body)

        mem_body = io.BytesIO()
        real_result_headers = await self._no_cache_request(
            method, path, headers, mem_body, body)

        time_since_cached = time.time() - cached_at
        cached_headers['age'] = str(time_since_cached)

        is_error = real_result_headers.get(':status', '200') in ('500', '502',
                                                                 '503', '504')

        if (time_since_cached < max_age + stale_if_error) and is_error:
            self._store_request(method,
                                path,
                                cached_headers,
                                cached_body,
                                cached_at=cached_at)
            if cached_body:
                result_body.write(cached_body)
            return cached_headers

        result_body.write(mem_body.getvalue())
        return real_result_headers