-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from smasty/master
Refactor the SDK
- Loading branch information
Showing
16 changed files
with
399 additions
and
367 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
env | ||
venv | ||
__pycache__ | ||
.pytest_cache | ||
test.py | ||
build | ||
dist | ||
exponea_python_sdk.egg-info | ||
exponea_python_sdk.egg-info | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
from exponea_python_sdk.client import Exponea | ||
name = "exponea_python_sdk" | ||
|
||
name = 'exponea_python_sdk' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,44 @@ | ||
class AnalysisType: | ||
FUNNEL = 'funnel' | ||
REPORT = 'report' | ||
SEGMENTATION = 'segmentation' | ||
|
||
|
||
ANALYSIS_TYPES = { | ||
AnalysisType.FUNNEL, | ||
AnalysisType.REPORT, | ||
AnalysisType.SEGMENTATION, | ||
} | ||
|
||
|
||
class Analyses: | ||
def __init__(self, client): | ||
self.client = client | ||
self.endpoint_base = "/data/v2/projects/{}/analyses".format(client.project_token) | ||
|
||
# Generic method for getting any type of Analyses | ||
self.endpoint_base = '/data/v2/projects/{}/analyses'.format(client.project_token) | ||
|
||
def get_analysis(self, analysis_type, analysis_id): | ||
# Construct request | ||
path = self.endpoint_base + "/" + analysis_type | ||
payload = { "analysis_id": analysis_id, "format": "table_json" } | ||
response = self.client.request("POST", path, payload) | ||
"""Generic method for getting any type of analyses""" | ||
assert analysis_type in ANALYSIS_TYPES, 'Unknown analysis type "{}"'.format(analysis_type) | ||
path = '{}/{}'.format(self.endpoint_base, analysis_type) | ||
payload = {'analysis_id': analysis_id, 'format': 'table_json'} | ||
response = self.client.post(path, payload) | ||
# In case analysis is not found | ||
if response is None: | ||
return None | ||
headers = response["header"] | ||
result = { "name": response["name"], "data": [] } | ||
for row in response["rows"]: | ||
headers = response['header'] | ||
result = {'name': response['name'], 'data': []} | ||
for row in response['rows']: | ||
item = {} | ||
for index, data in enumerate(row): | ||
item[headers[index]] = data | ||
result["data"].append(item) | ||
result['data'].append(item) | ||
return result | ||
|
||
def get_funnel(self, funnel_id): | ||
return self.get_analysis("funnel", funnel_id) | ||
return self.get_analysis(AnalysisType.FUNNEL, funnel_id) | ||
|
||
def get_report(self, report_id): | ||
return self.get_analysis("report", report_id) | ||
return self.get_analysis(AnalysisType.REPORT, report_id) | ||
|
||
def get_segmentation(self, segmentation_id): | ||
return self.get_analysis("segmentation", segmentation_id) | ||
return self.get_analysis(AnalysisType.SEGMENTATION, segmentation_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,70 @@ | ||
from urllib.parse import urlencode | ||
|
||
|
||
class Catalog: | ||
def __init__(self, client): | ||
self.client = client | ||
self.endpoint_base = "/data/v2/projects/{}/catalogs".format(client.project_token) | ||
self.endpoint_base = '/data/v2/projects/{}/catalogs'.format(client.project_token) | ||
|
||
def create_catalog(self, name, fields): | ||
path = self.endpoint_base | ||
payload = { "name": name, "fields": [ {"name": field } for field in fields ] } | ||
response = self.client.request("POST", path, payload) | ||
payload = {'name': name, 'fields': [{'name': field} for field in fields]} | ||
response = self.client.post(path, payload) | ||
if response is None: | ||
return None | ||
return response["id"] | ||
return response['id'] | ||
|
||
def get_catalog_name(self, catalog_id): | ||
path = self.endpoint_base + "/{}".format(catalog_id) | ||
response = self.client.request("GET", path) | ||
path = '{}/{}'.format(self.endpoint_base, catalog_id) | ||
response = self.client.get(path) | ||
if response is None: | ||
return None | ||
return response["data"]["name"] | ||
return response['data']['name'] | ||
|
||
def get_catalog_items(self, catalog_id, params=None): | ||
# URL encode any filters and params | ||
if bool(params): | ||
query_string = "?" + urlencode(params) | ||
else: | ||
query_string = "" | ||
path = self.endpoint_base + "/{}/items{}".format(catalog_id, query_string) | ||
response = self.client.request("GET", path) | ||
query_string = '?{}'.format(urlencode(params)) if params else '' | ||
path = '{}/{}/items{}'.format(self.endpoint_base, catalog_id, query_string) | ||
response = self.client.get(path) | ||
if response is None: | ||
return None | ||
# Delete unnecessary attributes | ||
del response["success"] | ||
for item in response["data"]: | ||
del item["catalog_id"] | ||
del response['success'] | ||
for item in response['data']: | ||
del item['catalog_id'] | ||
return response | ||
|
||
def update_catalog_name(self, catalog_id, catalog_name, fields): | ||
path = self.endpoint_base + "/{}".format(catalog_id) | ||
payload = { "name": catalog_name, "fields": [ { "name": field } for field in fields ] } | ||
response = self.client.request("PUT", path, payload) | ||
path = '{}/{}'.format(self.endpoint_base, catalog_id) | ||
payload = {'name': catalog_name, 'fields': [{'name': field} for field in fields]} | ||
response = self.client.put(path, payload) | ||
if response is None: | ||
return None | ||
return response["success"] | ||
return response['success'] | ||
|
||
def create_catalog_item(self, catalog_id, item_id, properties): | ||
path = self.endpoint_base + "/{}/items/{}".format(catalog_id, item_id) | ||
payload = { "properties": properties } | ||
response = self.client.request("PUT", path, payload) | ||
if response is None: | ||
return None | ||
return response["success"] | ||
|
||
path = '{}/{}/items/{}'.format(self.endpoint_base, catalog_id, item_id) | ||
payload = {'properties': properties} | ||
response = self.client.put(path, payload) | ||
return None if response is None else response['success'] | ||
|
||
def update_catalog_item(self, catalog_id, item_id, properties): | ||
path = self.endpoint_base + "/{}/items/{}/partial-update".format(catalog_id, item_id) | ||
payload = { "properties": properties } | ||
response = self.client.request("POST", path, payload) | ||
if response is None: | ||
return None | ||
return response["success"] | ||
|
||
path = '{}/{}/items/{}/partial-update'.format(self.endpoint_base, catalog_id, item_id) | ||
payload = {'properties': properties} | ||
response = self.client.post(path, payload) | ||
return None if response is None else response['success'] | ||
|
||
def delete_catalog_item(self, catalog_id, item_id): | ||
path = self.endpoint_base + "/{}/items/{}".format(catalog_id, item_id) | ||
response = self.client.request("DELETE", path) | ||
if response is None: | ||
return None | ||
return response["success"] | ||
|
||
path = '{}/{}/items/{}'.format(self.endpoint_base, catalog_id, item_id) | ||
response = self.client.delete(path) | ||
return None if response is None else response['success'] | ||
|
||
def delete_catalog_items(self, catalog_id): | ||
path = self.endpoint_base + "/{}/items".format(catalog_id) | ||
response = self.client.request("DELETE", path) | ||
if response is None: | ||
return None | ||
return response["success"] | ||
path = '{}/{}/items'.format(self.endpoint_base, catalog_id) | ||
response = self.client.delete(path) | ||
return None if response is None else response['success'] | ||
|
||
def delete_catalog(self, catalog_id): | ||
path = self.endpoint_base + "/{}".format(catalog_id) | ||
response = self.client.request("DELETE", path) | ||
if response is None: | ||
return None | ||
return response["success"] | ||
path = '{}/{}'.format(self.endpoint_base, catalog_id) | ||
response = self.client.delete(path) | ||
return None if response is None else response['success'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.