1+ import json
2+ import requests
3+
4+ api_key = ''
5+ api_base = 'https://www.optimizelyapis.com/experiment/v1/'
6+
7+
8+ class APIResource (object ):
9+ endpoint = ''
10+
11+ def __init__ (self ):
12+ raise NotImplementedError (self .__class__ .__name__ +
13+ ' contains only class methods and should not be instantiated.' )
14+
15+ @staticmethod
16+ def _parse_response (r ):
17+ if r .status_code in [200 , 201 , 202 , 204 ]:
18+ return r .json ()
19+ elif r .status_code == 400 :
20+ raise BadRequestError (r .text )
21+ elif r .status_code == 401 :
22+ raise UnauthorizedError (r .text )
23+ elif r .status_code == 403 :
24+ raise ForbiddenError (r .text )
25+ elif r .status_code == 404 :
26+ raise NotFoundError (r .text )
27+ elif r .status_code == 429 :
28+ raise TooManyRequestsError (r .text )
29+ elif r .status_code == 503 :
30+ raise ServiceUnavailableError (r .text )
31+ else :
32+ raise OptimizelyError (r .text )
33+
34+ @classmethod
35+ def all (cls ):
36+ return cls ._parse_response (requests .get (api_base + cls .endpoint , headers = {'Token' : api_key }))
37+
38+ @classmethod
39+ def get (cls , pid ):
40+ return cls ._parse_response (requests .get (api_base + cls .endpoint + str (pid ), headers = {'Token' : api_key }))
41+
42+ @classmethod
43+ def create (cls , data ):
44+ return cls ._parse_response (requests .post (api_base + cls .endpoint , data = json .dumps (data ),
45+ headers = {'Token' : api_key , 'Content-Type' : 'application/json' }))
46+
47+ @classmethod
48+ def update (cls , pid , data ):
49+ return cls ._parse_response (requests .put (api_base + cls .endpoint + str (pid ), data = json .dumps (data ),
50+ headers = {'Token' : api_key , 'Content-Type' : 'application/json' }))
51+
52+ @classmethod
53+ def delete (cls , pid ):
54+ return cls ._parse_response (requests .delete (api_base + cls .endpoint + str (pid ), headers = {'Token' : api_key }))
55+
56+
57+ class Project (APIResource ):
58+ endpoint = 'projects/'
59+
60+
61+ class Experiment (APIResource ):
62+ endpoint = 'experiments/'
63+
64+ @classmethod
65+ def all (cls ):
66+ raise NotImplementedError ('There is no method to get all experiments. Try using get_from_project() instead.' )
67+
68+ @classmethod
69+ def get_from_project (cls , pid ):
70+ return cls ._parse_response (requests .get (api_base + Project .endpoint + str (pid ) + '/' + cls .endpoint ,
71+ headers = {'Token' : api_key }))
72+
73+ @classmethod
74+ def get_results (cls , eid ):
75+ return cls ._parse_response (requests .get (api_base + cls .endpoint + str (eid ) + '/results' ,
76+ headers = {'Token' : api_key }))
77+
78+
79+ class OptimizelyError (Exception ):
80+ """ General exception for all Optimizely Experiments API related issues."""
81+ pass
82+
83+
84+ class BadRequestError (OptimizelyError ):
85+ """ Exception for when request was not sent in valid JSON."""
86+ pass
87+
88+
89+ class UnauthorizedError (OptimizelyError ):
90+ """ Exception for when API token is missing or included in the body rather than the header."""
91+ pass
92+
93+
94+ class ForbiddenError (OptimizelyError ):
95+ """ Exception for when API token is provided but it is invalid or revoked."""
96+ pass
97+
98+
99+ class NotFoundError (OptimizelyError ):
100+ """ Exception for when the id used in request is inaccurate or token user doesn't have permission to
101+ view/edit it."""
102+ pass
103+
104+
105+ class TooManyRequestsError (OptimizelyError ):
106+ """ Exception for when a rate limit for the API is hit."""
107+ pass
108+
109+
110+ class ServiceUnavailableError (OptimizelyError ):
111+ """ Exception for when the API is overloaded or down for maintenance."""
112+ pass
0 commit comments