Skip to content

Integrate Requests library #24

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

Closed
wants to merge 5 commits into from
Closed
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
59 changes: 58 additions & 1 deletion openapi_core/wrappers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""OpenAPI core wrappers module"""
import warnings

from six.moves.urllib.parse import urljoin
from six.moves.urllib.parse import urljoin, urlparse, parse_qsl
from werkzeug.datastructures import ImmutableMultiDict


Expand Down Expand Up @@ -107,6 +107,45 @@ def mimetype(self):
return self.request.mimetype


class RequestsOpenAPIRequest(BaseOpenAPIRequest):
def __init__(self, request):
self.request = request
self.url = urlparse(request.url)

@property
def host_url(self):
return self.url.scheme + '://' + self.url.netloc

@property
def path(self):
return self.url.path

@property
def method(self):
return self.request.method.lower()

@property
def path_pattern(self):
return self.url.path

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think your implementation for the path_pattern is going to work for routes that have path parameters. The library does a dictionary lookup where it expects the path_pattern to equal the path in the spec. For example, consider the path '/pets/{petId}'. Your implementation will cause the library to try to look up something like '/pets/123' in the paths dict, which will cause the validation to fail because that's not the way the path is defined in the OAS spec.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be pushing a PR sometime today that should deal with the path_pattern problem. If accepted then your implementation will likely work correctly with my addition.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it's more complicated than I thought. I have something that would work for the path_pattern but it doesn't account for the path and query parameters.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if we have route pattern, this would not work. I think we should add route pattern when creating RequestsOpenAPIRequest object:

openapi_request = RequestsOpenAPIRequest(req, url_pattern='/pets/{petId}')

Currently I am working on it, could not say when it would ready...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if this should actually be a part of the library. Consider this.

We already have a regex-like pattern in the route:
/pets/{petId})
If we convert this into a true regex then we should be able to match against every possible pattern that this path can have:
/pets/(.*)
It makes sense to do this in the spec object rather than the request object because the spec contains the patterns of every possible path in the API. Thus we merely match the url against all possible paths, which is O(n) though n should be small, and pick the pattern that matches the raw url.

The problem then (and this is where I am at the moment) is that we must also extract the path path and query parameters. The query params should be able to be parsed easily. The path params require a bit more work since this is part of the validators, but it's definitely doable.

A second option is to pass the spec to the request object and let it figure out everything there. However, I feel it would be far more user friendly to simply be able to have the core library extract this information from a simpler request object since there are plenty or url parsing tools and it would make implementing request wrappers much easier in the future.


@property
def parameters(self):
return {
'path': self.url.path,
'query': ImmutableMultiDict(parse_qsl(self.url.query)),
'headers': self.request.headers,
'cookies': self.request.cookies,
}

@property
def body(self):
return self.request.data

@property
def mimetype(self):
return self.request.headers.get('content-type')


class BaseOpenAPIResponse(object):

body = NotImplemented
Expand Down Expand Up @@ -140,3 +179,21 @@ def status_code(self):
@property
def mimetype(self):
return self.response.mimetype


class RequestsOpenAPIResponse(BaseOpenAPIResponse):

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

@property
def data(self):
return self.response.text

@property
def status_code(self):
return self.response.status_code

@property
def mimetype(self):
return self.response.headers.get('content-type')