8000 Merge pull request #87 from admire93/url-string · nirum-lang/nirum-python@88aeda2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 88aeda2

Browse files
authored
Merge pull request #87 from admire93/url-string
`url` now can be both `str` and `unicode`
2 parents 7969ed7 + a6f07be commit 88aeda2

File tree

3 files changed

+45
-6
lines changed

3 files changed

+45
-6
lines changed

CHANGES.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ Version 0.5.2
66

77
To be released.
88

9+
- ``url`` of ``nirum.rpc.Client`` and
10+
``method`` of ``nirum.rpc.Client.make_request``
11+
now can be both ``unicode`` and ``str`` on Python 2.7. [`#87`_]
12+
- ``nirum.rpc.Client`` had been an old-style class on Python 2, but now
13+
it became a new-style class also on Python 2. (As Python 3 has only new-style
14+
class, there's no change on Python 3.)
15+
16+
.. _#87: https://github.com/spoqa/nirum-python/pull/87
17+
918

1019
Version 0.5.1
1120
-------------

nirum/rpc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import json
88
import typing
99

10-
from six import integer_types, text_type
10+
from six import integer_types, string_types
1111
from six.moves import urllib
1212
from werkzeug.exceptions import HTTPException
1313
from werkzeug.http import HTTP_STATUS_CODES
@@ -313,7 +313,7 @@ def _raw_response(self, status_code, response_json, **kwargs):
313313
return WsgiResponse(content, status_code, headers, **kwargs)
314314

315315

316-
class Client:
316+
class Client(object):
317317

318318
def __init__(self, url, opener=urllib.request.build_opener()):
319319
self.url = url_endswith_slash(url)
@@ -375,9 +375,9 @@ def do_request(self, request_url, payload):
375375
repr(request_tuple)
376376
)
377377
http_method, request_url, headers, content = request_tuple
378-
if not isinstance(request_url, text_type):
378+
if not isinstance(request_url, string_types):
379379
raise TypeError(
380-
'`request_url` have to be instance of text. not {}'.format(
380+
'`request_url` have to be instance of string. not {}'.format(
381381
typing._type_repr(type(request_url))
382382
)
383383
)
@@ -393,9 +393,9 @@ def do_request(self, request_url, payload):
393393
typing._type_repr(type(content))
394394
)
395395
)
396-
if not isinstance(http_method, text_type):
396+
if not isinstance(http_method, string_types):
397397
raise TypeError(
398-
'`method` have to be instance of text. not {}'.format(
398+
'`method` have to be instance of string. not {}'.format(
399399
typing._type_repr(type(http_method))
400400
)
401401
)

tests/rpc_test.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ class MusicServiceTypeErrorImpl(nf.MusicService):
5454
get_music_by_artist_name = 1
5555

5656

57+
class MethodClient(Client):
58+
59+
def __init__(self, method, url, opener):
60+
self.method = method
61+
super(MethodClient, self).__init__(url, opener)
62+
63+
def make_request(self, _, request_url, headers, payload):
64+
return (
65+
self.method, request_url, headers,
66+
json.dumps(payload).encode('utf-8')
67+
)
68+
69+
5770
@mark.parametrize('impl, error_class', [
5871
(MusicServiceNameErrorImpl, InvalidNirumServiceMethodNameError),
5972
(MusicServiceTypeErrorImpl, InvalidNirumServiceMethodTypeError),
@@ -336,6 +349,23 @@ def test_client_ping():
336349
assert client.ping()
337350

338351

352+
@mark.parametrize(
353+
'url',
354+
[u'http://foobar.com/rpc/', 'http://foobar.com/rpc/']
355+
)
356+
def test_client_url(url):
357+
client = Client(url, MockOpener(url, MusicServiceImpl))
358+
assert client.ping()
359+
360+
361+
@mark.parametrize('method', [u'POST', 'POST'])
362+
def test_client_make_request_method_type(method):
363+
url = 'http://test.com'
364+
client = MethodClient(method, url,
365+
MockOpener(url, MusicServiceImpl))
366+
assert client.ping()
367+
368+
339369
@mark.parametrize('arity', [0, 1, 2, 3, 5])
340370
def test_client_make_request_arity_check(arity):
341371
class ExtendedClient(Client):

0 commit comments

Comments
 (0)
0