Skip to content

Commit c2277d0

Browse files
committed
get project function
1 parent b46a8f6 commit c2277d0

File tree

7 files changed

+99
-0
lines changed

7 files changed

+99
-0
lines changed

CmixAPIClient/api.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,22 @@ def fetch_raw_results(self, survey_id, payload):
139139
response = requests.post(url, headers=self._authentication_headers, json=payload)
140140
return response.json()
141141

142+
def api_get(self, endpoint, error=''):
143+
self.check_auth_headers()
144+
url = '{}/{}'.format(CMIX_SERVICES['survey'][self.url_type], endpoint)
145+
response = requests.get(url, headers=self._authentication_headers)
146+
if response.status_code != 200:
147+
if '' == error:
148+
error = 'CMIX returned a non-200 response code'
149+
raise CmixError(
150+
'{}: {} and error {}'.format(
151+
error,
152+
response.status_code,
153+
response.text
154+
)
155+
)
156+
return response.json()
157+
142158
def get_surveys(self, status, *args, **kwargs):
143159
'''kwargs:
144160

CmixAPIClient/project.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from .error import CmixError
5+
6+
7+
class CmixProject(object):
8+
def __init__(self, client, project_id):
9+
if None in [client, project_id]:
10+
raise CmixError("Client and project id are required.")
11+
self.client = client
12+
self.project_id = project_id
13+
14+
def get_project(self):
15+
project_endpoint = 'projects/{}'.format(self.project_id)
16+
project_error = 'CMIX returned a non-200 response code while getting project'
17+
project_response = self.client.api_get(project_endpoint, project_error)
18+
return project_response

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ A Python client library for the [Dynata Cmix API](https://wiki2.criticalmix.net/
2525

2626
## Supported API Functions
2727

28+
### CmixAPI
2829
authenticate(*args, **kwargs)
2930
fetch_banner_filter(survey_id, question_a, question_b, response_id)
3031
fetch_raw_results(survey_id, payload)
32+
get_project(project_id)
3133
get_surveys(status, *args, **kwargs)
3234
get_survey_data_layouts(survey_id)
3335
get_survey_definition(survey_id)
@@ -46,6 +48,10 @@ A Python client library for the [Dynata Cmix API](https://wiki2.criticalmix.net/
4648
update_project(project_id, status=None)
4749
create_survey(xml_string)
4850

51+
### CmixProject
52+
53+
get_project(project_id)
54+
4955
## Contributing
5056

5157
Information on [contributing](https://github.com/dynata/python-cmixapi-client/blob/dev/CONTRIBUTING.md) to this python library.

test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
listc=["London", "Rome", "Moscow", "Paris"]
2+
nfile=open("towns.txt", 'w')
3+
4+
i = 0
5+
6+
for town in listc:
7+
nfile.write(town)
8+
if line == 2:
9+
nfile.write('\n')
10+
i = 0
11+
continue
12+
i += 1
13+
14+
nfile.close()

tests/test_api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ def setUp(self):
2222
self.cmix_api = default_cmix_api()
2323
self.cmix_api._authentication_headers = {'Authorization': 'Bearer test'}
2424
self.survey_id = 1337
25+
self.project_id = 1492
2526

2627
def test_cmix_authentication_check(self):
2728
with self.assertRaises(CmixError):

tests/test_project.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import print_function
3+
from __future__ import unicode_literals
4+
import mock
5+
6+
from unittest import TestCase
7+
from CmixAPIClient.api import CMIX_SERVICES
8+
from CmixAPIClient.project import CmixProject
9+
from CmixAPIClient.error import CmixError
10+
from .test_api import default_cmix_api
11+
12+
13+
class TestCmixProject(TestCase):
14+
def setUp(self):
15+
self.cmix_api = default_cmix_api()
16+
self.cmix_api._authentication_headers = {'Authorization': 'Bearer test'}
17+
self.project_id = 1492
18+
19+
def test_get_project(self):
20+
project = CmixProject(self.cmix_api, self.project_id)
21+
22+
# success case
23+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
24+
mock_get = mock.Mock()
25+
mock_get.status_code = 200
26+
mock_get.json.return_value = {}
27+
mock_request.get.return_value = mock_get
28+
29+
project.get_project()
30+
31+
base_url = CMIX_SERVICES['survey']['BASE_URL']
32+
project_url = '{}/projects/{}'.format(base_url, self.project_id)
33+
mock_request.get.assert_any_call(project_url, headers=self.cmix_api._authentication_headers)
34+
35+
# error case (survey not found)
36+
with mock.patch('CmixAPIClient.api.requests') as mock_request:
37+
mock_get = mock.Mock()
38+
mock_get.status_code = 404
39+
mock_get.json.return_value = {}
40+
mock_request.get.return_value = mock_get
41+
42+
with self.assertRaises(CmixError):
43+
project.get_project()

towns.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
London

0 commit comments

Comments
 (0)