Exemplo n.º 1
0
def request_with_override(*,
                          uri: Union[str, SplitResult],
                          override_uri: Optional[Union[str,
                                                       SplitResult]] = None,
                          method: str = 'GET',
                          headers: Dict[str, str] = {},
                          **kwargs: Any) -> Any:
    # why not use endpoints? Despite the fact that it accepts a method and payload, it doesn't *actually* generate
    # sufficient headers so we'll use requests for these since we can
    if isinstance(uri, str):
        uri = urlsplit(uri)
    elif isinstance(uri, SplitResult):
        pass
    else:
        raise AssertionError(f'what is uri? {uri}')

    # don't always need this, but it doesn't hurt
    if 'Host' not in headers:
        headers = dict(headers)
        headers['Host'] = to_aws4_request_compatible_host(uri)
    s = requests.Session()
    if override_uri:
        override_uri = _urlsplit_if_not_already(override_uri)
        uri = urlunsplit((uri.scheme, override_uri.netloc, uri.path, uri.query,
                          uri.fragment))
        s.mount('https://', HostHeaderSSLAdapter())
    else:
        uri = urlunsplit(uri)
    return s.request(method=method, url=uri, headers=headers, **kwargs)
Exemplo n.º 2
0
 def _profile(self, gremlin_query: str) -> str:
     """
     return the Neptune specific explaination of the RUNNING query.  Now it can't return the result set, so the
     utility is limited to cases where you can re-run this, or are running as a one off from console, or as a last
     resort
     see https://docs.aws.amazon.com/neptune/latest/userguide/gremlin-profile-api.htlm
     see https://docs.aws.amazon.com/neptune/latest/userguide/gremlin-explain-background.html
     """
     # why not use endpoints? Despite the fact that it accepts a method and payload, it doesn't *actually* generate
     # sufficient headers so we'll use requests for these since we can
     url = urlsplit(self.endpoints.gremlin_endpoint().prepare_request().uri)
     assert url.scheme in ('wss', 'ws') and url.path == '/gremlin' and not url.query and not url.fragment, \
         f'url is not a Neptune ws url?: {url}'
     _profile_url = urlunsplit(
         ('https' if url.scheme == 'wss' else 'http', url.netloc, url.path + '/profile', '', ''))
     host = to_aws4_request_compatible_host(_profile_url)
     if self.override_uri:
         _profile_url = urlunsplit(
             ('https' if url.scheme == 'wss' else 'http', self.override_uri.netloc, url.path + '/profile', '', ''))
     s = requests.Session()
     s.mount('https://', HostHeaderSSLAdapter())
     response = s.post(_profile_url, auth=self.aws_auth,
                       data=json.dumps(dict(gremlin=gremlin_query)).encode('utf-8'),
                       # include Host header
                       headers=dict(Host=host))
     return response.content.decode('utf-8')