Skip to content

Commit ad1f8d6

Browse files
committed
Merge pull request #115 from maxcountryman/serializable-redux
making service and session objects serializable
2 parents 519fe7b + 14c3534 commit ad1f8d6

File tree

6 files changed

+90
-6
lines changed

6 files changed

+90
-6
lines changed

rauth/service.py

+26
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def process_token_request(r, decoder, *args):
2626

2727

2828
class Service(object):
29+
__attrs__ = ['name', 'base_url', 'authorize_url']
30+
2931
def __init__(self, name, base_url, authorize_url):
3032
#: The service name, e.g. 'twitter'.
3133
self.name = name
@@ -36,6 +38,14 @@ def __init__(self, name, base_url, authorize_url):
3638
#: The authorization URL.
3739
self.authorize_url = authorize_url
3840

41+
def __getstate__(self):
42+
return dict((attr, getattr(self, attr, None)) for
43+
attr in self.__attrs__)
44+
45+
def __setstate__(self, state):
46+
for attr, value in state.items():
47+
setattr(self, attr, value)
48+
3949

4050
class OAuth1Service(Service):
4151
'''
@@ -116,6 +126,12 @@ class OAuth1Service(Service):
116126
to :class:`rauth.HmacSha1Signature <HmacSha1Signature>`
117127
:type signature_obj: :class:`SignatureMethod`
118128
'''
129+
__attrs__ = Service.__attrs__ + ['consumer_key',
130+
'consumer_secret',
131+
'request_token_url',
132+
'access_token_url',
133+
'session_obj']
134+
119135
def __init__(self,
120136
consumer_key,
121137
consumer_secret,
@@ -396,6 +412,11 @@ class OAuth2Service(Service):
396412
:class:`OAuth2Session`
397413
:type session_obj: :class:`rauth.Session`
398414
'''
415+
__attrs__ = Service.__attrs__ + ['client_id',
416+
'client_secret',
417+
'access_token_url',
418+
'session_obj']
419+
399420
def __init__(self,
400421
client_id,
401422
client_secret,
@@ -557,6 +578,11 @@ class OflyService(Service):
557578
`rauth.OflySession`
558579
:type session_obj: :class:`rauth.Session`
559580
'''
581+
__attrs__ = Service.__attrs__ + ['app_id',
582+
'app_secret',
583+
'user_id',
584+
'session_obj']
585+
560586
def __init__(self,
561587
app_id,
562588
app_secret,

rauth/session.py

+16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626

2727
class RauthSession(Session):
28+
__attrs__ = Session.__attrs__ + ['service']
29+
2830
def __init__(self, service):
2931
#: A back reference to a service wrapper, if we're using one.
3032
self.service = service
@@ -88,6 +90,12 @@ class OAuth1Session(RauthSession):
8890
`None`.
8991
:type service: :class:`rauth.Service`
9092
'''
93+
__attrs__ = RauthSession.__attrs__ + ['consumer_key',
94+
'consumer_secret',
95+
'access_token',
96+
'access_token_secret',
97+
'signature']
98+
9199
VERSION = '1.0'
92100

93101
def __init__(self,
@@ -298,6 +306,10 @@ class OAuth2Session(RauthSession):
298306
`None`.
299307
:type service: :class:`rauth.Service`
300308
'''
309+
__attrs__ = RauthSession.__attrs__ + ['client_id',
310+
'client_secret',
311+
'access_token']
312+
301313
def __init__(self,
302314
client_id,
303315
client_secret,
@@ -387,6 +399,10 @@ class OflySession(RauthSession):
387399
`None`.
388400
:type service: :class:`rauth.Service`
389401
'''
402+
__attrs__ = RauthSession.__attrs__ + ['app_id',
403+
'app_secret',
404+
'user_id']
405+
390406
def __init__(self,
391407
app_id,
392408
app_secret,

tests/test_service.py

+8
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from datetime import datetime
1313

1414
import json
15+
import pickle
1516

1617

1718
class MutableDatetime(datetime):
@@ -112,3 +113,10 @@ class RequestMixin(object):
112113
def test_request(self, method, kwargs):
113114
r = self.session.request(method, 'foo', **kwargs)
114115
self.assert_ok(r)
116+
117+
118+
class ServiceMixin(object):
119+
def test_serialize(self):
120+
saved = pickle.loads(pickle.dumps(self.service))
121+
for attr in self.service.__attrs__:
122+
self.assertEqual(getattr(saved, attr), getattr(self.service, attr))

tests/test_service_oauth1.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'''
88

99
from base import RauthTestCase
10-
from test_service import HttpMixin, RequestMixin
10+
from test_service import HttpMixin, RequestMixin, ServiceMixin
1111

1212
from rauth.service import OAuth1Service
1313
from rauth.session import OAUTH1_DEFAULT_TIMEOUT, OAuth1Session
@@ -25,9 +25,11 @@
2525
import requests
2626

2727
import json
28+
import pickle
2829

2930

30-
class OAuth1ServiceTestCase(RauthTestCase, RequestMixin, HttpMixin):
31+
class OAuth1ServiceTestCase(RauthTestCase, RequestMixin, ServiceMixin,
32+
HttpMixin):
3133
consumer_key = '000'
3234
consumer_secret = '111'
3335

@@ -306,3 +308,11 @@ def test_get_auth_session(self):
306308
self.response.content = resp
307309
s = self.service.get_auth_session('foo', 'bar')
308310
self.assertIsInstance(s, OAuth1Session)
311+
312+
def test_pickle_session(self):
313+
session = pickle.loads(pickle.dumps(self.session))
314+
315+
# Add the fake request back to the session
316+
session.request = self.fake_request
317+
r = session.request('GET', 'http://example.com/', header_auth=True)
318+
self.assert_ok(r)

tests/test_service_oauth2.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
'''
88

99
from base import RauthTestCase
10-
from test_service import HttpMixin, RequestMixin
10+
from test_service import HttpMixin, RequestMixin, ServiceMixin
1111

1212
from rauth.service import OAuth2Service
1313
from rauth.session import OAUTH2_DEFAULT_TIMEOUT, OAuth2Session
@@ -20,9 +20,11 @@
2020
import requests
2121

2222
import json
23+
import pickle
2324

2425

25-
class OAuth2ServiceTestCase(RauthTestCase, RequestMixin, HttpMixin):
26+
class OAuth2ServiceTestCase(RauthTestCase, RequestMixin, ServiceMixin,
27+
HttpMixin):
2628
client_id = '000'
2729
client_secret = '111'
2830
access_token = '123'
@@ -136,3 +138,11 @@ def test_get_auth_session(self):
136138
'access_token=123&expires_in=3600&refresh_token=456'
137139
s = self.service.get_auth_session()
138140
self.assertIsInstance(s, OAuth2Session)
141+
142+
def test_pickle_session(self):
143+
session = pickle.loads(pickle.dumps(self.session))
144+
145+
# Add the fake request back to the session
146+
session.request = self.fake_request
147+
r = session.request('GET', 'http://example.com/', bearer_auth=True)
148+
self.assert_ok(r)

tests/test_service_ofly.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from base import RauthTestCase
1010
from test_service import (FakeHexdigest, HttpMixin, MutableDatetime,
11-
RequestMixin)
11+
RequestMixin, ServiceMixin)
1212

1313
from rauth.service import OflyService
1414
from rauth.session import OFLY_DEFAULT_TIMEOUT, OflySession
@@ -22,8 +22,11 @@
2222

2323
import requests
2424

25+
import pickle
2526

26-
class OflyServiceTestCase(RauthTestCase, RequestMixin, HttpMixin):
27+
28+
class OflyServiceTestCase(RauthTestCase, RequestMixin, ServiceMixin,
29+
HttpMixin):
2730
app_id = '000'
2831
app_secret = '111'
2932

@@ -163,3 +166,14 @@ def test_request_with_bad_hash_meth(self):
163166
def test_get_auth_session(self):
164167
s = self.service.get_auth_session('foo')
165168
self.assertIsInstance(s, OflySession)
169+
170+
def test_pickle_session(self):
171+
session = pickle.loads(pickle.dumps(self.session))
172+
173+
# Add the fake request back to the session
174+
session.request = self.fake_request
175+
r = self.session.request('GET',
176+
'http://example.com/',
177+
user_id=self.user_id,
178+
hash_meth='md5')
179+
self.assert_ok(r)

0 commit comments

Comments
 (0)