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

Commit

Permalink
Updates examples and tests to use new Tasks API and service param
Browse files Browse the repository at this point in the history
  • Loading branch information
dugjason authored and andimiller committed Nov 15, 2016
1 parent d014825 commit 104f93c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
24 changes: 14 additions & 10 deletions examples/pylon_api.py
Expand Up @@ -2,31 +2,32 @@
import time
from datasift import Client

datasift = Client("your username", "your API key")
#datasift = Client("USERNAME", "API_KEY")
datasift = Client("CS_7", "dff990e42c14ef5d5aa280b0e9fea9e2")

csdl = '(fb.content any "coffee") AND fb.language in "en"'

service = 'facebook'

print('Validating the CSDL')
print(datasift.pylon.validate(csdl))
print(datasift.pylon.validate(csdl, service=service))

print('Compiling the CSDL')
compiled = datasift.pylon.compile(csdl)
compiled = datasift.pylon.compile(csdl, service=service)

print (compiled)
print(compiled)

name = 'My analysis recording'

print('Start the recording and wait 10 seconds')
results = datasift.pylon.start(service, compiled['hash'], name)
results = datasift.pylon.start(compiled['hash'], name, service=service)

recording_id = results['id']

time.sleep(10)

print('Stop the recording')
datasift.pylon.stop(service, recording_id)
datasift.pylon.stop(recording_id, service=service)

analyze_parameters = {
'analysis_type': 'freqDist',
Expand All @@ -49,10 +50,13 @@
analyze_filter = 'fb.content contains "starbucks"'

print('Hit the analyze end point and return the insights')
print(datasift.pylon.analyze(service, recording_id, analyze_parameters, analyze_filter))
print(datasift.pylon.analyze(recording_id, analyze_parameters, analyze_filter, service=service))

print('Retrive some sample interactions from the recording')
print(datasift.pylon.sample(service, recording_id))
print(datasift.pylon.sample(recording_id, service=service))

print('Retrieve the analysis using get')
print(datasift.pylon.get(service, recording_id))
print('Retrieve the recording details using get')
print(datasift.pylon.get(recording_id, service=service))

print('List all recordings')
print(datasift.pylon.list(service=service))
26 changes: 13 additions & 13 deletions examples/pylon_tasks_api.py
@@ -1,12 +1,12 @@
from __future__ import print_function
import time
from datasift import Client

datasift = Client("your username", "your API key")
datasift = Client("USERNAME", "API_KEY")

name = 'My analysis recording'
name = 'Example Analysis Task'
service = 'facebook'

datasift.pylon.list('facebook')
#datasift.pylon.list(service=service)

analyze_parameters = {
'filter': '',
Expand All @@ -16,20 +16,20 @@
'analysis_type': 'freqDist',
'parameters': {
'threshold': 5,
'target': 'li.activity.type'
'target': 'fb.activity.type'
}
}
}

print('Hit the analyze end point and return the insights')
print('Create an analysis task')
task = datasift.pylon.task.create(
service='facebook',
subscription_id='sub_id',
name='new task',
parameters=analyze_parameters
subscription_id='SUBSCRIPTION_ID',
name=name,
parameters=analyze_parameters,
service=service
)

print(task)

print('Get the task taht was created')
datasift.pylon.task.get(task['id'])
print('Get the task that was created')
results = datasift.pylon.task.get(task['id'], service=service)
print(results)
26 changes: 13 additions & 13 deletions tests/test_client.py
Expand Up @@ -613,13 +613,13 @@ def test_can_compile_pylon_csdl(self):

def test_can_start_a_pylon_recording(self):
with HTTMock(intentionally_blank):
result = self.client.pylon.start("service", "dummy hash", "dummy name")
result = self.client.pylon.start("dummy hash", name="dummy name", service="service")
self.assertEqual(result.status_code, 204)
self.assertDictEqual({}, result)

def test_can_stop_a_pylon_recording(self):
with HTTMock(intentionally_blank):
result = self.client.pylon.stop("service", "dummy hash")
result = self.client.pylon.stop("dummy id", service="service")
self.assertEqual(result.status_code, 204)
self.assertDictEqual({}, result)

Expand All @@ -629,22 +629,22 @@ def test_can_analyze_a_pylon_recording(self):
for expected_output in expected_outputs:
runs += 1
with HTTMock(mock):
result = self.client.pylon.analyze("service", "target hash", parameters={'analysis_type': 'freqDist', 'parameters': {'threshold': 5, 'target': 'fb.author.age'}}, filter="CSDL Filter", start=time.time()-60, end=time.time())
result = self.client.pylon.analyze("target id", parameters={'analysis_type': 'freqDist', 'parameters': {'threshold': 5, 'target': 'fb.author.age'}}, filter="CSDL Filter", start=time.time()-60, end=time.time(), service="service")
self.assertEqual(result.status_code, 200)
assert_dict_structure(self, expected_output, result)
self.assertNotEqual(runs, 0, "ensure that at least one case was tested")

