@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
140 lines • 5.93 kB
JavaScript
/*!
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
/* tslint:disable:no-duplicate-string */
import { DefaultMessagesOptions } from '../../options/request.options';
import { HttpVerb } from '../constants/http-verb';
import { Header } from '../constants/header';
import { ContentType } from '../constants/content-type';
/**
* Handle to send Messages requests.
*/
export class DefaultHttpMessagesHandle {
constructor(requestFactory) {
this.requestFactory = requestFactory;
}
/**
* returns an instance of HttpMessagesHandle based on the Context provided.
*
* @param factory - The factory to build with.
* @returns The HttpMessagesHandle
*/
static getInstance(factory) {
return new DefaultHttpMessagesHandle(factory.buildInstance('things'));
}
/**
* Initiates claiming the specified Thing.
*
* @param thingId - The ID of the Thing to claim.
* @param claimMessage - The message to be sent to the Thing for authentication.
* @param options - Options to use for the request.
* @returns A Promise for the server response
*/
claim(thingId, claimMessage, options) {
const messageOptions = options === undefined ? DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(Header.CONTENT_TYPE, ContentType.JSON);
return this.requestFactory.fetchGenericJsonRequest({
verb: HttpVerb.POST,
id: thingId,
path: 'inbox/claim',
requestOptions: messageOptions,
payload: claimMessage
});
}
/**
* Sends a message to a Thing.
*
* @param thingId - The ID of the Thing to message.
* @param messageSubject - The subject of the message to send.
* @param message - The message to send.
* @param contentType - The content type of the message.
* @param options?? - Options to use for the request.
* @returns A Promise for the server response
*/
messageToThing(thingId, messageSubject, message, contentType = ContentType.JSON, options) {
const messageOptions = options === undefined ? DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(Header.CONTENT_TYPE, contentType);
return this.requestFactory.fetchGenericJsonRequest({
verb: HttpVerb.POST,
id: thingId,
path: `inbox/messages/${messageSubject}`,
requestOptions: messageOptions,
payload: message
});
}
/**
* Sends a message from a Thing.
*
* @param thingId - The ID of the Thing to message from.
* @param messageSubject - The subject of the message to send.
* @param message - The message to send.
* @param contentType - The content type of the message.
* @param options?? - Options to use for the request.
* @returns A Promise for the server response
*/
messageFromThing(thingId, messageSubject, message, contentType = ContentType.JSON, options) {
const messageOptions = options === undefined ? DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(Header.CONTENT_TYPE, contentType);
return this.requestFactory.fetchGenericJsonRequest({
verb: HttpVerb.POST,
id: thingId,
path: `outbox/messages/${messageSubject}`,
requestOptions: messageOptions,
payload: message
});
}
/**
* Sends a message to a Feature.
*
* @param thingId - The ID of the Thing the Feature belongs to.
* @param featureId - The ID of the Feature to message.
* @param messageSubject - The subject of the message to send.
* @param message - The message to send.
* @param contentType - The content type of the message.
* @param options?? - Options to use for the request.
* @returns A Promise for the server response
*/
messageToFeature(thingId, featureId, messageSubject, message, contentType = ContentType.JSON, options) {
const messageOptions = options === undefined ? DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(Header.CONTENT_TYPE, contentType);
return this.requestFactory.fetchGenericJsonRequest({
verb: HttpVerb.POST,
id: thingId,
path: `features/${featureId}/inbox/messages/${messageSubject}`,
requestOptions: messageOptions,
payload: message
});
}
/**
* Sends a message from a Feature.
*
* @param thingId - The ID of the Thing the Feature belongs to.
* @param featureId - The ID of the Feature to message from.
* @param messageSubject - The subject of the message to send.
* @param message - The message to send.
* @param contentType - The content type of the message.
* @param options?? - Options to use for the request.
* @returns A Promise for the server response
*/
messageFromFeature(thingId, featureId, messageSubject, message, contentType = ContentType.JSON, options) {
const messageOptions = options === undefined ? DefaultMessagesOptions.getInstance() : options;
messageOptions.addHeader(Header.CONTENT_TYPE, contentType);
return this.requestFactory.fetchGenericJsonRequest({
verb: HttpVerb.POST,
id: thingId,
path: `features/${featureId}/outbox/messages/${messageSubject}`,
requestOptions: messageOptions,
payload: message
});
}
}
//# sourceMappingURL=messages-http.js.map