Skip to content
This repository was archived by the owner on Sep 28, 2023. It is now read-only.

Commit e53f5c8

Browse files
committed
Merge pull request #1 from optimizely/aharris/refactor-code
Refactor to use client
2 parents 11740ce + c8f90e3 commit e53f5c8

11 files changed

Lines changed: 268 additions & 902 deletions

File tree

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,17 @@ Usage
1010
Set your API key and you're ready to go!
1111
```python
1212
>>> import optimizely
13-
>>> optimizely.api_key = 'abcdefghijklmnopqrstuvwxyz:123456'
13+
>>> client = optimizely.Client('abcdefghijklmnopqrstuvwxyz:123456')
1414

15-
>>> projects = optimizely.Project.list()
15+
>>> projects = client.Projects.get()
1616
>>> projects
17-
[<Project object with ID: 12345>]
17+
# [<optimizely.resource.Project object at 0x000000000>, <optimizely.resource.Project object at 0x000000010>]
1818

1919
>>> my_project = projects[0]
2020
>>> my_project.project_name
21-
u'My Project'
21+
# 'My Project'
2222

23-
>>> my_project = optimizely.Project.update(12345, {'project_name': 'My Project (Updated)'})
24-
>>> my_project.project_name
25-
u'My Project (Updated)'
23+
>>> my_project.project_name = 'My Project (Updated)'
24+
>>> my_project.save()
2625

2726
```

optimizely/__init__.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
# Authors:
44
# Andy Harris <andy.harris@optimizely.com>
55

6-
# configuration variables
7-
api_key = None
8-
api_base = 'https://www.optimizelyapis.com/experiment/v1/'
6+
# Client
7+
from optimizely.client import *
98

109
# Resources
11-
from optimizely.resource import Project, Experiment, Result, Variation, Goal, Audience
10+
from optimizely.resource import *
1211

1312
# Errors
14-
from optimizely.error import (
15-
OptimizelyError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, TooManyRequestsError,
16-
ServiceUnavailableError, InvalidIDError)
13+
from optimizely.error import *

optimizely/api_requester.py

Lines changed: 0 additions & 41 deletions
This file was deleted.

optimizely/client.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
__all__ = ['Client']
2+
3+
import urlparse
4+
import requests
5+
6+
from optimizely import error
7+
from optimizely import resource
8+
9+
BASE_URL = 'https://www.optimizelyapis.com/experiment/v1/'
10+
11+
12+
class Client(object):
13+
ALLOWED_REQUESTS = ['get', 'post', 'put', 'delete']
14+
15+
def __init__(self, api_key, api_base=BASE_URL):
16+
# set API information
17+
self.api_key = api_key
18+
self.api_base = api_base
19+
20+
# instantiate resource generators for the relevant API resources
21+
self.Projects = resource.ResourceGenerator(client=self, resource=resource.Project)
22+
self.Experiments = resource.ResourceGenerator(client=self, resource=resource.Experiment)
23+
self.Variations = resource.ResourceGenerator(client=self, resource=resource.Variation)
24+
self.Goals = resource.ResourceGenerator(client=self, resource=resource.Goal)
25+
self.Audiences = resource.ResourceGenerator(client=self, resource=resource.Audience)
26+
self.Dimensions = resource.ResourceGenerator(client=self, resource=resource.Dimension)
27+
self.Schedules = resource.ResourceGenerator(client=self, resource=resource.Schedule)
28+
29+
def request(self, method, url_parts, headers=None, data=None):
30+
""" Method for making requests to the Optimizely API
31+
"""
32+
if method in self.ALLOWED_REQUESTS:
33+
# add request token header
34+
headers = headers or {}
35+
headers.update({'Token': self.api_key})
36+
37+
# make request and return parsed response
38+
url = urlparse.urljoin(self.api_base, '/'.join([str(url_part) for url_part in url_parts]))
39+
return self.parse_response(getattr(requests, method)(url, headers=headers, data=data))
40+
else:
41+
raise error.BadRequestError('%s is not a valid request type.' % method)
42+
43+
@staticmethod
44+
def parse_response(resp):
45+
""" Method to parse response from the Optimizely API and return results as JSON. Errors are thrown for various
46+
errors that the API can throw.
47+
"""
48+
if resp.status_code in [200, 201, 202]:
49+
return resp.json()
50+
elif resp.status_code == 204:
51+
return None
52+
elif resp.status_code == 400:
53+
raise error.BadRequestError(resp.json().get('message'))
54+
elif resp.status_code == 401:
55+
raise error.UnauthorizedError(resp.json().get('message'))
56+
elif resp.status_code == 403:
57+
raise error.ForbiddenError(resp.json().get('message'))
58+
elif resp.status_code == 404:
59+
raise error.NotFoundError(resp.json().get('message'))
60+
elif resp.status_code == 429:
61+
raise error.TooManyRequestsError(resp.json().get('message'))
62+
elif resp.status_code == 503:
63+
raise error.ServiceUnavailableError(resp.json().get('message'))
64+
else:
65+
raise error.OptimizelyError(resp.text)

optimizely/error.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
__all__ = [
2+
'OptimizelyError', 'BadRequestError', 'UnauthorizedError', 'ForbiddenError', 'NotFoundError',
3+
'TooManyRequestsError', 'ServiceUnavailableError', 'InvalidIDError']
4+
5+
16
class OptimizelyError(Exception):
27
""" General exception for all Optimizely Experiments API related issues."""
38
pass

0 commit comments

Comments
 (0)