ozone-typescript-client
ozone-typescript-client
is a typescript module that manage connection and communication to ozone v3 API.
State machine
Interface
export interface OzoneClient extends StateMachine<ClientState> {
readonly config: ClientConfiguration
readonly authInfo?: AuthInfo
readonly lastFailedLogin?: Response<AuthInfo>
readonly isAuthenticated: boolean
readonly isConnected: boolean
start(): Promise<void>
readonly preFilters: InstalledFilter[]
readonly postFilters: InstalledFilter[]
updateWSURL(url: string): void
updateCredentials(ozoneCredentials: OzoneCredentials): void
stop(): Promise<void>
callForResponse<T>(request: Request): Promise<Response<T>>
call<T>(request: Request): Promise<T>
onMessage<M extends DeviceMessage>(messageType: string, callBack: (message: M) => void): ListenerRegistration
onAnyMessage(callBack: (message: DeviceMessage) => void): ListenerRegistration
send(message: DeviceMessage): void
itemClient<T extends Item>(typeIdentifier: string): ItemClient<T>
blobClient(): BlobClient
roleClient(): RoleClient
typeClient(): TypeClient
taskClient(): TaskClient
permissionClient(): PermissionClient
importExportClient(): ImportExportClient
fileTypeClient(): FileTypeClient
insertSessionIdInURL(url: string): string
}
Usage
Initialization example:
import { OzoneClient } from 'ozone-typescript-client'
import UserCredentials = OzoneClient.UserCredentials
import OzoneCredentials = OzoneClient.OzoneCredentials
import ClientConfiguration = OzoneClient.ClientConfiguration
import newOzoneClient = OzoneClient.newOzoneClient
let client: OzoneClient.OzoneClient
async function init() {
const credentials = new UserCredentials('ozoneUser', 'ozonePassword')
const config: ClientConfiguration = {
ozoneURL: `http://my.ozone.domain/ozone`,
ozoneCredentials: credentials
}
client = newOzoneClient(config)
await client.start()
}
Using API client example:
import { OzoneClient } from 'ozone-typescript-client'
import { Video, toPatch } from 'ozone-type'
declare function getClient(): OzoneClient.OzoneClient
const videoClient = getClient().itemClient<Video>('video')
const myOriginalVideo = await videoClient.findOne('uuid-yyyy-zzz')
const videoToUpdate = toPatch(myOriginalVideo)
videoToUpdate.name = 'a new name'
const updatedVideo = await videoClient.save(videoToUpdate)
login application example
import { OzoneClient } from 'ozone-typescript-client'
import UserCredentials = OzoneClient.UserCredentials
import ClientStates = OzoneClient.states
import { getDefaultClient } from 'ozone-default-client'
import once from 'ozone-components-utils/once'
export class PageLoginDefault extends Polymer.Element {
ready(): void {
super.ready()
const defaultClient = getDefaultClient()
defaultClient.onEnterState(ClientStates.STOPPED,() => {
this.set('isConnected', false)
})
defaultClient.onEnterState(ClientStates.AUTHENTICATED,() => {
this.set('isConnected', true)
})
if (defaultClient.authInfo) {
this.set('isConnected', true)
}
}
public async submitForm(e: Event): Promise<void> {
const updateMessagesOnError = once(() => {
this.set('isConnected', false)
const err = defaultClient.lastFailedLogin
if (err && err.status === 400) {
this.set('errorMessage', this.localized.msgEmptyCredentials)
} else if (err && err.status === 403) {
this.set('errorMessage', this.localized.msgInvalidCredentials)
} else {
this.set('errorMessage', this.localized.msgUnknownError)
}
})
const defaultClient = getDefaultClient()
defaultClient.onEnterState(ClientStates.AUTHENTICATION_ERROR, updateMessagesOnError)
const userCredentials: UserCredentials = new UserCredentials(this.username, this.password)
defaultClient.updateCredentials(userCredentials)
try {
await defaultClient.start()
} catch (err) {
}
}
}
Install
$ npm install --save ozone-typescript-client