-
Notifications
You must be signed in to change notification settings - Fork 792
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '3122-test-island-logout' into develop
- Loading branch information
Showing
6 changed files
with
178 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
envs/monkey_zoo/blackbox/island_client/i_monkey_island_requests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from abc import ABC, abstractmethod | ||
from typing import Dict | ||
|
||
|
||
class IMonkeyIslandRequests(ABC): | ||
@abstractmethod | ||
def get_token_from_server(self): | ||
pass | ||
|
||
@abstractmethod | ||
def login(self): | ||
pass | ||
|
||
@abstractmethod | ||
def get(self, url, data=None): | ||
pass | ||
|
||
@abstractmethod | ||
def post(self, url, data): | ||
pass | ||
|
||
@abstractmethod | ||
def put(self, url, data): | ||
pass | ||
|
||
@abstractmethod | ||
def put_json(self, url, json: Dict): | ||
pass | ||
|
||
@abstractmethod | ||
def post_json(self, url, json: Dict): | ||
pass | ||
|
||
@abstractmethod | ||
def patch(self, url, data: Dict): | ||
pass | ||
|
||
@abstractmethod | ||
def delete(self, url): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
envs/monkey_zoo/blackbox/island_client/reauthorizing_monkey_island_requests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import functools | ||
from http import HTTPStatus | ||
from typing import Dict | ||
|
||
from .i_monkey_island_requests import IMonkeyIslandRequests | ||
|
||
|
||
class ReauthorizingMonkeyIslandRequests(IMonkeyIslandRequests): | ||
def __init__(self, monkey_island_requests: IMonkeyIslandRequests): | ||
self.requests = monkey_island_requests | ||
|
||
def get_token_from_server(self): | ||
return self.requests.get_token_from_server() | ||
|
||
def login(self): | ||
self.requests.login() | ||
|
||
class _Decorators: | ||
@classmethod | ||
def refresh_auth_token(cls, request_function): | ||
@functools.wraps(request_function) | ||
def request_function_wrapper(self, *args, **kwargs): | ||
# noinspection PyArgumentList | ||
resp = request_function(self, *args, **kwargs) | ||
if resp.status_code == HTTPStatus.UNAUTHORIZED: | ||
self.requests.login() | ||
resp = request_function(self, *args, **kwargs) | ||
|
||
return resp | ||
|
||
return request_function_wrapper | ||
|
||
@_Decorators.refresh_auth_token | ||
def get(self, url, data=None): | ||
return self.requests.get(url, data=data) # noqa: DUO123 | ||
|
||
@_Decorators.refresh_auth_token | ||
def post(self, url, data): | ||
return self.requests.post(url, data=data) # noqa: DUO123 | ||
|
||
@_Decorators.refresh_auth_token | ||
def put(self, url, data): | ||
return self.requests.put(url, data=data) # noqa: DUO123 | ||
|
||
@_Decorators.refresh_auth_token | ||
def put_json(self, url, json: Dict): | ||
return self.requests.put_json(url, json=json) # noqa: DUO123 | ||
|
||
@_Decorators.refresh_auth_token | ||
def post_json(self, url, json: Dict): | ||
return self.requests.post_json(url, json=json) # noqa: DUO123 | ||
|
||
@_Decorators.refresh_auth_token | ||
def patch(self, url, data: Dict): | ||
return self.requests.patch(url, data=data) # noqa: DUO123 | ||
|
||
@_Decorators.refresh_auth_token | ||
def delete(self, url): | ||
return self.requests.delete(url) # noqa: DUO123 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters