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

feat(core): add request override support #12

Merged
merged 4 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ runner.environments.update({'BASE_URL': 'http://127.0.0.1:5000'})
runner.environments.update({'PASSWORD': 'test', 'EMAIL': 'you@email.com'})
```

### Override Request Parameters

It may be useful to override request parameters at runtime. You can do this using the `request_overrides` option:

```python
headers = { "MyHeader": "Value" }
pp = PostPython('collections/tests.postman_collection.json', request_overrides={
'headers': headers
})
```

### AttributeError

Since `RequestMethods` and `get_request` does not really exists your intelligent IDE cannot help you.
Expand Down
13 changes: 11 additions & 2 deletions postpy2/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from copy import copy

import requests
from mergedeep import merge

from postpy2.extractors import extract_dict_from_raw_headers, extract_dict_from_headers, extract_dict_from_raw_mode_data, format_object, extract_dict_from_formdata_mode_data, exctact_dict_from_files

Expand All @@ -24,12 +25,13 @@ def load(self, postman_enviroment_file_path):


class PostPython:
def __init__(self, postman_collection_file_path):
def __init__(self, postman_collection_file_path, request_overrides=None):
with open(postman_collection_file_path, encoding='utf8') as postman_collection_file:
self.__postman_collection = json.load(postman_collection_file)

self.__folders = {}
self.environments = CaseSensitiveDict()
self.request_overrides = request_overrides
self.__load()

def __load(self):
Expand Down Expand Up @@ -108,9 +110,16 @@ def __init__(self, post_python, data):
self.request_kwargs['method'] = data['method']

def __call__(self, *args, **kwargs):

current_request_kwargs = copy(self.request_kwargs)

if self.post_python.request_overrides:
current_request_kwargs = merge(current_request_kwargs, self.post_python.request_overrides)

new_env = copy(self.post_python.environments)
new_env.update(kwargs)
formatted_kwargs = format_object(self.request_kwargs, new_env)

formatted_kwargs = format_object(current_request_kwargs, new_env)
return requests.request(**formatted_kwargs)

def set_files(self, data):
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pytest
parameterized
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
setuptools.setup(
name='postpy2',
packages=['postpy2'],
version='0.0.6',
version='0.0.7',
author='Martin Kapinos',
author_email='matkapi19@gmail.com',
description='A library to use postman collection V2 in python.',
Expand All @@ -17,7 +17,8 @@
keywords=['postman', 'rest', 'api'], # arbitrary keywords
install_requires=[
'requests',
'python-magic'
'python-magic',
'mergedeep'
],
classifiers=[
"Programming Language :: Python :: 3",
Expand Down
Empty file added tests/__init__.py
Empty file.
61 changes: 61 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
from unittest.mock import Mock, call, patch

from parameterized import parameterized

from postpy2.core import PostRequest


@parameterized.expand([
(
'with_request_override',
{'headers': {'RUNTIMEHEADER': 'RUNTIMEVALUE'}},
[
call(
headers={'myCollectionHeader': 'MyCollectionHeaderValue',
'RUNTIMEHEADER': 'RUNTIMEVALUE'},
method='POST',
url='raw_url')
]
),
(
'no_request_override',
None,
[
call(
headers={'myCollectionHeader': 'MyCollectionHeaderValue'},
method='POST',
url='raw_url')
]
),
(
'with_existing_keys',
{'headers': {'myCollectionHeader': 'RUNTIMEVALUE'}},
[
call(
headers={'myCollectionHeader': 'RUNTIMEVALUE' },
method='POST',
url='raw_url')
]
),
])
@patch('postpy2.core.requests.request')
def test_request_overrides(
name: str,
request_overrides,
expected_request,
mock_requests: Mock):

mock_post_python = Mock()
mock_post_python.request_overrides = request_overrides
mock_post_python.environments = {}

data = {
'name': 'name',
'url': {'raw': 'raw_url'},
'method': 'POST',
'header': [{'key': 'myCollectionHeader', 'value': 'MyCollectionHeaderValue'}]
}
post_request = PostRequest(mock_post_python, data)
post_request()

assert mock_requests.call_args_list == expected_request