-
-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add registry support which enables different response matching and selection logic. Co-authored-by: Mark Story <[email protected]>
- Loading branch information
1 parent
476ad67
commit 3dccae8
Showing
8 changed files
with
229 additions
and
49 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class FirstMatchRegistry(object): | ||
def __init__(self): | ||
self._responses = [] | ||
|
||
@property | ||
def registered(self): | ||
return self._responses | ||
|
||
def reset(self): | ||
self._responses = [] | ||
|
||
def find(self, request): | ||
found = None | ||
found_match = None | ||
match_failed_reasons = [] | ||
for i, response in enumerate(self.registered): | ||
match_result, reason = response.matches(request) | ||
if match_result: | ||
if found is None: | ||
found = i | ||
found_match = response | ||
else: | ||
# Multiple matches found. Remove & return the first response. | ||
return self.registered.pop(found), match_failed_reasons | ||
else: | ||
match_failed_reasons.append(reason) | ||
return found_match, match_failed_reasons | ||
|
||
def add(self, response): | ||
self.registered.append(response) | ||
|
||
def remove(self, response): | ||
while response in self.registered: | ||
self.registered.remove(response) | ||
|
||
def replace(self, response): | ||
try: | ||
index = self.registered.index(response) | ||
except ValueError: | ||
raise ValueError( | ||
"Response is not registered for URL {}".format(response.url) | ||
) | ||
self.registered[index] = response |
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,17 @@ | ||
from typing import ( | ||
List, | ||
Tuple, | ||
) | ||
from requests.adapters import PreparedRequest | ||
from responses import BaseResponse | ||
|
||
class FirstMatchRegistry: | ||
_responses = List[BaseResponse] | ||
def __init__(self) -> None: ... | ||
@property | ||
def registered(self) -> List[BaseResponse]: ... | ||
def reset(self) -> None: ... | ||
def find(self, request: PreparedRequest) -> Tuple[BaseResponse, List[str]]: ... | ||
def add(self, response: BaseResponse) -> None: ... | ||
def remove(self, response: BaseResponse) -> None: ... | ||
def replace(self, response: BaseResponse) -> None: ... |
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
Oops, something went wrong.