@eclipse-ditto/ditto-javascript-client-dom
Version:
DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.
75 lines • 2.73 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
*/
import { HttpRequestSenderBuilder } from './request-factory/http-request-sender';
import { DefaultDittoHttpClient } from './ditto-client-http';
import { ImmutableURL } from '../auth/auth-provider';
import { AbstractBuilder } from './builder-steps';
/**
* Implementation of all the methods to build a Context.
*/
export class HttpClientBuilder extends AbstractBuilder {
constructor(requester) {
super();
this.requester = requester;
this.customHandles = {};
}
/**
* Build a new HttpClientBuilder.
*
* @param requester - The requester to use.
*/
static newBuilder(requester) {
return new HttpClientBuilder(requester);
}
finalize() {
return this;
}
// TODO: rebuild so that DittoHttpClient interface can be used
build() {
const url = this.buildUrl();
return DefaultDittoHttpClient.getInstance(new HttpRequestSenderBuilder(this.requester, url, this.authProviders), this.customHandles);
}
buildClient(tls, domain, apiVersion, authProviders) {
this.tls = tls;
this.domain = domain;
this.apiVersion = apiVersion;
this.authProviders = authProviders;
return this.build();
}
buildUrl() {
const protocol = this.tls ? 'https' : 'http';
const path = (this.customPath === undefined) ? '/api' : this.customPath;
return ImmutableURL.newInstance(protocol, this.domain, `${path}/${this.apiVersion}`);
}
withCustomThingsHandle(factory) {
this.customHandles = Object.assign(this.customHandles, { thingsHandle: factory });
return this;
}
withCustomFeaturesHandle(factory) {
this.customHandles = Object.assign(this.customHandles, { featuresHandle: factory });
return this;
}
withCustomMessagesHandle(factory) {
this.customHandles = Object.assign(this.customHandles, { messagesHandle: factory });
return this;
}
withCustomPoliciesHandle(factory) {
this.customHandles = Object.assign(this.customHandles, { policiesHandle: factory });
return this;
}
withCustomSearchHandle(factory) {
this.customHandles = Object.assign(this.customHandles, { searchHandle: factory });
return this;
}
}
//# sourceMappingURL=http-client-builder.js.map