Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added cookie support #1310

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions stix_shifter_utils/stix_transmission/utils/RestApiClientAsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(self, host, port=None, headers={}, url_modifier_function=None, cert
self.auth = auth

# This method is used to set up an HTTP request and send it to the server
async def call_api(self, endpoint, method, headers=None, data=None, urldata=None, timeout=None):
async def call_api(self, endpoint, method, headers=None, cookies=None, data=None, urldata=None, timeout=None):
try:
# covnert server cert to file
if self.server_cert_file_content:
Expand Down Expand Up @@ -122,9 +122,10 @@ async def call_api(self, endpoint, method, headers=None, data=None, urldata=None
async with call(url, headers=actual_headers, params=urldata, data=data,
ssl=self.ssl_context,
timeout=client_timeout,
cookies=cookies,
auth=self.auth) as response:

respWrapper = ResponseWrapper(response)
respWrapper = ResponseWrapper(response, client)
await respWrapper.wait()

if respWrapper.code == 429:
Expand Down Expand Up @@ -166,8 +167,9 @@ class ResponseWrapper:
_headers = None
_code = None

def __init__(self, response):
def __init__(self, response, client=None):
self.response = response
self.client = client

async def wait(self):
self._content = await self.response.content.read()
Expand All @@ -180,6 +182,12 @@ def read(self):
def raise_for_status(self):
return self.response.raise_for_status()

def get_cookies(self, url):
cookie = None
if self.client:
cookie = self.client._client.cookie_jar.filter_cookies(url)
return cookie

@property
def headers(self):
return self._headers
Expand Down