UNPKG

@eclipse-ditto/ditto-javascript-client-dom

Version:

DOM implementation of Eclipse Ditto JavaScript API to be used in browsers.

342 lines 12.1 kB
/*! * 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 { Entries, Entry, Policy, Resource, Resources, Subject, Subjects } from '../../model/policies.model'; import { HttpVerb } from '../constants/http-verb'; /** * Handle to send Policies requests. */ export class DefaultPoliciesHandle { constructor(requestFactory) { this.requestFactory = requestFactory; } /** * returns an instance of PoliciesHandle using the provided RequestSender. * * @param builder - The builder for the RequestSender to work with. * @returns The PoliciesHandle */ static getInstance(builder) { return new DefaultPoliciesHandle(builder.buildInstance('policies')); } /** * Gets a Policy. * * @param policyId - The ID of the Policy to get. * @param options - Options to use for the request. * @returns A Promise for the Policy */ getPolicy(policyId, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => Policy.fromObject(o, policyId), id: policyId, requestOptions: options }); } /** * Gets the Entries of a Policy. * * @param policyId - The ID of the Policy. * @param options - Options to use for the request. * @returns A Promise for the Entries */ getEntries(policyId, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: Entries.fromObject, id: policyId, path: 'entries', requestOptions: options }); } /** * Gets an Entry of a Policy. * * @param policyId - The ID of the Policy. * @param label - The label of the Entry to get. * @param options - Options to use for the request. * @returns A Promise for the Entry */ getEntry(policyId, label, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => Entry.fromObject(o, label), id: policyId, path: `entries/${label}`, requestOptions: options }); } /** * Gets the Subjects of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param options - Options to use for the request. * @returns A Promise for the Subjects */ getSubjects(policyId, label, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: Subjects.fromObject, id: policyId, path: `entries/${label}/subjects`, requestOptions: options }); } /** * Gets a Subject of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param subjectId - The ID of the Subject to get. * @param options - Options to use for the request. * @returns A Promise for the Subject */ getSubject(policyId, label, subjectId, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => Subject.fromObject(o, subjectId), id: policyId, path: `entries/${label}/subjects/${subjectId}`, requestOptions: options }); } /** * Gets the Resources of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param options - Options to use for the request. * @returns A Promise for the Resources */ getResources(policyId, label, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: Resources.fromObject, id: policyId, path: `entries/${label}/resources`, requestOptions: options }); } /** * Gets a Resource of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param resourcePath - The path to the Resource. * @param options - Options to use for the request. * @returns A Promise for the Resource */ getResource(policyId, label, resourcePath, options) { return this.requestFactory.fetchJsonRequest({ verb: HttpVerb.GET, parser: o => Resource.fromObject(o, resourcePath), id: policyId, path: `entries/${label}/resources/${resourcePath}`, requestOptions: options }); } /** * Adds or updates a Policy. * * @param policy - The new Policy. * @param options - Options to use for the request. * @returns A Promise for a response containing the new Policy if provided by the response */ putPolicy(policy, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => Policy.fromObject(o, policy.id), id: policy.id, requestOptions: options, payload: policy.toObject() }); } /** * Adds or updates the Entries of a Policy. * * @param policyId - The ID of the Policy. * @param entries - The new Entries. * @param options - Options to use for the request. * @returns A Promise a response containing the new Entries if provided by the response */ putEntries(policyId, entries, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: Entries.fromObject, id: policyId, path: 'entries', requestOptions: options, payload: Entries.toObject(entries) }); } /** * Adds or updates an Entry of a Policy. * * @param policyId - The ID of the Policy. * @param entry - The new Entry. * @param options - Options to use for the request. * @returns A Promise for a response containing the new Entry if provided by the response */ putEntry(policyId, entry, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => Entry.fromObject(o, entry.id), id: policyId, path: `entries/${entry.id}`, requestOptions: options, payload: entry.toObject() }); } /** * Adds or updates the Subjects of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param subjects - The new Subjects. * @param options - Options to use for the request. * @returns A Promise for a response containing the new Subjects if provided by the response */ putSubjects(policyId, label, subjects, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: Subjects.fromObject, id: policyId, path: `entries/${label}/subjects`, requestOptions: options, payload: Subjects.toObject(subjects) }); } /** * Adds or updates a Subject of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param subject - The new Subject. * @param options - Options to use for the request. * @returns A Promise for a response containing the new Subject if provided by the response */ putSubject(policyId, label, subject, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => Subject.fromObject(o, subject.id), id: policyId, path: `entries/${label}/subjects/${subject.id}`, requestOptions: options, payload: subject.toObject() }); } /** * Adds or updates the Resources of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param resources - The new Resources. * @param options - Options to use for the request. * @returns A Promise for a response containing the new Resources if provided by the response */ putResources(policyId, label, resources, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: Resources.fromObject, id: policyId, path: `entries/${label}/resources`, requestOptions: options, payload: Resources.toObject(resources) }); } /** * Adds or updates a Resource of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param resource - The new Resource. * @param options - Options to use for the request. * @returns A Promise for a response containing the new Resource if provided by the response */ putResource(policyId, label, resource, options) { return this.requestFactory.fetchPutRequest({ verb: HttpVerb.PUT, parser: o => Resource.fromObject(o, resource.id), id: policyId, path: `entries/${label}/resources/${resource.id}`, requestOptions: options, payload: resource.toObject() }); } /** * Deletes a Policy. * * @param policyId - The ID of the Policy to delete. * @param options - Options to use for the request. * @returns A Promise for the response */ deletePolicy(policyId, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: policyId, requestOptions: options }); } /** * Deletes an Entry of a Policy. * * @param policyId - The ID of the Policy. * @param label - The label of the Entry to delete. * @param options - Options to use for the request. * @returns A Promise for the response */ deleteEntry(policyId, label, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: policyId, path: `entries/${label}`, requestOptions: options }); } /** * Deletes a Subject of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param subjectId - The ID of the Subject to delete. * @param options - Options to use for the request. * @returns A Promise for the response */ deleteSubject(policyId, label, subjectId, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: policyId, path: `entries/${label}/subjects/${subjectId}`, requestOptions: options }); } /** * Deletes a Subject of an Entry. * * @param policyId - The ID of the Policy the Entry belongs to. * @param label - The label of the Entry. * @param resourcePath - The path to the Resource to delete. * @param options - Options to use for the request. * @returns A Promise for the response */ deleteResource(policyId, label, resourcePath, options) { return this.requestFactory.fetchRequest({ verb: HttpVerb.DELETE, id: policyId, path: `entries/${label}/resources/${resourcePath}`, requestOptions: options }); } } //# sourceMappingURL=policies.js.map