Post

5 followers Follow
0
Avatar

Clarizen API integration with Python

Hi, 

 

Is there any sample code available to call Clarizen API from Python 2.7 

I am currently using Curl but would like to know if Python calls are supported as well. 

Best,

Sahil

Sahil Batra Answered

Official comment

Avatar

Hi Sahil,

You can use our REST API (https://api.clarizen.com/v2.0/services/) for that.

Here is a full example of how to login, create new project and retrieve all your projects:

 

*******************************************************************************************************

import json
import requests

clarizen_rest_api_url = 'https://api.clarizen.com/v2.0/services/'
login_url = 'authentication/login'
query_url = 'data/EntityQuery'
create_and_retrieve_url = 'data/createAndRetrieve'

clarizen_user_name = 'clarizen_user_name'
clarizen_password = 'clarizen_password'
session_id = ''


def login():
data = {'userName': clarizen_user_name, 'password': clarizen_password}
url = clarizen_rest_api_url + login_url
response = requests.post(url, data)
response_data = json.loads(response.text)
global session_id
session_id = response_data['sessionId']


def query(data=None):
headers = {'Authorization': 'Session ' + session_id}
url = clarizen_rest_api_url + query_url
response = requests.post(url, data=json.dumps(data), headers=headers)
json_response = json.loads(response.content)
return json_response.get('entities')


def create(data=None):
headers = {'Authorization': 'Session ' + session_id}
url = clarizen_rest_api_url + create_and_retrieve_url
response = requests.post(url, data=json.dumps(data), headers=headers)
json_response = json.loads(response.content)
return json_response.get('entity')


def retrieve_projects():
data = {'typeName': 'Project', 'fields': ['Name', 'StartDate']}
results = query(data)
return results


def create_project(name):
data = {'entity': {'Id': '/Project', 'Name': name, 'StartDate': '2017-6-16',}, 'fields': ['Name', 'DueDate']}
results = create(data)
return results


login()
create_results = create_project('My Project A')
query_results = retrieve_projects()


I hope this helps,

Elad

Elad Franklin
Comment actions Permalink

Please sign in to leave a comment.

13 comments

0
Avatar

Thank you Elad ! Just wondering if you might have a sample of how to call CZQL using Python as well ?

Sahil Batra 0 votes
Comment actions Permalink
1
Avatar

Hi Sahil,

Here you go:

 

czql_url = 'data/Query'

def czql_query(data=None):
headers = {'Authorization': 'Session ' + session_id}
url = clarizen_rest_api_url + czql_url
response = requests.post(url, data=json.dumps(data), headers=headers)
json_response = json.loads(response.content)
return json_response.get('entities')

def retrieve_customers():
data = {'q': 'SELECT Name FROM Customer'}
results = czql_query(data)
return results
Elad Franklin 1 vote
Comment actions Permalink
0
Avatar

Hi,

Can you please provide an example python code that shows how to update project property of a specific project? Thanks

Aditya Kurve 0 votes
Comment actions Permalink
0
Avatar

Hi Aditya,

Here you can see the documentation for update an entity in Clarizen:

https://api2.clarizen.com/V2.0/services/data/objects

Here is a code example in Python for changing a project name:

update_url = 'data/objects'
def update(id, type, data=None):
headers = {'Authorization': 'Session ' + session_id}
url = clarizen_rest_api_url + update_url + '/' + type + '/' + id
response = requests.post(url, data=json.dumps(data), headers=headers)
json_response = json.loads(response.content)
return json_response.get('entity')

def update_projects_name(id ,name):
data = { 'Name': name}
results = update(id, 'Project', data)
return results

update_projects_name('[project external id]', '[project new name]')


I hope this helps,
Elad

Elad Franklin 0 votes
Comment actions Permalink
0
Avatar

Hello Elad,

 

The retrieve_projects() function above does not return all the projects. Not sure why. Any ideas?

Aditya Kurve 0 votes
Comment actions Permalink
0
Avatar

Elad, the above seems to work great for creating Work Items (Project, Task), but Creation of a Case object (for example a Request) does not work.

 

Using:

def create_project(name):
data = {'entity': {'Id': '/Project', 'Name': name, 'StartDate': '2017-6-16',}, 'fields': ['Name', 'DueDate']}
results = create(data)
return results

 

as

def create_request(name):
data = {'entity': {'Id': '/Request', 'Name': name, 'StartDate': '2017-6-16',}, 'fields': ['Name', 'DueDate']}
results = create(data)
return results

 

Does not create a request, looking through https://api.clarizen.com/v2.0/services/ I am not seeing what I am missing.  Could you clarify how one might create a request?

 

Jared Thompson 0 votes
Comment actions Permalink
0
Avatar

Elad, the above seems to work great for creating Work Items (Project, Task), but Creation of a Case object (for example a Request) does not work.

 

Using:

def create_project(name):
data = {'entity': {'Id': '/Project', 'Name': name, 'StartDate': '2017-6-16',}, 'fields': ['Name', 'DueDate']}
results = create(data)
return results

 

as

def create_request(name):
data = {'entity': {'Id': '/Request', 'Name': name, 'StartDate': '2017-6-16',}, 'fields': ['Name', 'DueDate']}
results = create(data)
return results

 

Does not create a request, looking through https://api.clarizen.com/v2.0/services/ I am not seeing what I am missing.  Could you clarify how one might create a request?

 

Jared Thompson 0 votes
Comment actions Permalink
0
Avatar

Hi Jared,

The request body should look like this (for example):

{
      'entity': {'Id': '/EnhancementRequest', 'Title': 'Request name', 'OpeningDate': '2017-6-16'},
       'fields': ['DueDate'],
}

Elad Franklin 0 votes
Comment actions Permalink
0
Avatar

Elad, thanks, where in the documentation did you find /EnhancementRequest ? I have looked through the documentation and (at least as of yet) have not found it.  I am trying to ensure I understand how to think about these questions so I can better answer my own questions through the provided documentation.

Jared Thompson 0 votes
Comment actions Permalink
0
Avatar

Hi Jared,

If you are an admin in Clarizen you can go to configure page in the Settings module and see all entities type API names.

Elad Franklin 0 votes
Comment actions Permalink