Skip to content

Commit

Permalink
Merge pull request #3 from wy-z/develop
Browse files Browse the repository at this point in the history
[core] Add 'req_opts' option
  • Loading branch information
wy-z authored Aug 17, 2020
2 parents 344f413 + f8d51d2 commit fe591b0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
39 changes: 21 additions & 18 deletions requests_openapi/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@ class Operation(object):
_method: str
_spec: typing.Dict[str, typing.Any]
_requestor: Requestor
_req_opts: typing.Dict[str, typing.Any]
_server: Server

_call: typing.Optional[typing.Callable] = None

def __init__(self, path, method, spec, requestor=None, server=None):
def __init__(self, path, method, spec, requestor=None, req_opts={}, server=None):
self._path = path
self._method = method
self._spec = spec
self._requestor = requestor
self._req_opts = req_opts
self._server = server

@property
Expand Down Expand Up @@ -121,6 +123,8 @@ def f(**kwargs):
kwargs.setdefault("params", {}).update(params)
kwargs.setdefault("headers", {}).update(headers)
kwargs.setdefault("cookies", {}).update(cookies)
for k, v in self._req_opts.items():
kwargs.setdefault(k, v)
return self._requestor.request(
self._method, self.url(**path_params), **kwargs
)
Expand Down Expand Up @@ -155,12 +159,14 @@ def load_spec_from_file(file_path):

class Client(object):
_requestor: Requestor
_req_opts: typing.Dict[str, typing.Any]
_server: Server
_operations: typing.Dict[str, typing.Any]
_spec: typing.Dict[str, typing.Any]

def __init__(self, requestor=None, server=None):
def __init__(self, requestor=None, server=None, req_opts={}):
self._requestor = requestor or requests.Session()
self._req_opts = req_opts
self._server = server
self._operations = {}
self._spec = {}
Expand Down Expand Up @@ -216,27 +222,24 @@ def _collect_operations(self):
)
continue

op = Operation(
path,
method,
op_spec,
requestor=self._requestor,
req_opts=self._req_opts,
server=self._server,
)
if operation_id not in self._operations:
self._operations[operation_id] = Operation(
path,
method,
op_spec,
requestor=self._requestor,
server=self._server,
)
self._operations[operation_id] = op
else:
logging.warn(
f"multiple '{operation_id}' found , operation ID should be unique"
)
v = self._operations[operation_id]
if type(v) is not list:
self._operations[operation_id] = [v]
self._operations[operation_id].append(
Operation(
path,
method,
op_spec,
requestor=self._requestor,
server=self._server,
)
)
self._operations[operation_id].append(op)

def load_spec_from_url(self, url):
spec = load_spec_from_url(url)
Expand Down
2 changes: 1 addition & 1 deletion requests_openapi/requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class Requestor(abc.ABC):
@abc.abstractmethod
def request(self, method, url, params={}, headers={}, cookies={}, *kwargs):
def request(self, method, url, params={}, headers={}, cookies={}, **kwargs):
pass


Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def read(filename):
return fd.read()


requirements = ["requests", "pyyaml"]
requirements_dev = ["tox", "pytest"]

setup(
name="requests-openapi",
url="https://github.com/wy-z/requests-openapi",
Expand All @@ -22,7 +25,8 @@ def read(filename):
use_scm_version=True,
setup_requires=["setuptools_scm"],
packages=find_packages(exclude=("tests",)),
install_requires=["requests", "pyyaml"],
install_requires=requirements,
extras_require={"dev": requirements_dev},
classifiers=[
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
Expand Down
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[tox]
nvlist = py3

testenv]
[testenv]
ommands = py.test requests_openapi
eps = pytest

testenv:black]
[testenv:black]
deps =
black
commands =
Expand Down

0 comments on commit fe591b0

Please sign in to comment.