UNPKG

@c8y/client

Version:

Client application programming interface to access the Cumulocity IoT-Platform REST services.

190 lines 7.85 kB
import { IAuthentication, ICredentials, IFetchResponse } from './core'; import { Realtime } from './realtime'; import { FetchClient, AlarmService, ApplicationService, AuditService, DeviceRegistrationBulkService, DeviceRegistrationService, EventService, InventoryBinaryService, InventoryRoleService, InventoryService, MeasurementService, OperationBulkService, OperationService, SystemOptionsService, TenantOptionsService, TenantSecurityOptionsService, TenantLoginOptionsService, TenantService, UserGroupService, UserRoleService, UserService, IdentityService, SmartGroupsService, SmartRulesService } from './services'; export interface IBootstrapOptions { deviceId: string; basicAuthToken: string; timeout?: number; expire?: number; baseUrl?: string; } export declare class Client { /** * Authenticates the given user. Determines the tenant by itself via a call to tenant/currentTenant. * * **Example** * ```typescript * * let client: Client; * (async () => { * client = await Client.authenticate({ * user: 'testuser', * password: 'password1337!' * }, 'https://acme.cumulocity.com'); * * //you have access to the client api now * const { data, paging, res }); = await client.inventory.list({ pageSize: 100 }); * })(); * ``` */ static authenticate(credentials: ICredentials, baseUrl?: string): Promise<Client>; /** * Authenticates the given user via OAuth Internal. Determines the tenant by itself via a call to tenant/currentTenant. * Login call goes to `/tenant/oauth/token` and the retrieved token will be added in the `Authorization` header to subsequent requests. * * **Example** * ```typescript * * let client: Client; * (async () => { * client = await Client.authenticateViaOAuthInternal({ * user: 'testuser', * password: 'password1337!' * }, 'https://acme.cumulocity.com'); * * //you have access to the client api now * const { data, paging, res }); = await client.inventory.list({ pageSize: 100 }); * })(); * ``` */ static authenticateViaOAuthInternal(credentials: ICredentials, baseUrl?: string): Promise<Client>; /** * Authenticates the given user via OAuth Internal. Determines the tenant by itself via a call to tenant/currentTenant. * Login call goes to `/tenant/oauth` and the retrieved token will be attached as a Cookie in browser environments to subsequent requests (using 'CookieAuth'). * In none browser environments (e.g. nodejs) the retrieved token will be added in the `Authorization` header to subsequent requests (using 'NodeJSCookieAuth'). * * **Example** * ```typescript * * let client: Client; * (async () => { * client = await Client.authenticateViaOAuthInternalCookie({ * user: 'testuser', * password: 'password1337!' * }, 'https://acme.cumulocity.com'); * * //you have access to the client api now * const { data, paging, res }); = await client.inventory.list({ pageSize: 100 }); * })(); * ``` */ static authenticateViaOAuthInternalCookie(credentials: ICredentials, baseUrl?: string): Promise<Client>; /** * Allows to use http to register a device on the platform. * * **Deprecated** Please use MQTT to bootstrap a device. */ static deviceBootstrap(options: IBootstrapOptions): Promise<Client>; /** * Retrieves microservice credentials for the subscribed tenants * using provided bootstrap credentials * * **Example** * ```typescript * * (async () => { * const subscriptions = await Client.getMicroserviceSubscriptions({ * tenant: process.env.C8Y_BOOTSTRAP_TENANT, * user: process.env.C8Y_BOOTSTRAP_USER, * password: process.env.C8Y_BOOTSTRAP_PASSWORD * }, process.env.C8Y_BASEURL); * * const clients = subscriptions.map(subscription => new Client(new BasicAuth(subscription), process.env.C8Y_BASEURL)); * // you have access to the client api now * const promiseArray = clients.map(client => client.options.tenant.detail({ * category: process.env.APPLICATION_KEY, * key: 'someSetting' * })); * })(); * ``` */ static getMicroserviceSubscriptions(bootstrapCredentials: ICredentials, baseUrl: string): Promise<ICredentials[]>; /** * Performs the OAuth Internal login. * * If the parameter `bearerAuth` is set to true (default), the `/tenant/oauth/token` endpoint is called and a string containing the token returned. * * If the parameter `bearerAuth` is set to false, the `/tenant/oauth` endpoint is called and a Cookie should be set and the response returned. * The response can be used in none browser environments to extract the `set-cookie` header from it. * * **Example** * ```typescript * * (async () => { * const client = await Client.loginViaOAuthInternal({ * tenant: process.env.C8Y_BOOTSTRAP_TENANT, * user: process.env.C8Y_BOOTSTRAP_USER, * password: process.env.C8Y_BOOTSTRAP_PASSWORD * }, true, process.env.C8Y_BASEURL); * * // you have access to the client api now * const option = client.options.tenant.detail({ * category: process.env.APPLICATION_KEY, * key: 'someSetting' * }); * })(); * ``` */ static loginViaOAuthInternal(credentials: ICredentials, bearerAuth?: true, baseUrl?: string): Promise<string>; static loginViaOAuthInternal(credentials: ICredentials, bearerAuth: false, baseUrl?: string): Promise<IFetchResponse>; private static getCurrentTenant; alarm: AlarmService; application: ApplicationService; audit: AuditService; core: FetchClient; deviceRegistration: DeviceRegistrationService; deviceRegistrationBulk: DeviceRegistrationBulkService; event: EventService; inventory: InventoryService; inventoryRole: InventoryRoleService; inventoryBinary: InventoryBinaryService; measurement: MeasurementService; operation: OperationService; operationBulk: OperationBulkService; options: { security: TenantSecurityOptionsService; system: SystemOptionsService; login: TenantLoginOptionsService; tenant: TenantOptionsService; }; realtime: Realtime; role: InventoryRoleService; tenant: TenantService; user: UserService; userGroup: UserGroupService; userRole: UserRoleService; identity: IdentityService; smartGroups: SmartGroupsService; smartRules: SmartRulesService; /** * Initializes a new Client, which allows to request data from the API. Differently * to Client.authenticate([...]) it needs a tenant given and does not verify if the * login is correct. * * **Example** * ```typescript * * const auth = new BasicAuth({ * user: 'youruser', * password: 'yourpassword', * tenant: 'acme' * }); // use CookieAuth() if your platform uses oauth (only in browser!) * * const baseUrl = 'https://acme.cumulocity.com'; * const client = new Client(auth, baseUrl); * (async () => { * const { data, paging, res }); = await client.inventory.list({ pageSize: 100 }); * })(); * ``` * * @param auth The Authentication strategy to use (e.g. new BasicAuth()) * @param baseUrl The URL to request (optional in browser, mandatory in node) */ constructor(auth: IAuthentication, baseUrl?: string); /** * Allows to change the current Authentication * @param auth The new Authentication information. */ setAuth(auth: IAuthentication): void; } //# sourceMappingURL=Client.d.ts.map