Files
onspring-api-sdk-python/README.md
T

540 lines
14 KiB
Markdown
Raw Normal View History

2022-05-02 15:34:55 -05:00
# Onspring API Python SDK
2022-05-02 15:36:56 -05:00
The python SDK for **version 2** of the Onspring API is meant to simplify development in Python for Onspring customers who want to build integrations with their Onspring instance.
2022-05-02 15:34:55 -05:00
2022-05-03 08:56:47 -05:00
**Note:**
2022-05-03 08:57:24 -05:00
This is an unofficial SDK for the Onspring API. It was not built in consultation with Onspring Technologies LLC or a member of their development team.
2022-05-03 08:56:47 -05:00
2022-05-03 09:46:08 -05:00
This SDK was developed independently using their existing C# SDK, their swagger page, and api documentation with the intention of making development of integrations done in Python with an Onspring instance quicker and more convenient.
2022-05-03 08:56:47 -05:00
2022-05-03 09:00:26 -05:00
## Dependencies
2022-05-03 09:40:19 -05:00
### Python
2022-05-03 09:40:47 -05:00
Requires use of Python 3.6.0 or later.
2022-05-03 09:40:19 -05:00
### Requests
2022-05-03 09:37:14 -05:00
All methods for the `OnspringClient` make use of the [Requests](https://docs.python-requests.org/en/latest/) library to interact with the endpoints of version 2 of the Onspring API.
2022-05-02 15:34:55 -05:00
## Installation
2022-05-02 15:36:16 -05:00
Install the SDK using pip:
2022-05-02 15:34:55 -05:00
`pip install onspring-api-sdk-python`
## API Key
In order to successfully interact with the Onspring Api you will need an API key. API keys are obtained by an Onspring user with permissions to at least **Read** API Keys for your instance via the following steps:
1. Login to the Onspring instance.
2. Navigate to **Administration** > **Security** > **API Keys**
3. On the list page, add a new API Key - this will require **Create** permissions - or click an existing API key to view its details.
4. Click on the **Developer Information** tab.
5. Copy the **X-ApiKey Header** value from this tab.
## Start Coding
2022-05-03 09:00:26 -05:00
### `OnspringClient`
2022-05-03 09:00:55 -05:00
The most common way to use the SDK is to create an `OnspringClient` instance and call its methods. Its constructor requires two parameters:
2022-05-02 15:34:55 -05:00
- `baseUrl` - currently this should always be: `https://api.onspring.com`
- `apiKey` - the value obtained by following the steps in the **API Key** section
It is best practice to read these values in from a configuration file for both flexibility and security purposes.
2022-05-02 16:31:26 -05:00
Example `config.ini` file:
2022-05-02 15:34:55 -05:00
2022-05-02 16:22:06 -05:00
```ini
2022-05-02 16:20:47 -05:00
[prod]
key = 000000ffffff000000ffffff/00000000-ffff-0000-ffff-000000000000
url = https://api.onspring.com
```
Example constructing `OnspringClient`:
2022-05-02 16:22:06 -05:00
2022-05-02 16:29:34 -05:00
```python
2022-05-02 15:34:55 -05:00
from OnspringClient import OnspringClient
from configparser import ConfigParser
cfg = ConfigParser()
cfg.read('config.ini')
key = cfg['prod']['key']
url = cfg['prod']['url']
client = OnspringClient(url, key)
```
2022-05-03 09:00:26 -05:00
### `ApiResponse`
2022-05-03 10:50:34 -05:00
Each `OnspringClient` method - aside from `CanConnect` - returns an `ApiResponse` object which will have the following properties:
- `statusCode` - The http status code of the response.
- `data` - If the request was successful will contain the response data deserialized to custom python objects.
- `message` - A message that may provide more detail about the requests success or failure.
- `raw` - Exposes the raw response object of the request if you'd like to handle it directly.
The goal with this `ApiResponse` object is to provide the flexibility to do with the response what you'd like as well as already having the raw JSON response deserialized to python objects.
2022-05-03 10:54:10 -05:00
If you do want to handle and/or manipulate the response object yourself you will want to use the value of the `ApiResponse`'s `raw` property which will be a [`Response`](https://docs.python-requests.org/en/latest/user/advanced/#request-and-response-objects) object from the [Requests](https://docs.python-requests.org/en/latest/) library.
2022-05-03 10:50:34 -05:00
2022-05-02 15:34:55 -05:00
## Full API Documentation
2022-05-02 16:48:31 -05:00
You may wish to refer to the full [Onspring API documentation](https://software.onspring.com/hubfs/Training/Admin%20Guide%20-%20v2%20API.pdf) when determining which values to pass as parameters to some of the `OnspringClient` methods. There is also a [swagger page](https://api.onspring.com/swagger/index.html) that you can use for making exploratory requests.
2022-05-02 15:34:55 -05:00
## Example Code
The examples that follow assume you have created an `OnspringClient` as described in the **Start Coding** section.
2022-05-02 16:15:28 -05:00
2022-05-03 11:30:23 -05:00
### Connectivity
#### Verify connectivity
2022-05-02 16:15:28 -05:00
2022-05-02 16:29:34 -05:00
```python
2022-05-02 16:15:28 -05:00
canConnect = client.CanConnect()
if canConnect:
print('Connected successfully')
else:
print('Attempt to connect failed')
```
2022-05-02 16:26:41 -05:00
2022-05-03 11:30:23 -05:00
### Apps
#### Get Apps
2022-05-02 16:26:41 -05:00
2022-05-03 11:04:31 -05:00
Returns a paged collection of apps and/or surveys that can be paged through. By default the page size is 50 and page number is 1.
2022-05-02 16:29:34 -05:00
```python
2022-05-02 16:26:41 -05:00
response = client.GetApps()
print(f'Status Code: {response.statusCode}')
print(f'Page Size: {response.data.pageSize}')
print(f'Page Number: {response.data.pageNumber}')
print(f'Total Pages: {response.data.totalPages}')
print(f'Total Records: {response.data.totalRecords}')
for app in response.data.apps:
print(f'Id: {app.id}')
print(f'Name: {app.name}')
print(f'href: {app.href}')
```
2022-05-03 08:56:47 -05:00
2022-05-03 11:04:31 -05:00
You can set your own page size and page number (max is 1,000) as well.
```python
2022-05-03 14:58:08 -05:00
from Models import PagingRequest
2022-05-03 11:04:31 -05:00
pagingRequest = PagingRequest(1, 100)
response = client.GetApps(pagingRequest)
print(f'Status Code: {response.statusCode}')
print(f'Page Size: {response.data.pageSize}')
print(f'Page Number: {response.data.pageNumber}')
print(f'Total Pages: {response.data.totalPages}')
print(f'Total Records: {response.data.totalRecords}')
for app in response.data.apps:
print(f'Id: {app.id}')
print(f'Name: {app.name}')
print(f'href: {app.href}')
```
2022-05-03 11:30:23 -05:00
#### Get App By Id
2022-05-03 08:56:47 -05:00
2022-05-03 11:08:19 -05:00
Returns an Onspring app or survey according to provided id.
2022-05-03 08:56:47 -05:00
```python
2022-05-03 13:02:30 -05:00
response = client.GetAppById(appId=195)
2022-05-03 11:04:31 -05:00
2022-05-03 11:08:19 -05:00
print(f'Status Code: {response.statusCode}')
print(f'id: {response.data.app.id}')
print(f'Name: {response.data.app.name}')
print(f'href: {response.data.app.href}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
#### Get Apps By Ids
2022-05-03 08:56:47 -05:00
2022-05-03 11:08:19 -05:00
Returns a collection of Onspring apps and/or surveys according to provided ids.
2022-05-03 08:56:47 -05:00
```python
2022-05-03 13:02:30 -05:00
response = client.GetAppsByIds(appIds=[195, 240])
2022-05-03 11:08:19 -05:00
print(f'Status Code: {response.statusCode}')
print(f'Count: {response.data.count}')
for app in response.data.apps:
print(f'Id: {app.id}')
print(f'Name: {app.name}')
print(f'href: {app.href}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
### Fields
2022-05-03 08:56:47 -05:00
2022-05-03 11:30:23 -05:00
#### Helpers
2022-05-03 08:56:47 -05:00
2022-05-03 11:30:23 -05:00
Example `PrintField` method referenced in following examples.
2022-05-03 11:18:35 -05:00
```python
def PrintField(field: Field):
print('Field:')
print(f' Id: {field.id}')
print(f' App Id: {field.appId}')
print(f' Name: {field.name}')
print(f' Type: {field.type}')
print(f' Status: {field.status}')
print(f' IsRequired: {field.isRequired}')
print(f' IsUnique: {field.isUnique}')
if field.type == 'Formula':
print(f' Output Type: {field.outputType}')
if field.outputType == 'ListValue':
print(f' Multiplicity: {field.multiplicity}')
print(' Values:')
for value in field.values:
print(f' {value.AsString()}')
if field.type == 'List':
print(f' Multiplicity: {field.multiplicity}')
print(' Values:')
for value in field.values:
print(f' {value.AsString()}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
#### Get Field By Id
2022-05-03 11:33:59 -05:00
Returns an Onspring field according to provided id.
2022-05-03 11:30:23 -05:00
```python
2022-05-03 13:02:30 -05:00
response = client.GetFieldById(fieldId=9686)
2022-05-03 11:30:23 -05:00
print(f'Status Code: {response.statusCode}')
2022-05-03 11:33:59 -05:00
PrintField(response.data.field)
2022-05-03 11:30:23 -05:00
```
#### Get Fields By Ids
2022-05-03 08:56:47 -05:00
2022-05-03 11:54:23 -05:00
Returns a collection of Onspring fields according to provided ids.
2022-05-03 08:56:47 -05:00
```python
2022-05-03 13:02:30 -05:00
response = client.GetFieldsByIds(fieldIds=[9686, 9687])
2022-05-03 11:40:10 -05:00
print(f'Status Code: {response.statusCode}')
print(f'Count: {response.data.count}')
for field in response.data.fields:
PrintField(field)
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
#### Get Fields By App Id
2022-05-03 08:56:47 -05:00
2022-05-03 11:54:23 -05:00
Returns a paged collection of fields that can be paged through. By default the page size is 50 and page number is 1.
2022-05-03 08:56:47 -05:00
```python
2022-05-03 13:02:30 -05:00
response = client.GetFieldsByAppId(appId=195)
2022-05-03 11:54:23 -05:00
print(f'Status Code: {response.statusCode}')
print(f'Page Size: {response.data.pageSize}')
print(f'Page Number: {response.data.pageNumber}')
print(f'Total Pages: {response.data.totalPages}')
print(f'Total Records: {response.data.totalRecords}')
for field in response.data.fields:
PrintField(field)
```
You can set your own page size and page number (max is 1,000) as well.
```python
2022-05-03 14:58:08 -05:00
from Models import PagingRequest
2022-05-03 11:54:23 -05:00
pagingRequest = PagingRequest(1, 100)
2022-05-03 13:02:30 -05:00
response = client.GetFieldsByAppId(appId=195, pagingRequest)
2022-05-03 11:54:23 -05:00
print(f'Status Code: {response.statusCode}')
print(f'Page Size: {response.data.pageSize}')
print(f'Page Number: {response.data.pageNumber}')
print(f'Total Pages: {response.data.totalPages}')
print(f'Total Records: {response.data.totalRecords}')
for field in response.data.fields:
PrintField(field)
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
### Files
#### Get File Info By Id
2022-05-03 08:56:47 -05:00
2022-05-03 13:06:12 -05:00
Returns the Onspring file's metadata.
2022-05-03 08:56:47 -05:00
```python
2022-05-03 13:02:30 -05:00
response = client.GetFileInfoById(recordId=1, fieldId=6990, fileId=274)
2022-05-03 08:56:47 -05:00
2022-05-03 12:40:57 -05:00
print(f'Status Code: {response.statusCode}')
print(f'Name: {response.data.fileInfo.name}')
print(f'Type: {response.data.fileInfo.type}')
print(f'Owner: {response.data.fileInfo.owner}')
print(f'Content Type: {response.data.fileInfo.contentType}')
print(f'Created Date: {response.data.fileInfo.createdDate}')
print(f'Modified Date: {response.data.fileInfo.modifiedDate}')
print(f'File Href: {response.data.fileInfo.fileHref}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
#### Get File By Id
2022-05-03 08:56:47 -05:00
2022-05-03 13:06:12 -05:00
Returns the file itself.
2022-05-03 08:56:47 -05:00
```python
2022-05-03 13:02:30 -05:00
response = client.GetFileById(recordId=1, fieldId=6990, fileId=274)
2022-05-03 12:40:57 -05:00
print(f'Status Code: {response.statusCode}')
print(f'Name: {response.data.file.name}')
print(f'Content Type: {response.data.file.contentType}')
print(f'Content Length: {response.data.file.contentLength}')
filePath = f'C:\\Users\\sfree\\Documents\\Temp\\{response.data.file.name}'
with open(filePath, "wb") as file:
file.write(response.data.file.content)
print(f'File Location: {filePath}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
#### Save File
2022-05-03 08:56:47 -05:00
```python
2022-05-03 12:54:32 -05:00
from Models import SaveFileRequest
import os
import mimetypes
filePath = 'C:\\Users\\sfree\\Documents\\Temp\\Test Attachment.txt'
fileName = os.path.basename(filePath)
contentType = mimetypes.guess_type(filePath)
request = SaveFileRequest(
2022-05-03 12:57:57 -05:00
recordId=60,
fieldId=6989,
2022-05-03 12:54:32 -05:00
fileName,
filePath,
contentType,
2022-05-03 12:57:57 -05:00
notes='Initial revision',
modifiedDate=datetime.now())
2022-05-03 12:54:32 -05:00
response = client.SaveFile(request)
print(f'Status Code: {response.statusCode}')
print(f'File Id: {response.data.id}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 12:40:57 -05:00
#### Delete File By Id
```python
2022-05-03 13:37:23 -05:00
response = client.DeleteFileById(recordId=60, fieldId=6989, fileId=231)
print(f'Status Code: {response.statusCode}')
print(f'Message: {response.message}')
2022-05-03 12:40:57 -05:00
```
2022-05-03 11:30:23 -05:00
### Lists
#### Add Or Update List Value
2022-05-03 08:56:47 -05:00
2022-05-03 13:54:31 -05:00
To add a list value don't provide an id value.
2022-05-03 08:56:47 -05:00
```python
2022-05-03 13:54:31 -05:00
from Models import ListItemRequest
request = ListItemRequest(
listId=906,
name='Not Started',
id='',
numericValue=0,
color='#ffffff')
response = client.AddOrUpdateListItem(request)
print(f'Status Code: {response.statusCode}')
print(f'Id: {response.data.id}')
```
To update a list value provide an id value.
```python
from Models import ListItemRequest
request = ListItemRequest(
listId=906,
name='Pending',
id='4118d53a-9121-4345-8682-07f23d606daa',
numericValue=0,
color='#ffffff')
response = client.AddOrUpdateListItem(request)
print(f'Status Code: {response.statusCode}')
print(f'Id: {response.data.id}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
#### Delete List Value
2022-05-03 13:40:07 -05:00
```python
2022-05-03 13:54:31 -05:00
response = client.DeleteListItem(listId=906, itemId='36f94d8c-2b9d-465e-9ad1-ede04109efc9')
print(f'Status Code: {response.statusCode}')
print(f'Message: {response.message}')
2022-05-03 13:40:07 -05:00
```
2022-05-03 11:30:23 -05:00
### Records
#### Get Records By App Id
2022-05-03 08:56:47 -05:00
2022-05-03 14:58:08 -05:00
Returns a paged colletion of records that can be paged through. By default the page size is 50 and page number is 1.
2022-05-03 08:56:47 -05:00
```python
2022-05-03 14:58:08 -05:00
request = GetRecordsByAppRequest(appId=195)
response = client.GetRecordsByAppId(request)
print(f'Status Code: {response.statusCode}')
print(f'Page Size: {response.data.pageSize}')
print(f'Page Number: {response.data.pageNumber}')
print(f'Total Pages: {response.data.totalPages}')
print(f'Total Records: {response.data.totalRecords}')
for record in response.data.records:
print(f'AppId: {record.appId}')
print(f'RecordId: {record.recordId}')
for field in record.fields:
print(f'Type: {field.type}')
print(f'FieldId: {field.fieldId}')
print(f'Value: {field.GetResultValueString()}')
```
You can set your own page size and page number (max is 1,000) as well. In addition to specifying what field values to return and in what format (Raw vs. Formatted) to return them.
```python
from Models import PagingRequest, GetRecordsByAppRequest
from Enums import DataFormat
pagingRequest = PagingRequest(1,10)
request = GetRecordsByAppRequest(
appId=195,
fieldIds=[9686],
dataFormat=DataFormat.Formatted.name,
pagingRequest)
response = client.GetRecordsByAppId(request)
print(f'Status Code: {response.statusCode}')
print(f'Page Size: {response.data.pageSize}')
print(f'Page Number: {response.data.pageNumber}')
print(f'Total Pages: {response.data.totalPages}')
print(f'Total Records: {response.data.totalRecords}')
for record in response.data.records:
print(f'AppId: {record.appId}')
print(f'RecordId: {record.recordId}')
for field in record.fields:
print(f'Type: {field.type}')
print(f'FieldId: {field.fieldId}')
print(f'Value: {field.GetResultValueString()}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
#### Get Record By Id
2022-05-03 08:56:47 -05:00
2022-05-03 15:07:38 -05:00
Returns an onspring record based on the provided app and record ids.
2022-05-03 14:58:08 -05:00
2022-05-03 15:07:38 -05:00
```python
from Models import GetRecordByIdRequest
request = GetRecordByIdRequest(appId=195, recordId=60)
response = client.GetRecordById(request)
print(f'Status Code: {response.statusCode}')
print(f'AppId: {response.data.appId}')
print(f'RecordId: {response.data.recordId}')
for field in response.data.fields:
print(f'Type: {field.type}')
print(f'FieldId: {field.fieldId}')
print(f'Value: {field.GetResultValueString()}')
```
You can specify what field values to return and in what format (Raw vs. Formatted) to return them.
```python
from Models import GetRecordByIdRequest
from Enums import DataFormat
request = GetRecordByIdRequest(
appId=195,
recordId=60,
fieldIds=[9686],
dataFormat=DataFormat.Formatted.name)
response = client.GetRecordById(request)
print(f'Status Code: {response.statusCode}')
print(f'AppId: {response.data.appId}')
print(f'RecordId: {response.data.recordId}')
for field in response.data.fields:
print(f'Type: {field.type}')
print(f'FieldId: {field.fieldId}')
print(f'Value: {field.GetResultValueString()}')
2022-05-03 08:56:47 -05:00
```
2022-05-03 11:30:23 -05:00
#### Delete Record By Id
2022-05-03 08:56:47 -05:00
```python
```
2022-05-03 11:30:23 -05:00
#### Get Records By Ids
2022-05-03 08:56:47 -05:00
```python
```
2022-05-03 11:30:23 -05:00
#### Query Records
2022-05-03 08:56:47 -05:00
```python
```
2022-05-03 11:30:23 -05:00
#### Add or Update A Record
2022-05-03 08:56:47 -05:00
```python
```
2022-05-03 11:30:23 -05:00
#### Delete Records By Ids
```python
```
### Reports
#### Get Report By Id
```python
```
#### Get Reports By App Id
2022-05-03 08:56:47 -05:00
```python
```