Getting started with Congree and Python
Introduction
This document is itended to give some ideas and code snippets to allow for an easier integration of Congree's features in Python application.
It is not the intention of this document to provide a full-blown tutorial on Python development but rather to give some ideas and hints on how to integrate Congree into Python application.
The tutorial demonstrates how to use Congree's REST API using some basic methods providing a ready-to-use approach to get started with Python and Congree.
The code sample use the Python lbrary requests which needs to be installed to make the code work.
pip install requests
Authentication
The code below shows a method that allows for an authentication. It expects three arguments:
- the URL of the server where the Congree Server is running
- the username
- the password
The method returns the token that must be used in allo other methods to allow for an authorization.
import requests import json server = 'http://win-ki2kq6turvn' authUrl = '/congreeidentityserver/connect/token' documentsUrl = '/congree/api/Settings/v1/Document/' checkDocumentUrl = '/congree/api/Linguistic/v1/Check' def GetToken(serverUrl, username, password): url = serverUrl + authUrl payload = "grant_type=password&username=" + username + "&password=" + password + "&scope=webApi&client_id=CongreeWebAPI" headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.request("POST", url, data=payload, headers=headers) responsetext = json.loads(response.text) return responsetext["access_token"]
Congree Python module
This is the complete module written in Python. It can be included in any script using import congree.
import requests import json authUrl = '/congreeidentityserver/connect/token' documentsUrl = '/congree/api/Settings/v1/Document/' checkDocumentUrl = '/congree/api/Linguistic/v1/Check' searchTermUrl = '/congree/api/Terminology/v1/SearchTerm' __name__ = "congree" def GetToken(serverUrl, username, password): url = serverUrl + authUrl payload = "grant_type=password&username=" + username + "&password=" + password + "&scope=webApi&client_id=CongreeWebAPI" headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.request("POST", url, data=payload, headers=headers) responsetext = json.loads(response.text) return responsetext["access_token"] def GetDocumentSettings(server, token): url = server + documentsUrl payload = "" headers = {'authorization': 'Bearer ' + token} response = requests.request("GET", url, data=payload, headers=headers) return response.text def Check(serverUrl, text, token, ruleset): url = serverUrl + checkDocumentUrl querystring = {"ruleSet":ruleset} payload = "{'Xml': '" + text + "','options': {'includeReporting': true,'extractTermCandidates': true,'includeContextMapping': true}}" headers = { 'Content-type': "application/json", 'authorization': "Bearer " + token } response = requests.post(url, data=payload.encode('utf-8'), headers=headers, params=querystring) responsetext = json.loads(response.text) return responsetext def SearchTerm(server, term, ruleset,culture, token): url = server + searchTermUrl querystring = {"term":term,"ruleSet":ruleset,"culture":culture} payload = "" headers = {'authorization': 'Bearer ' + token} response = requests.request("GET", url, data=payload, headers=headers, params=querystring) return response.text
Finding terms
import requests import json server = 'http://win-ki2kq6turvn' authUrl = '/congreeidentityserver/connect/token' documentsUrl = '/congree/api/Settings/v1/Document/' checkDocumentUrl = '/congree/api/Linguistic/v1/Check' searchTermUrl = '/congree/api/Terminology/v1/SearchTerm' def SearchTerm(server, term, ruleset,culture, token): url = server + searchTermUrl querystring = {"term":term,"ruleSet":ruleset,"culture":culture} payload = "" headers = {'authorization': 'Bearer ' + token} response = requests.request("GET", url, data=payload, headers=headers, params=querystring) return response.text