Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Usabilla API - Python Client
# Usabilla API - Python Client

[![Build Status](https://travis-ci.org/usabilla/api-python.svg?branch=master)](https://travis-ci.org/usabilla/api-python)
[![PyPI version](https://badge.fury.io/py/usabilla-api.svg)](https://badge.fury.io/py/usabilla-api)
Expand All @@ -7,7 +7,8 @@
The Usabilla Python client allows users to access data from their Usabilla accounts.
It makes use of the API to request the following products and resources:

###Usabilla for Websites
### Usabilla for Websites

- Buttons
- Feedback items
- Campaigns
Expand All @@ -16,13 +17,17 @@ It makes use of the API to request the following products and resources:
- In-Page widgets
- In-Page feedback

###Usabilla for Email
- Buttons
- Feedback items
### Usabilla for Email

- Buttons
- Feedback items

### Usabilla for Apps

###Usabilla for Apps
- Apps
- Feedback items
- Apps
- Feedback items
- Campaigns
- Campaign results

For more information on resources, authorization and available API calls, please visit out [documentation](https://usabilla.com/api).

Expand All @@ -35,15 +40,15 @@ pip install usabilla-api
```

## Examples
The example folder contains an example of the client, which gives an idea how the client can be used and what is possible.

The example folder contains an example of the client, which gives an idea how the client can be used and what is possible.

###Iterators
### Iterators

When working with the <code>limit</code> parameters (default value is **100**) you can request resources using the <code>item_iterator()</code> function.
The API returns data in pages. This function returns a [Generator](https://wiki.python.org/moin/Generators) which
traverses these pages for you and yields each result in the current page before retrieving the next page.

##Support
## Support

The Usabilla Python Client API is maintained by Usabilla Development Team. Everyone is encouraged to file bug reports, feature requests, and pull requests through GitHub. This input is critical and will be carefully considered, but we can’t promise a specific resolution or time frame for any request. For more information please email our Support Team at [email protected]
23 changes: 21 additions & 2 deletions example/examples_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Set the limit of buttons to retrieve to 1
if False:
api.set_query_parameters({'limit': 1})
api.set_query_parameters({'limit': 1})

# Set a limit for last 7 days
if False:
Expand All @@ -25,5 +25,24 @@
print first_form['name']

# Get the feedback of the first app form
feedback = api.get_resource(api.SCOPE_LIVE, api.PRODUCT_APPS, api.RESOURCE_FEEDBACK, first_form['id'], iterate=True)
feedback = api.get_resource(
api.SCOPE_LIVE,
api.PRODUCT_APPS,
api.RESOURCE_FEEDBACK,
first_form['id'],
iterate=True)
print len([item for item in feedback])

# Campaigns for apps
app_campaigns = api.get_resource(
api.SCOPE_LIVE,
api.PRODUCT_APPS,
api.RESOURCE_CAMPAIGN)
first_campaign = app_campaigns['items'][0]
responses = api.get_resource(
api.SCOPE_LIVE,
api.PRODUCT_APPS,
api.RESOURCE_CAMPAIGN_RESULT,
first_campaign['id'],
iterate=True)
print len([item for item in responses])
35 changes: 26 additions & 9 deletions tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import sys
import unittest
import usabilla as ub
Expand All @@ -6,10 +7,9 @@
from unittest import TestCase, main as unittest_main


import logging

logging.basicConfig(level=logging.DEBUG)


class TestCredentials(TestCase):

def setUp(self):
Expand Down Expand Up @@ -66,18 +66,15 @@ def test_client_constants(self):
self.assertEqual(self.client.host_protocol, 'https://')
self.assertEqual('',self.client.query_parameters)


def test_sign_key(self):
signed_key = self.client.sign(self.secret_key.encode('utf-8'), 'usbl1_request'.encode('utf-8'))
self.assertEqual(signed_key, b"&-\x88\x80Z9\xe8Pnvx\xe4S\xeeZ\x9fG\xc5\xf7g\x11|\xc1\xaa~q(\xef\xaf\x95\xc0\xac")


def test_get_signature_key(self):
datestamp = '20150115'
signing_key = self.client.get_signature_key(self.secret_key, datestamp)
self.assertEqual(signing_key, b"\x15\x8d\xd7U\xceG\xdeH\x8aHwU\xf5qg\xae\xd4Z\x19`\xedM\x80\x87\x97V\xbf\xe9pw\xaa\xae")


def test_query_parameters(self):
params = {'limit': 1}
self.client.set_query_parameters(params)
Expand All @@ -88,12 +85,32 @@ def test_query_parameters(self):

def test_check_resource_validity(self):
with self.assertRaises(ub.GeneralError):
self.client.check_resource_validity('nonexisting', 'nonexisting', 'nonexisting')
self.client.check_resource_validity(
'nonexisting',
'nonexisting',
'nonexisting')
with self.assertRaises(ub.GeneralError):
self.client.check_resource_validity('live', 'nonexisting', 'nonexisting')
self.client.check_resource_validity(
'live',
'nonexisting',
'nonexisting')
with self.assertRaises(ub.GeneralError):
self.client.check_resource_validity('live', 'websites', 'nonexisting')
self.assertEqual(self.client.check_resource_validity('live', 'websites', 'button'), '/live/websites/button')
self.client.check_resource_validity(
'live',
'websites',
'nonexisting')
self.assertEqual(
self.client.check_resource_validity('live', 'websites', 'button'),
'/live/websites/button')
self.assertEqual(
self.client.check_resource_validity('live', 'apps', 'campaign'),
'/live/apps/campaign')
self.assertEqual(
self.client.check_resource_validity(
'live',
'apps',
'campaign_result'),
'/live/apps/campaign/:id/results')

def test_handle_id(self):
url = '/live/websites/button/:id/feedback'
Expand Down
16 changes: 9 additions & 7 deletions usabilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ class APIClient(object):
'apps': {
'resources' : {
'app': '',
'feedback': '/:id/feedback'
'feedback': '/:id/feedback',
'campaign': '/campaign',
'campaign_result': '/campaign/:id/results'
}
}
}
Expand Down Expand Up @@ -237,7 +239,6 @@ def send_signed_request(self, scope):
request_url = self.host + scope + '?' + canonical_querystring
r = requests.get(self.host_protocol + request_url, headers=headers)


if r.status_code != 200:
return r
else:
Expand All @@ -260,14 +261,15 @@ def check_resource_validity(self, scope, product, resource):
"""
if scope not in self.resources['scopes'].keys():
raise GeneralError('invalid scope', 'Invalid scope name')
if product not in self.resources['scopes'][scope]['products'].keys():
found_scope = self.resources['scopes'][scope]
if product not in found_scope['products'].keys():
raise GeneralError('invalid product', 'Invalid product name')
if resource not in self.resources['scopes'][scope]['products'][product]['resources'].keys():
found_product = found_scope['products'][product]
if resource not in found_product['resources'].keys():
raise GeneralError('invalid resource', 'Invalid resource name')
found_resource = found_product['resources'][resource]

url = '/' + scope + '/' + product + self.resources['scopes'][scope]['products'][product]['resources'][resource]

return url
return '/%s/%s%s' % (scope, product, found_resource)

def handle_id(self, url, resource_id):
"""Replaces the :id pattern in the url
Expand Down