Skip to content

Commit

Permalink
renaming classes
Browse files Browse the repository at this point in the history
- #80
- pyswagger.io.SwaggerRequest -> Request
- pyswagger.io.SwaggerResponse -> Response
  • Loading branch information
mission-liao committed Jul 2, 2016
1 parent c744b92 commit 7c5d26b
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pyswagger/contrib/client/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def request(self, req_and_resp, opt={}):
data=data
)

# convert to SwaggerResponse
# convert to Response
resp.apply_with(
status=r.status_code,
header=r.headers.items(),
Expand Down
12 changes: 6 additions & 6 deletions pyswagger/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,9 @@ def update_with(self, name, security_info):
def __call__(self, req):
""" apply security info for a request.
:param SwaggerRequest req: the request to be authorized.
:param Request req: the request to be authorized.
:return: the updated request
:rtype: SwaggerRequest
:rtype: Request
"""
if not req._security:
return req
Expand Down Expand Up @@ -549,7 +549,7 @@ def __init__(self, security=None):
def prepare_schemes(self, req):
""" make sure this client support schemes required by current request
:param pyswagger.io.SwaggerRequest req: current request object
:param pyswagger.io.Request req: current request object
"""

# fix test bug when in python3 scheme, more details in commint msg
Expand All @@ -563,10 +563,10 @@ def request(self, req_and_resp, opt):
""" preprocess before performing a request, usually some patching.
authorization also applied here.
:param req_and_resp: tuple of SwaggerRequest and SwaggerResponse
:type req_and_resp: (SwaggerRequest, SwaggerResponse)
:param req_and_resp: tuple of Request and Response
:type req_and_resp: (Request, Response)
:return: patched request and response
:rtype: SwaggerRequest, SwaggerResponse
:rtype: Request, Response
"""
req, resp = req_and_resp

Expand Down
17 changes: 10 additions & 7 deletions pyswagger/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
logger = logging.getLogger(__name__)


class SwaggerRequest(object):
class Request(object):
""" Request layer
"""
# TODO: make a new class for 'prepared' SwaggerRequest
# TODO: make a new class for 'prepared' Request

# option: url_netloc, replace netloc part in url, useful
# when testing a set of Swagger APIs locally.
Expand Down Expand Up @@ -145,9 +145,9 @@ def _patch(self, opt={}):
""" private function to patch this request. This function
could be called before/after preparation.
:param dict opt: options, used options would be popped. Refer to SwaggerRequest.opt_* for details.
:param dict opt: options, used options would be popped. Refer to Request.opt_* for details.
"""
opt_netloc = opt.pop(SwaggerRequest.opt_url_netloc, None)
opt_netloc = opt.pop(Request.opt_url_netloc, None)
if opt_netloc:
scheme, netloc, path, params, query, fragment = six.moves.urllib.parse.urlparse(self.__url)
self.__url = six.moves.urllib.parse.urlunparse(
Expand All @@ -162,7 +162,7 @@ def prepare(self, scheme='http', handle_files=True, encoding='utf-8'):
:param str scheme: scheme used in this request
:param bool handle_files: False to skip multipart/form-data encoding
:param str encoding: encoding for body content.
:rtype: SwaggerRequest
:rtype: Request
"""

# combine path parameters into path
Expand Down Expand Up @@ -297,7 +297,7 @@ def _security(self):
return self.__op.security


class SwaggerResponse(object):
class Response(object):
""" Response layer
"""

Expand Down Expand Up @@ -332,7 +332,7 @@ def apply_with(self, status=None, raw=None, header=None):
:param str raw: body content
:param dict header: header section
:return: return self for chaining
:rtype: SwaggerResponse
:rtype: Response
"""

if status != None:
Expand Down Expand Up @@ -412,3 +412,6 @@ def header(self):
"""
return self.__header


SwaggerRequest = Request
SwaggerResponse = Response
5 changes: 3 additions & 2 deletions pyswagger/spec/v2_0/objects.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import absolute_import
from ..base import BaseObj, FieldMeta
from ...utils import final
from ...io import SwaggerRequest, SwaggerResponse
from ...io import Request
from ...io import Response as _Response
from ...primitives import Array
import six
import copy
Expand Down Expand Up @@ -287,7 +288,7 @@ def _convert_parameter(p):
raise ValueError('Unknown parameters: {0}'.format(unknown))

return \
SwaggerRequest(op=self, params=params), SwaggerResponse(self)
Request(op=self, params=params), _Response(self)


class PathItem(six.with_metaclass(FieldMeta, BaseObj_v2_0)):
Expand Down
2 changes: 1 addition & 1 deletion pyswagger/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _hook(url):
url = 'test.com/api/v1'
app = App.load('https://'+url, url_load_hook=_hook)
app.prepare()
# try to make a SwaggerRequest and verify its url
# try to make a Request and verify its url
req, _ = app.s('t1').get()
self.assertEqual(req.url, '//test.com/t1')
self.assertEqual(req.schemes, ['https'])
Expand Down
12 changes: 6 additions & 6 deletions pyswagger/tests/v1_2/test_io.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from pyswagger import App, errs
from ..utils import get_test_data_folder
from pyswagger.primitives import Model, Array
from pyswagger.io import SwaggerRequest
from pyswagger.io import Request
import unittest
import json


app = App._create_(get_test_data_folder(version='1.2', which='wordnik'))


class SwaggerRequest_Pet_TestCase(unittest.TestCase):
""" test SwaggerRequest from Operation's __call__ """
class Request_Pet_TestCase(unittest.TestCase):
""" test Request from Operation's __call__ """

def test_updatePet(self):
""" Pet.updatePet """
Expand Down Expand Up @@ -162,7 +162,7 @@ def test_opt_url_netloc(self):
req, _ = app.op['getPetById'](petId=100)
req.prepare()

req._patch({SwaggerRequest.opt_url_netloc: 'localhost:9001'})
req._patch({Request.opt_url_netloc: 'localhost:9001'})
self.assertEqual(req.url, 'http://localhost:9001/api/pet/100')
self.assertEqual(req.path, '/api/pet/100')

Expand All @@ -171,8 +171,8 @@ def test_uploadFile(self):
# TODO: implement File upload


class SwaggerResponse_TestCase(unittest.TestCase):
""" test SwaggerResponse from Pet's Operation's __call__ """
class Response_TestCase(unittest.TestCase):
""" test Response from Pet's Operation's __call__ """

def test_updatePet(self):
""" Pet.updatePet """
Expand Down
16 changes: 8 additions & 8 deletions pyswagger/tests/v2_0/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import os


class SwaggerResponseTestCase(unittest.TestCase):
""" test SwaggerResponse """
class ResponseTestCase(unittest.TestCase):
""" test Response """

@classmethod
def setUpClass(kls):
Expand All @@ -18,29 +18,29 @@ def test_response_schema_with_status(self):
""" make sure schema works as expected """

# make sure exception raised
resp = io.SwaggerResponse(self.app.s('/resp').get)
resp = io.Response(self.app.s('/resp').get)
self.assertRaises(Exception, resp.apply_with, None, 'some string')

# test for 'default'
resp = io.SwaggerResponse(self.app.s('/resp').get)
resp = io.Response(self.app.s('/resp').get)
resp.apply_with(0, 'test string', {'content-type': 'text/plain'})
self.assertEqual(resp.status, 0)
self.assertEqual(resp.raw, 'test string')
self.assertEqual(resp.data, 'test string')

# test for specific status code
r = '{"id": 0, "message": "test string 2"}'
resp = io.SwaggerResponse(self.app.s('/resp').get)
resp = io.Response(self.app.s('/resp').get)
resp.apply_with(404, r)
self.assertEqual(resp.status, 404)
self.assertEqual(resp.raw, r)
self.assertTrue(isinstance(resp.data, primitives.Model))

def test_error_only_status(self):
""" it's legal for a Swagger without any successful status Response object.
in this case, users need to access response via SwaggerResponse.raw
in this case, users need to access response via Response.raw
"""
resp = io.SwaggerResponse(self.app.s('/resp').post)
resp = io.Response(self.app.s('/resp').post)

# make sure we are ok when no matching status and without a 'default'
resp.apply_with(200, 'some data')
Expand All @@ -63,7 +63,7 @@ def test_raw_body_only(self):
""" an option skip passing body byte stream to swagger-primitive-factory
"""
r = '{"id": 0, "message": "test string 2"}'
resp = io.SwaggerResponse(self.app.s('/resp').get)
resp = io.Response(self.app.s('/resp').get)
resp.raw_body_only = True
resp.apply_with(404, r)
self.assertEqual(resp.status, 404)
Expand Down
4 changes: 2 additions & 2 deletions pyswagger/tests/v2_0/test_prim.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def test_read_only(self):
op = self.app.s('/k').post
self.assertRaises(Exception, op, p1=dict(protected=1))

resp = io.SwaggerResponse(op)
resp = io.Response(op)
resp.apply_with(0, '{"protected":1}') # allowed


Expand Down Expand Up @@ -262,7 +262,7 @@ def test_multi_level_array(self):

def test_header_in_response(self):
""" header in response """
resp = io.SwaggerResponse(self.app.s('/t').get)
resp = io.Response(self.app.s('/t').get)

resp.apply_with(status=200, raw=None, header=dict(
test='1|2,3|4,5|6 7|8,9|10 11|12,13|14'
Expand Down

0 comments on commit 7c5d26b

Please sign in to comment.