Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

removed consumer dependency injection

some simple pylint fixes
  • Loading branch information...
commit 42ae14efabe26a295ee7d88a1081f5cb39718f06 1 parent 7dfddb0
Piotr Zalewa authored

Showing 1 changed file with 25 additions and 22 deletions. Show diff stats Hide diff stats

  1. +25 22 marketplace/connection.py
47 marketplace/connection.py
... ... @@ -1,3 +1,6 @@
  1 +""" Provide connection with Marketplace API
  2 +
  3 +"""
1 4 import json
2 5 import logging
3 6 import time
@@ -8,9 +11,13 @@
8 11
9 12 log = logging.getLogger('marketplace.%s' % __name__)
10 13
  14 +
11 15 class NotExpectedStatusCode(requests.exceptions.HTTPError):
  16 + """ Raise if status code returned from API is not the expected one
  17 + """
12 18 pass
13 19
  20 +
14 21 def _get_args(consumer):
15 22 """Provide a dict with oauth data
16 23 """
@@ -21,7 +28,11 @@ def _get_args(consumer):
21 28 oauth_timestamp=int(time.time()),
22 29 oauth_version='1.0')
23 30
  31 +
24 32 class Connection:
  33 + """ Keeps the consumer class and provides the way to connect to the
  34 + Marketplace API
  35 + """
25 36 signature_method = oauth.SignatureMethod_HMAC_SHA1()
26 37
27 38 def __init__(self, consumer_key, consumer_secret):
@@ -30,25 +41,16 @@ def __init__(self, consumer_key, consumer_secret):
30 41 def set_consumer(self, consumer_key, consumer_secret):
31 42 """Sets the consumer attribute
32 43 """
33   - self.consumer = self.get_consumer(consumer_key, consumer_secret)
34   -
35   - def get_consumer(self, consumer_key, consumer_secret):
36   - """Get the :class:`oauth.Consumer` instance with provided key and
37   - secret
38   - """
39   - return oauth.Consumer(consumer_key, consumer_secret)
  44 + self.consumer = oauth.Consumer(consumer_key, consumer_secret)
40 45
41   -
42   - def prepare_request(self, method, url, body='', consumer=None):
  46 + def prepare_request(self, method, url, body=''):
43 47 """Adds consumer and signs the request
44 48
45 49 :returns: headers of the signed request
46 50 """
47   - if not consumer:
48   - consumer = self.consumer
49 51 req = oauth.Request(method=method, url=url,
50   - parameters=_get_args(consumer))
51   - req.sign_request(self.signature_method, consumer, None)
  52 + parameters=_get_args(self.consumer))
  53 + req.sign_request(self.signature_method, self.consumer, None)
52 54
53 55 headers = req.to_header()
54 56 headers['Content-type'] = 'application/json'
@@ -61,22 +63,23 @@ def prepare_request(self, method, url, body='', consumer=None):
61 63
62 64 @staticmethod
63 65 def _get_error_reason(response):
  66 + """extract error reason from the response. It might be either
  67 + the 'reason' or the entire response
  68 + """
64 69 body = response.json
65 70 if body and 'reason' in body:
66 71 return body['reason']
67 72 return response.content
68 73
69   - def fetch(self, method, url, data=None, expected_status_code=None,
70   - consumer=None):
71   - kwargs = self.prepare_request(method, url, data, consumer)
  74 + def fetch(self, method, url, data=None, expected_status_code=None):
  75 + kwargs = self.prepare_request(method, url, data)
72 76 response = getattr(requests, method.lower())(url, **kwargs)
73 77 if response.status_code >= 400:
74 78 response.raise_for_status()
75   - if expected_status_code and response.status_code != expected_status_code:
76   - raise NotExpectedStatusCode(self._get_error_reason(response))
  79 + if (expected_status_code
  80 + and response.status_code != expected_status_code):
  81 + raise NotExpectedStatusCode(self._get_error_reason(response))
77 82 return response
78 83
79   - def fetch_json(self, method, url, data=None, expected_status_code=None,
80   - consumer=None):
81   - return self.fetch(method, url, data, consumer,
82   - expected_status_code).json()
  84 + def fetch_json(self, method, url, data=None, expected_status_code=None):
  85 + return self.fetch(method, url, data, expected_status_code).json()

0 comments on commit 42ae14e

Please sign in to comment.
Something went wrong with that request. Please try again.