sireno / oauth

Automatically exported from code.google.com/p/oauth
0 stars 0 forks source link

How about to change `from_consumer_and_token` to `classmethod` instead of `staticmethod`. #112

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi, i'm just humble developer.

I found some flaws in oauth.py. I tried to inherit the `OAuthRequest` to
add the custom method and properties, but the return instance from
`from_consumer_and_token` is not the inherited class, so under the current
`staticmethod`, `OAuthRequest` does not allow the inheritance, so I guess
it must be replaced by `classmethod`.

So, currently I changed the original `oauth.py` like this,
{{{
    @classmethod
    def from_consumer_and_token(cls, oauth_consumer, token=None,
http_method=HTTP_METHOD, http_url=None, parameters=None):

}}}

Thanks for your good work.

Original issue reported on code.google.com by spikeek...@gmail.com on 28 Jul 2009 at 10:55

GoogleCodeExporter commented 8 years ago

Original comment by leah.culver on 12 Aug 2009 at 9:21

GoogleCodeExporter commented 8 years ago
Has this been checked in yet?  

classmethods are the Right Thing to do because it allows you to instantiate 
overriden
subclasses much more easily instead of forcing child classes to override the 
method
or do elaborate hacks to get their own instances...

Original comment by ruib...@gmail.com on 25 Aug 2009 at 4:46

GoogleCodeExporter commented 8 years ago
Can we do this for all the staticmethod's inside the python bindings?

Original comment by ruib...@gmail.com on 25 Aug 2009 at 4:47

GoogleCodeExporter commented 8 years ago
>> Comment 3 by ruibalp, Today (4 minutes ago)
>> Can we do this for all the staticmethod's inside the python bindings?
yes, most of the `staticmethod` must be changed to use the `classmethod`. I 
attached 
my patch in below, check it.
{{{
Index: oauth.py
===================================================================
--- oauth.py    (revision 1116)
+++ oauth.py    (working copy)
@@ -134,20 +134,20 @@
             data['oauth_callback_confirmed'] = self.callback_confirmed
         return urllib.urlencode(data)

-    def from_string(s):
+    def from_string(cls, s):
         """ Returns a token from something like:
         oauth_token_secret=xxx&oauth_token=xxx
         """
         params = cgi.parse_qs(s, keep_blank_values=False)
         key = params['oauth_token'][0]
         secret = params['oauth_token_secret'][0]
-        token = OAuthToken(key, secret)
+        token = cls(key, secret)
         try:
             token.callback_confirmed = params['oauth_callback_confirmed'][0]
         except KeyError:
             pass # 1.0, no callback confirmed.
         return token
-    from_string = staticmethod(from_string)
+    from_string = classmethod(from_string)

     def __str__(self):
         return self.to_string()
@@ -262,7 +262,7 @@
         """Calls the build signature method within the signature method."""
         return signature_method.build_signature(self, consumer, token)

-    def from_request(http_method, http_url, headers=None, parameters=None,
+    def from_request(cls, http_method, http_url, headers=None, parameters=None,
             query_string=None):
         """Combines multiple parameter sources."""
         if parameters is None:
@@ -276,7 +276,7 @@
                 auth_header = auth_header[6:]
                 try:
                     # Get the parameters from the header.
-                    header_params = OAuthRequest._split_header(auth_header)
+                    header_params = cls._split_header(auth_header)
                     parameters.update(header_params)
                 except:
                     raise OAuthError('Unable to parse OAuth parameters from '
@@ -284,21 +284,21 @@

         # GET or POST query string.
         if query_string:
-            query_params = OAuthRequest._split_url_string(query_string)
+            query_params = cls._split_url_string(query_string)
             parameters.update(query_params)

         # URL parameters.
         param_str = urlparse.urlparse(http_url)[4] # query
-        url_params = OAuthRequest._split_url_string(param_str)
+        url_params = cls._split_url_string(param_str)
         parameters.update(url_params)

         if parameters:
-            return OAuthRequest(http_method, http_url, parameters)
+            return cls(http_method, http_url, parameters)

         return None
-    from_request = staticmethod(from_request)
+    from_request = classmethod(from_request)

-    def from_consumer_and_token(oauth_consumer, token=None,
+    def from_consumer_and_token(cls, oauth_consumer, token=None,
             callback=None, verifier=None, http_method=HTTP_METHOD,
             http_url=None, parameters=None):
         if not parameters:
@@ -308,7 +308,7 @@
             'oauth_consumer_key': oauth_consumer.key,
             'oauth_timestamp': generate_timestamp(),
             'oauth_nonce': generate_nonce(),
-            'oauth_version': OAuthRequest.version,
+            'oauth_version': cls.version,
         }

         defaults.update(parameters)
@@ -323,10 +323,10 @@
             # 1.0a support for callback in the request token request.
             parameters['oauth_callback'] = callback

-        return OAuthRequest(http_method, http_url, parameters)
-    from_consumer_and_token = staticmethod(from_consumer_and_token)
+        return cls(http_method, http_url, parameters)
+    from_consumer_and_token = classmethod(from_consumer_and_token)

-    def from_token_and_callback(token, callback=None, http_method=HTTP_METHOD,
+    def from_token_and_callback(cls, token, callback=None, 
http_method=HTTP_METHOD,
             http_url=None, parameters=None):
         if not parameters:
             parameters = {}
@@ -336,8 +336,8 @@
         if callback:
             parameters['oauth_callback'] = callback

-        return OAuthRequest(http_method, http_url, parameters)
-    from_token_and_callback = staticmethod(from_token_and_callback)
+        return cls(http_method, http_url, parameters)
+    from_token_and_callback = classmethod(from_token_and_callback)

     def _split_header(header):
         """Turn Authorization: header into parameters."""
@@ -647,4 +647,4 @@
     def build_signature(self, oauth_request, consumer, token):
         key, raw = self.build_signature_base_string(oauth_request, consumer,
             token)
-        return key
\ No newline at end of file
+        return key
}}}

Original comment by spikeek...@gmail.com on 25 Aug 2009 at 5:08