Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

some refactoring (mostly pep8)

  • Loading branch information...
commit 6fd074af5f1b9124231c6d40448582839339d274 1 parent 2df9033
Piotr Zalewa zalun authored
24 example/commands.py
View
@@ -1,5 +1,6 @@
"""Commands to run on the Marketplace API
"""
+
import sys
import json
@@ -14,6 +15,7 @@ def validate_manifest(auth, manifest_url):
'message': 'FAILED to issue validation. '
'Status code: %d' % response.status_code}
+
def is_manifest_valid(auth, manifest_id):
response = auth.is_manifest_valid(manifest_id)
if response is None:
@@ -26,6 +28,7 @@ def is_manifest_valid(auth, manifest_id):
return {'success': True,
'message': 'Your manifest is not valid:\n%s' % response}
+
def create(auth, manifest_id):
response = auth.create(manifest_id)
content = json.loads(response.content)
@@ -38,6 +41,7 @@ def create(auth, manifest_id):
return {'success': False,
'message': response.content}
+
def status(auth, app_id):
response = auth.status(app_id)
if response.status_code != 200:
@@ -49,7 +53,16 @@ def status(auth, app_id):
'message': '\n'.join(
['%s: %s' % (k, v) for k, v in content.items()])}
+
def update(auth, app_id):
+
+ def get_value(key, val):
+ variable = raw_input('%s (%s): ' % (key, val))
+ if key in truthy_keys and not variable and not val:
+ sys.stdout.write('This parameter is required.\n')
+ variable = get_value(key, val)
+ return variable
+
editable_keys = ['name', 'device_types', 'summary', 'support_email',
'homepage', 'categories', 'description', 'privacy_policy',
'support_url', 'payment_type']
@@ -62,12 +75,7 @@ def update(auth, app_id):
if key not in editable_keys:
del data[key]
sys.stderr.write('Please provide data, hit Enter for no change\n')
- def get_value(key, val):
- variable = raw_input('%s (%s): ' % (key, val))
- if key in truthy_keys and not variable and not val:
- sys.stdout.write('This parameter is required.\n')
- variable = get_value(key, val)
- return variable
+
for key, val in data.items():
if key in editable_keys:
variable = get_value(key, val)
@@ -84,6 +92,7 @@ def get_value(key, val):
return {'success': True,
'message': 'Your app has been updated'}
+
def add_screenshot(auth, app_id, filename):
response = auth.create_screenshot(app_id, filename)
if response.status_code != 201:
@@ -95,6 +104,7 @@ def add_screenshot(auth, app_id, filename):
'message': '\n'.join(
['%s: %s' % (k, v) for k, v in content.items()])}
+
def get_screenshot(auth, screenshot_id):
response = auth.get_screenshot(screenshot_id)
if response.status_code != 200:
@@ -106,6 +116,7 @@ def get_screenshot(auth, screenshot_id):
'message': '\n'.join(
['%s: %s' % (k, v) for k, v in content.items()])}
+
def del_screenshot(auth, screenshot_id):
response = auth.del_screenshot(screenshot_id)
if response.status_code != 204:
@@ -115,6 +126,7 @@ def del_screenshot(auth, screenshot_id):
return {'success': True,
'message': 'Screenshot deleted'}
+
def get_categories(auth):
response = auth.get_categories()
if response.status_code != 200:
16 example/config.py
View
@@ -1,15 +1,21 @@
"""Read config from environment variables
"""
+
from os import environ
CONSUMER_KEY = (environ['CONSUMER_KEY']
- if 'CONSUMER_KEY' in environ else 'consumer_key')
+ if 'CONSUMER_KEY' in environ
+ else 'consumer_key')
CONSUMER_SECRET = (environ['CONSUMER_SECRET']
- if 'CONSUMER_SECRET' in environ else 'consumer_secret')
+ if 'CONSUMER_SECRET' in environ
+ else 'consumer_secret')
MARKETPLACE_PORT = (environ['MARKETPLACE_PORT']
- if 'MARKETPLACE_PORT' in environ else 443)
+ if 'MARKETPLACE_PORT' in environ
+ else 443)
MARKETPLACE_DOMAIN = (environ['MARKETPLACE_DOMAIN']
- if 'MARKETPLACE_DOMAIN' in environ else 'marketplace.mozilla.org')
+ if 'MARKETPLACE_DOMAIN' in environ
+ else 'marketplace.mozilla.org')
MARKETPLACE_PROTOCOL = (environ['MARKETPLACE_PROTOCOL']
- if 'MARKETPLACE_PROTOCOL' in environ else 'https')
+ if 'MARKETPLACE_PROTOCOL' in environ
+ else 'https')
33 marketplace/client.py
View
@@ -4,6 +4,7 @@
For full spec please read Marketplace API documentation
https://github.com/mozilla/zamboni/blob/master/docs/topics/api.rst
"""
+
import json
import logging
import mimetypes
@@ -70,7 +71,7 @@ def validate_manifest(self, manifest_url):
# there is a bug request to make this synchronous on Marketplace side
# this will return the same as :method:`get_manifest_validation_result`
return self.conn.fetch('POST',
- self.url('validate'), {'manifest': manifest_url})
+ self.url('validate'), {'manifest': manifest_url})
def get_manifest_validation_result(self, manifest_id):
"""Check if the manifest is processed and if it's valid
@@ -84,7 +85,7 @@ def get_manifest_validation_result(self, manifest_id):
* validation - empty string if valid else error dict
"""
return self.conn.fetch('GET',
- self.url('validation_result') % manifest_id)
+ self.url('validation_result') % manifest_id)
def is_manifest_valid(self, manifest_id):
"""Check validation shortcut
@@ -116,7 +117,7 @@ def create(self, manifest_id):
* slug (string) unique name in marketplace
"""
return self.conn.fetch('POST', self.url('create'),
- {'manifest': '%s' % manifest_id})
+ {'manifest': '%s' % manifest_id})
def update(self, app_id, data):
"""Update app identified by app_id with data
@@ -140,18 +141,18 @@ def update(self, app_id, data):
* content (dict) or empty if successful
"""
assert ('name' in data
- and data['name']
- and 'summary' in data
- and 'categories' in data
- and data['categories']
- and 'support_email' in data
- and data['support_email']
- and 'device_types' in data
- and data['device_types']
- and 'payment_type' in data
- and data['payment_type']
- and 'privacy_policy' in data
- and data['privacy_policy'])
+ and data['name']
+ and 'summary' in data
+ and 'categories' in data
+ and data['categories']
+ and 'support_email' in data
+ and data['support_email']
+ and 'device_types' in data
+ and data['device_types']
+ and 'payment_type' in data
+ and data['payment_type']
+ and 'privacy_policy' in data
+ and data['privacy_policy'])
return self.conn.fetch('PUT', self.url('app') % app_id, data)
def status(self, app_id):
@@ -209,7 +210,7 @@ def del_screenshot(self, screenshot_id):
* status_code (int) 204 if successful
"""
return self.conn.fetch('DELETE',
- self.url('screenshot') % screenshot_id)
+ self.url('screenshot') % screenshot_id)
def get_categories(self):
"""Get all categories from Marketplae
2  marketplace/connection.py
View
@@ -1,6 +1,6 @@
""" Provide connection with Marketplace API
-
"""
+
import json
import logging
import time
24 marketplace/tests/test_client.py
View
@@ -1,7 +1,3 @@
-"""
-tests.testClient
-----------------
-"""
import json
import logging
import os
@@ -29,6 +25,7 @@
MARKETPLACE_DOMAIN = 'marketplace-dev.allizom.org'
MARKETPLACE_PROTOCOL = 'https'
+
class Response:
"""This is used to create a mock of response from API
"""
@@ -36,15 +33,16 @@ def __init__(self, status_code, content=None):
self.status_code = status_code
self.content = content
+
class TestClient(unittest.TestCase):
def setUp(self):
self.marketplace = marketplace.Client(
- domain=MARKETPLACE_DOMAIN,
- port=MARKETPLACE_PORT,
- protocol=MARKETPLACE_PROTOCOL,
- consumer_key='consumer_key',
- consumer_secret='consumer_secret')
+ domain=MARKETPLACE_DOMAIN,
+ port=MARKETPLACE_PORT,
+ protocol=MARKETPLACE_PROTOCOL,
+ consumer_key='consumer_key',
+ consumer_secret='consumer_secret')
def tearDown(self):
requests.post = OLD_POST
@@ -148,7 +146,7 @@ def test_update(self):
'summary': 'Changed Summary',
'categories': ['Business', 'Game'],
'support_email': 'john@doe.com',
- 'device_types': ['phone',],
+ 'device_types': ['phone', ],
'privacy_policy': 'any',
'payment_type': 'free'})
eq_(response.status_code, 202)
@@ -157,7 +155,7 @@ def test_update(self):
def test_status(self):
resp = {'categories': ['Business', 'Game'],
'description': None,
- 'device_types': ['phone',],
+ 'device_types': ['phone', ],
'homepage': None,
'id': 1,
'manifest': '0a650e5e4c434b5cb60c5495c0d88a89',
@@ -189,7 +187,7 @@ def test_delete_not_implemented(self):
def test_add_screenshot(self):
path = lambda *a: os.path.join(
- os.path.dirname(os.path.abspath(__file__)), *a)
+ os.path.dirname(os.path.abspath(__file__)), *a)
resp = {'filetype': 'image/png',
'thumbnail_url': 'https://marketplace-dev-cdn.allizom.org/img/'
'uploads/previews/thumbs/71/71761.png?'
@@ -212,7 +210,7 @@ def test_add_screenshot(self):
eq_(data['file']['type'], 'image/jpeg')
# create a screenshot with a jpeg image and not default position
self.marketplace.create_screenshot(123,
- path('mozilla.jpg'), position=2)
+ path('mozilla.jpg'), position=2)
data = json.loads(requests.post.call_args[1]['data'])
eq_(data['position'], 2)
eq_(data['file']['type'], 'image/jpeg')
19 marketplace/tests/test_connection.py
View
@@ -1,7 +1,3 @@
-"""
-tests.testClient
-----------------
-"""
import json
import logging
import unittest
@@ -29,6 +25,7 @@ def __init__(self, status_code, content=None):
self.status_code = status_code
self._content = content
+
class TestClient(unittest.TestCase):
def setUp(self):
@@ -44,30 +41,30 @@ def test_raising_on_httperror(self):
resp = {"reason": "Error with OAuth headers"}
requests.post = Mock(return_value=Response(401, json.dumps(resp)))
self.assertRaises(requests.exceptions.HTTPError, self.conn.fetch,
- 'POST', 'http://example.com/', {})
+ 'POST', 'http://example.com/', {})
resp = "<html><title>404</title><body><p>Error 404</p></body></html>"
requests.post = Mock(return_value=Response(404, resp))
self.assertRaises(requests.exceptions.HTTPError, self.conn.fetch,
- 'POST', 'http://example.com/', {})
+ 'POST', 'http://example.com/', {})
def test_raising_on_unexpected(self):
resp = {"reason": "Error with OAuth headers"}
requests.post = Mock(return_value=Response(204, json.dumps(resp)))
self.assertRaises(requests.exceptions.HTTPError, self.conn.fetch,
- 'POST', 'http://example.com/', {}, 201)
+ 'POST', 'http://example.com/', {}, 201)
def test_error_reason_json(self):
resp = {"reason": "message"}
self.assertEquals(
- Connection._get_error_reason(Response(204,
- json.dumps(resp))), resp['reason'])
+ Connection._get_error_reason(Response(204, json.dumps(resp))),
+ resp['reason'])
def test_error_reason_text(self):
resp = "<html><title>404</title><body><p>Error 404</p></body></html>"
self.assertEquals(
- Connection._get_error_reason(Response(204, resp)),
- resp)
+ Connection._get_error_reason(Response(204, resp)),
+ resp)
def test_set_consumer(self):
assert isinstance(self.conn.consumer, oauth.Consumer)
Please sign in to comment.
Something went wrong with that request. Please try again.