CoCalc Public FilesPublic / PYTHON / 05-specialized.ipynbOpen with one click!
Author: Hal Snyder
Views : 138
Description: API calls other than query
Compute Environment: Ubuntu 18.04 (Deprecated)

Examples of Specialized API Calls

Calls that use message type other than query.

In [ ]:
# import utility definitions %run ./01-utils.ipynb
In [ ]:
# load credentials uinfo = load_user_info("../../SECRET/testuser.yaml")
In [ ]:
# Example A - create project # It is possible to create two projects with the same name and description; such projects will have different project_id attributes. For that reason, we check first if a matching project has already been created, and skip the creation step if it has already been done. title = 'SAMPLE PROJECT' description = '#apitest' payload = {"query":{"projects":[{"project_id":None, "title":title, "description":None}]}} response = call_api("query", payload, uinfo['api_key']) matching_projects = response['query']['projects'] lmatch = len(matching_projects) print('found {} projects with title "{}"'.format(lmatch, title)) myproj = None if lmatch > 0: print("skipping project creation, already have these projects:") pp.pprint(matching_projects) myproj = matching_projects[0]['project_id'] else: print("creating project - note project_id") payload = {'title' :title, 'description':description} response = call_api('create_project', payload, uinfo['api_key']) pp.pprint(response) try: myproj = response['project_id'] print("new project id = {}".format(myproj)) except: print('project not created')
In [ ]:
# Example B - create text file # Directory will be created if it doesn't already exist. # Text file will be overwritten if it already exists. # If you run this while the project is stopped, the call may fail with ECONNREFUSED. path = "TEXT/sample.md" text = """ # Sample markdown file ## Section 1 section 1 contents """ print("creating text file") payload = {'project_id' :myproj, 'content' :text, 'path' :path} response = call_api('write_text_file_to_project', payload, uinfo['api_key']) pp.pprint(response)
In [ ]: