Files
onspring-api-sdk-python/Models.py
T

523 lines
17 KiB
Python
Raw Normal View History

import datetime
2022-04-16 22:55:22 -05:00
import uuid
2022-04-19 21:09:55 -05:00
from requests import Response
2022-04-18 15:07:31 -05:00
from Enums import *
from decimal import Decimal
from datetime import datetime
from Helpers import parseDate
2022-04-17 00:31:43 -05:00
# generic
2022-04-15 14:44:27 -05:00
class ApiResponse:
2022-04-19 21:09:55 -05:00
def __init__(self, statusCode=None, data=None, message=None, raw=None):
2022-04-15 14:44:27 -05:00
self.statusCode = statusCode
self.isSuccessful = int(statusCode) < 400
self.data = data
self.message = message
2022-04-19 21:09:55 -05:00
self.raw = raw
2022-04-15 14:44:27 -05:00
class PagingRequest:
def __init__(self, pageNumber: int, pageSize: int):
2022-04-15 14:44:27 -05:00
self.pageNumber = pageNumber
self.pageSize = pageSize
#app specific
2022-04-15 14:44:27 -05:00
class App:
def __init__(self, href: str, id: int, name: str):
2022-04-15 14:44:27 -05:00
self.href = href
self.id = id
self.name = name
class GetAppsResponse:
def __init__(self, pageNumber: int, pageSize: int, totalPages:int , totalRecords: int, apps: list[App]):
2022-04-15 14:44:27 -05:00
self.pageNumber = pageNumber
self.pageSize = pageSize
self.totalPages = totalPages
self.totalRecords = totalRecords
self.apps = apps
class GetAppByIdResponse:
def __init__(self, app: App):
self.app = app
class GetAppsByIdsResponse:
def __init__(self, count: int, apps: list[App]):
2022-04-15 14:44:27 -05:00
self.count = count
self.apps = apps
# field specific
2022-04-15 14:44:27 -05:00
class Field:
def __init__(self, id: int, appId: int, name: str, type: str, status: str, isRequired: bool, isUnique: bool):
2022-04-15 14:44:27 -05:00
self.id = id
self.appId = appId
self.name = name
self.type = type
self.status = status
self.isRequired = isRequired
self.isUnique = isUnique
class GetFieldByIdResponse:
def __init__(self, field: Field):
self.field = field
class GetFieldsByIdsResponse:
def __init__(self, count: int, fields: list[Field]):
2022-04-15 14:44:27 -05:00
self.count = count
self.fields = fields
class GetFieldsByAppIdResponse:
def __init__(self, pageNumber: int, pageSize: int, totalPages: int, totalRecords: int, fields: list[Field]):
2022-04-15 14:44:27 -05:00
self.pageNumber = pageNumber
self.pageSize = pageSize
self.totalPages = totalPages
self.totalRecords = totalRecords
self.fields = fields
# file specific
class File:
def __init__(self, name: str, contentType: str, contentLength: int, content: bytes):
self.name = name
self.contentType = contentType
self.contentLength = contentLength
self.content = content
class FileInfo:
2022-04-18 15:07:31 -05:00
def __init__(self, type: str, contentType: str, name: str, createdDate: datetime, modifiedDate: datetime, owner: str, fileHref: str):
self.type = type
self.contentType = contentType
self.name = name
self.createdDate = createdDate
self.modifiedDate = modifiedDate
self.owner = owner
self.fileHref = fileHref
class GetFileInfoByIdResponse:
def __init__(self, fileInfo: FileInfo):
self.fileInfo = fileInfo
class GetFileByIdResponse:
def __init__(self, file: File):
self.file = file
class SaveFileRequest:
def __init__(self, recordId: int, fieldId: int, fileName: str, filePath: str, contentType: str, notes: str=None, modifiedDate: datetime=None):
self.recordId = recordId
self.fieldId = fieldId
self.notes = notes
self.modifiedDate = modifiedDate
self.fileName = fileName
self.filePath = filePath
self.contentType = contentType
class SaveFileResponse:
def __init__(self, id: int):
2022-04-16 22:55:22 -05:00
self.id = id
# list specific
class ListItemRequest:
def __init__(self, listId: int, name: str, id: uuid=None, numericValue: int=None, color: str=None):
self.listId = listId
self.name = name
self.id = id
self.numericValue = numericValue
self.color = color
class AddOrUpdateListItemResponse:
def __init__(self, id: uuid):
2022-04-17 00:31:43 -05:00
self.id = id
# record specific
2022-04-18 10:39:20 -05:00
class RecordFieldValue:
2022-04-19 21:09:55 -05:00
def __init__(self, fieldId: int, value: str, type: str=None):
2022-04-18 10:39:20 -05:00
self.type = type
self.fieldId = fieldId
self.value = value
def AsString(self):
if self.type != ResultValueType.String.name:
return None
return StringFieldValue(self.fieldId, self.value).value
2022-04-18 10:39:20 -05:00
2022-04-18 16:13:34 -05:00
def AsInteger(self):
2022-04-18 10:39:20 -05:00
if self.type != ResultValueType.Integer.name:
return None
2022-04-20 13:45:34 -05:00
return IntegerFieldValue(self.fieldId, int(self.value)).value
2022-04-18 10:39:20 -05:00
def AsDecimal(self):
if self.type != ResultValueType.Decimal.name:
return None
2022-04-20 13:45:34 -05:00
return DecimalFieldValue(self.fieldId, Decimal(self.value)).value
2022-04-18 10:39:20 -05:00
def AsDate(self):
if self.type != ResultValueType.Date.name:
return None
2022-04-18 15:07:31 -05:00
date = parseDate(self.value)
2022-04-18 10:39:20 -05:00
2022-04-20 13:45:34 -05:00
return DateFieldValue(self.fieldId, date).value
2022-04-18 10:39:20 -05:00
def AsGuid(self):
if self.type != ResultValueType.Guid.name:
return None
2022-04-20 13:45:34 -05:00
return GuidFieldValue(self.fieldId, uuid.UUID(self.value)).value
2022-04-18 10:39:20 -05:00
2022-04-18 15:07:31 -05:00
def AsTimeSpan(self):
if self.type != ResultValueType.TimeSpan.name:
return None
value = dict(self.value)
quantity = value.get('quantity')
increment = value.get('increment')
recurrence = value.get('recurrence')
endByDate = value.get('endByDate')
endAfterOccurrences = value.get('endAfterOccurrences')
endByDate = parseDate(endByDate)
data = TimeSpanData(
quantity,
increment,
recurrence,
endByDate,
endAfterOccurrences)
2022-04-20 13:45:34 -05:00
return TimeSpanValue(self.fieldId, data).value
2022-04-18 15:07:31 -05:00
def AsStringList(self):
if self.type != ResultValueType.StringList.name:
return None
2022-04-20 13:45:34 -05:00
strings = [str(string) for string in self.value]
return StringListValue(self.fieldId, strings).value
2022-04-18 15:07:31 -05:00
def AsIntegerList(self):
if self.type != ResultValueType.IntegerList.name:
return None
2022-04-20 13:45:34 -05:00
integers = [int(integer) for integer in self.value]
2022-04-18 15:07:31 -05:00
2022-04-20 13:45:34 -05:00
return IntegerListValue(self.fieldId, integers).value
2022-04-18 15:07:31 -05:00
def AsGuidList(self):
if self.type != ResultValueType.GuidList.name:
return None
guids = []
for guid in self.value:
guids.append(uuid.UUID(guid))
2022-04-20 13:45:34 -05:00
return GuidListValue(self.fieldId, guids).value
2022-04-18 15:07:31 -05:00
def AsAttachmentList(self):
2022-04-18 16:13:34 -05:00
if self.type != ResultValueType.AttachmentList.name:
return None
attachments = []
for attachment in self.value:
attachment = dict(attachment)
attachment = Attachment(
attachment.get('fileId'),
attachment.get('fileName'),
attachment.get('notes'),
attachment.get('storageLocation'))
attachments.append(attachment)
2022-04-20 13:45:34 -05:00
return AttachmentListValue(self.fieldId, attachments).value
2022-04-18 15:07:31 -05:00
def AsScoringGroupList(self):
2022-04-18 16:13:34 -05:00
if self.type != ResultValueType.ScoringGroupList.name:
return
scoringGroups = []
for scoringGroup in self.value:
scoringGroup = dict(scoringGroup)
scoringGroup = ScoringGroup(
uuid.UUID(scoringGroup.get('listValueId')),
scoringGroup.get('name'),
Decimal(scoringGroup.get('score')),
Decimal(scoringGroup.get('maximumScore')))
scoringGroups.append(scoringGroup)
2022-04-20 13:45:34 -05:00
return ScoringGroupListValue(self.fieldId, scoringGroups).value
2022-04-18 15:07:31 -05:00
def AsFileList(self):
2022-04-18 16:13:34 -05:00
if self.type != ResultValueType.FileList.name:
return None
2022-04-20 13:45:34 -05:00
files = [int(file) for file in self.value]
2022-04-18 16:13:34 -05:00
2022-04-20 13:45:34 -05:00
return FileListValue(self.fieldId, files).value
2022-04-18 16:13:34 -05:00
def getValue(self):
if self.type == ResultValueType.String.name:
return self.AsString()
elif self.type == ResultValueType.Integer.name:
return self.AsInteger()
elif self.type == ResultValueType.Decimal.name:
return self.AsDecimal()
elif self.type == ResultValueType.Date.name:
return self.AsDate()
elif self.type == ResultValueType.TimeSpan.name:
return self.AsTimeSpan()
elif self.type == ResultValueType.Guid.name:
return self.AsGuid()
elif self.type == ResultValueType.StringList.name:
return self.AsStringList()
elif self.type == ResultValueType.IntegerList.name:
return self.AsIntegerList()
elif self.type == ResultValueType.GuidList.name:
return self.AsGuidList()
elif self.type == ResultValueType.AttachmentList.name:
return self.AsAttachmentList()
elif self.type == ResultValueType.ScoringGroupList.name:
return self.AsScoringGroupList()
elif self.type == ResultValueType.FileList.name:
return self.AsFileList()
else:
return None
2022-04-18 10:39:20 -05:00
2022-04-20 13:45:34 -05:00
def GetResultValueString(self):
match self.type:
case ResultValueType.String.name:
return self.AsString()
case ResultValueType.Integer.name:
return self.AsInteger()
case ResultValueType.Decimal.name:
return self.AsDecimal()
case ResultValueType.Date.name:
return self.AsDate()
case ResultValueType.TimeSpan.name:
data = self.AsTimeSpan()
return f'Quantity: {data.quantity}, Increment: {data.increment}, Recurrence: {data.recurrence}, EndByDate: {data.endByDate}, EndAfterOccurrences: {data.endAfterOccurrences}'
case ResultValueType.Guid.name:
return self.AsGuid()
case ResultValueType.StringList.name:
data = self.AsStringList()
return f'{",".join(data)}'
case ResultValueType.IntegerList.name:
data = self.AsIntegerList()
return f'{",".join([str(i) for i in data])}'
case ResultValueType.GuidList.name:
data = self.AsGuidList()
return f'{",".join([str(guid) for guid in data])}'
case ResultValueType.AttachmentList.name:
data = self.AsAttachmentList()
strings = []
for attachment in data:
string = f'FileId: {attachment.fileId}, FileName: {attachment.fileName}, Notes: {attachment.notes}, StorageLocation: {attachment.storageLocation}'
strings.append(string)
return f'{"; ".join(strings)}'
case ResultValueType.ScoringGroupList.name:
data = self.AsScoringGroupList()
strings = []
for scoringGroup in data:
string = f'ListValueId: {scoringGroup.listValueId}, Name: {scoringGroup.name}, Score: {scoringGroup.score}, Max Score: {scoringGroup.maximumScore}'
strings.append(string)
return f'{"; ".join(strings)}'
case ResultValueType.FileList.name:
data = self.AsFileList()
return f'{",".join([str(i) for i in data])}'
2022-04-18 10:39:20 -05:00
class Record:
2022-04-19 21:09:55 -05:00
def __init__(self, appId: int, fields: list[RecordFieldValue], recordId: int=None):
2022-04-18 10:39:20 -05:00
self.appId = appId
self.recordId = recordId
2022-04-18 15:07:31 -05:00
self.fields = fields
2022-04-18 10:39:20 -05:00
2022-04-17 00:31:43 -05:00
class GetRecordsByAppRequest:
def __init__(self, appId: int, fieldIds: list[int]=[], dataFormat: str=DataFormat.Raw.name, pagingRequest: PagingRequest=PagingRequest(1,50)):
self.appId = appId
self.fieldIds = fieldIds
self.dataFormat = dataFormat
self.pageSize = pagingRequest.pageSize
2022-04-18 10:39:20 -05:00
self.pageNumber = pagingRequest.pageNumber
2022-04-19 21:09:55 -05:00
class QueryRecordsRequest:
def __init__(self, appId: int, filter: str, fieldIds: list[int]=[], dataFormat: str=DataFormat.Raw.name, pagingRequest: PagingRequest=PagingRequest(1,50)):
self.appId = appId
self.filter = filter
self.fieldIds = fieldIds
self.dataFormat = dataFormat
self.pagingRequest = pagingRequest
class GetRecordsResponse:
2022-04-18 10:39:20 -05:00
def __init__(self, pageNumber: int, pageSize: int, totalPages: int, totalRecords: int, records: list[Record]):
self.pageNumber = pageNumber
self.pageSize = pageSize
self.totalPages = totalPages
self.totalRecords = totalRecords
2022-04-19 21:09:55 -05:00
self.records = records
class GetRecordByIdRequest:
def __init__(self, appId: int, recordId: int, fieldIds: list[int]=[], dataFormat: str=DataFormat.Raw.name):
self.appId = appId
self.recordId = recordId
self.fieldIds = fieldIds
self.dataFormat = dataFormat
class GetBatchRecordsRequest:
def __init__(self, appId: int, recordIds: list[int], fieldIds: list[int]=[], dataFormat: str=DataFormat.Raw.name):
self.appId = appId
self.recordIds = recordIds
self.fieldIds = fieldIds
self.dataFormat = dataFormat
class GetBatchRecordsResponse:
def __init__(self, count: int, records: list[Record]):
self.count = count
self.records = records
class AddOrUpdateRecordResponse:
def __init__(self, id: int, warnings: list[str]=[]):
self.id = id
self.warnings = warnings
2022-04-20 13:45:34 -05:00
class DeleteBatchRecordsRequest:
def __init__(self, appId: int, recordIds: list[int]):
self.id = id
self.recordIds = recordIds
# field types
class StringFieldValue(RecordFieldValue):
def __init__(self, fieldId: int, value):
self.type = ResultValueType.String.name
RecordFieldValue.__init__(self, fieldId, value, self.type)
2022-04-20 13:45:34 -05:00
class IntegerFieldValue(RecordFieldValue):
def __init__(self, fieldId: int, value: int):
self.type = ResultValueType.Integer.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
2022-04-20 13:45:34 -05:00
class DecimalFieldValue(RecordFieldValue):
def __init__(self, fieldId: int, value: Decimal):
self.type = ResultValueType.Decimal.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
2022-04-20 13:45:34 -05:00
class DateFieldValue(RecordFieldValue):
def __init__(self, fieldId: int, value: datetime):
self.type = ResultValueType.Date.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
2022-04-20 13:45:34 -05:00
class GuidFieldValue(RecordFieldValue):
def __init__(self, fieldId: int, value: uuid.UUID):
self.type = ResultValueType.Guid.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
class TimeSpanData:
def __init__(self, quantity: Decimal, increment: Increment, recurrence: Recurrence=None, endByDate: datetime=None, endAfterOccurrences: int=None):
self.quantity = quantity
self.increment = increment
self.recurrence = recurrence
self.endByDate = endByDate
self.endAfterOccurrences = endAfterOccurrences
def AsString(self):
if self.endByDate != None:
formattedDate = self.endByDate.strftime("%m/%d/%Y %I:%M %p")
return f'Every {self.quantity} {self.increment} End By {formattedDate}'
elif self.endAfterOccurrences != None:
return f'Every {self.quantity} {self.increment} End After {self.endAfterOccurrences}'
else:
return f'{self.quantity} {self.increment}'
2022-04-20 13:45:34 -05:00
class TimeSpanValue(RecordFieldValue):
def __init__(self, fieldId: int, value: TimeSpanData):
self.type = ResultValueType.TimeSpan.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
2022-04-20 13:45:34 -05:00
class StringListValue(RecordFieldValue):
def __init__(self, fieldId: int, value: list[str]):
self.type = ResultValueType.StringList.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
2022-04-20 13:45:34 -05:00
class IntegerListValue(RecordFieldValue):
def __init__(self, fieldId: int, value: list[int]):
self.type = ResultValueType.IntegerList.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
2022-04-20 13:45:34 -05:00
class GuidListValue(RecordFieldValue):
def __init__(self, fieldId: int, value: list[uuid.UUID]):
self.type = ResultValueType.GuidList.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
class Attachment:
def __init__(self, fileId: int, fileName: str, notes: str, storageLocation: str):
self.fileId = fileId
self.fileName = fileName
self.notes = notes
self.storageLocation = storageLocation
2022-04-20 13:45:34 -05:00
class AttachmentListValue(RecordFieldValue):
def __init__(self, fieldId: int, value: list[Attachment]):
self.type = ResultValueType.AttachmentList.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
2022-04-20 13:45:34 -05:00
class FileListValue(RecordFieldValue):
def __init__(self, fieldId: int, value: list[int]):
self.type = ResultValueType.FileList.name
2022-04-20 13:45:34 -05:00
RecordFieldValue.__init__(self, fieldId, value, self.type)
class ScoringGroup:
def __init__(self, listValueId: uuid.UUID, name: str, score: Decimal, maximumScore: Decimal):
self.listValueId = listValueId
self.name = name
self.score = score
self.maximumScore = maximumScore
2022-04-20 13:45:34 -05:00
class ScoringGroupListValue(RecordFieldValue):
def __init__(self, fieldId: int, value: list[ScoringGroup]):
self.type = ResultValueType.ScoringGroupList.name
RecordFieldValue.__init__(self, fieldId, value, self.type)