def test_can_fail_analyze_gracefully(self):
with HTTMock(internal_server_error):
self.assertRaises(DataSiftApiFailure, self.client.pylon.analyze, "service", "target hash", parameters={'analysis_type': 'freqDist', 'parameters': {'threshold': 5, 'target': 'fb.author.age'}}, filter="CSDL Filter", start=time.time()-60, end=time.time())
self.assertRaises(DataSiftApiFailure, self.client.pylon.analyze, "target id", parameters={'analysis_type': 'freqDist', 'parameters': {'threshold': 5, 'target': 'fb.author.age'}}, filter="CSDL Filter", start=time.time()-60, end=time.time(), service="service")

def test_can_get_specific_recording(self):
mock, expected_outputs = mock_output_of(self.client.pylon.get)
with HTTMock(mock):
runs = 0
for expected_output in expected_outputs:
runs += 1
result = self.client.pylon.get("service", "dummy analysis hash")
result = self.client.pylon.get("dummy analysis hash", service="service")
self.assertEqual(result.status_code, 200)
assert_dict_structure(self, result, expected_output)
self.assertNotEqual(runs, 0, "ensure that at least one case was tested")
Expand All @@ -655,7 +655,7 @@ def test_can_get_recordings(self):
runs = 0
for expected_output in expected_outputs:
runs += 1
result = self.client.pylon.list("service", per_page=25, page=1)
result = self.client.pylon.list(per_page=25, page=1, service="service")
self.assertEqual(result.status_code, 200)
assert_dict_structure(self, result, expected_output)
self.assertNotEqual(runs, 0, "ensure that at least one case was tested")
Expand All @@ -666,7 +666,7 @@ def test_can_get_pylon_tags(self):
runs = 0
for expected_output in expected_outputs:
runs += 1
result = self.client.pylon.tags("service", "dummy hash")
result = self.client.pylon.tags("dummy hash", service="service")
self.assertEqual(result.status_code, 200)
assert_dict_structure(self, result, expected_output)
self.assertNotEqual(runs, 0, "ensure that at least one case was tested")
Expand All @@ -677,7 +677,7 @@ def test_can_get_pylon_sample(self):
runs = 0
for expected_output in expected_outputs:
runs += 1
result = self.client.pylon.sample("service", "dummy hash")
result = self.client.pylon.sample("dummy hash", service="service")
self.assertEqual(result.status_code, 200)
assert_dict_structure(self, result, expected_output)
self.assertNotEqual(runs, 0, "ensure that at least one case was tested")
Expand Down Expand Up @@ -885,11 +885,11 @@ def test_can_return_futures(self):
assert(isinstance(f, concurrent.futures.Future))

def test_can_return_futures_from_prefixed_endpoints(self):
f = self.client.pylon.analyze("service", 1, 2, 3)
f = self.client.pylon.analyze(1, 2, 3, service="service")
assert(isinstance(f, concurrent.futures.Future))

def test_futures_have_a_process_method(self):
f = self.client.pylon.analyze("service", 1, 2, 3)
f = self.client.pylon.analyze(1, 2, 3, service="service")
assert(hasattr(f, "process"))

class TestMockedTaskClient(TestCase):
Expand All @@ -903,7 +903,7 @@ def test_can_create_a_pylon_task(self):
for expected_output in expected_outputs:
runs += 1
with HTTMock(mock):
result = self.client.pylon.task.create("service", "subscription_id", "name", {'filter': '', 'analysis_type': 'freqDist', 'parameters': {'analysis_type': 'freqDist', 'parameters': {'threshold': 5, 'target': 'li.activity.type'}}}, 'type')
result = self.client.pylon.task.create("subscription_id", "name", {'filter': '', 'analysis_type': 'freqDist', 'parameters': {'analysis_type': 'freqDist', 'parameters': {'threshold': 5, 'target': 'fb.activity.type'}}}, 'type', service="service")
self.assertEqual(result.status_code, 200)
assert_dict_structure(self, result, expected_output)
self.assertNotEqual(runs, 0, "ensure that at least one case was tested")
Expand All @@ -914,7 +914,7 @@ def test_can_get_specific_pylon_task(self):
runs = 0
for expected_output in expected_outputs:
runs += 1
result = self.client.pylon.task.get("service", "dummy id")
result = self.client.pylon.task.get("dummy id", service="service")
self.assertEqual(result.status_code, 200)
assert_dict_structure(self, result, expected_output)
self.assertNotEqual(runs, 0, "ensure that at least one case was tested")
Expand All @@ -925,7 +925,7 @@ def test_can_get_pylon_tasks(self):
runs = 0
for expected_output in expected_outputs:
runs += 1
result = self.client.pylon.task.list("service", per_page=25, page=1)
result = self.client.pylon.task.list(per_page=25, page=1, service="service")
self.assertEqual(result.status_code, 200)
assert_dict_structure(self, result, expected_output)
self.assertNotEqual(runs, 0, "ensure that at least one case was tested")
Expand Down

0 comments on commit 104f93c

Please sign in to comment.