ocket8888 commented on code in PR #7480:
URL: https://github.com/apache/trafficcontrol/pull/7480#discussion_r1194198324
##########
experimental/traffic-portal/src/app/api/profile.service.ts:
##########
@@ -71,6 +71,33 @@ export class ProfileService extends APIService {
return this.get<Array<ResponseProfile>>(path).toPromise();
}
+ /**
+ * Retrieves Profiles associated with a Parameter from the API.
+ *
+ * @param p Either a {@link ResponseParameter} or an integral, unique
identifier of a Parameter, for which the
+ * Profiles are to be retrieved.
+ * @returns The requested Profile(s).
+ */
+ public async getProfilesByParam(p: number| ResponseParameter):
Promise<Array<ResponseProfile>> {
+ let id: number;
+ if (typeof p === "number") {
+ id = p;
+ } else {
+ if (!p.id || p.id < 0) {
+ throw new Error("Parameter id must be
defined!");
+ }
+ id = p.id;
+ }
+
+ const path = "profiles";
+ if (id !== undefined) {
+ const params = {param: id};
+ const r = await this.get<Array<ResponseProfile>>(path,
undefined, params).toPromise();
+ return r;
+ }
+ return this.get<Array<ResponseProfile>>(path).toPromise();
Review Comment:
The type of `id` down here isn't `undefined` - it's `never`. What that means
is that there is no code path that reaches here, so your check doesn't do
anything because `id` can never be `undefined` at that point.
##########
experimental/traffic-portal/src/app/api/profile.service.ts:
##########
@@ -71,6 +71,33 @@ export class ProfileService extends APIService {
return this.get<Array<ResponseProfile>>(path).toPromise();
}
+ /**
+ * Retrieves Profiles associated with a Parameter from the API.
+ *
+ * @param p Either a {@link ResponseParameter} or an integral, unique
identifier of a Parameter, for which the
+ * Profiles are to be retrieved.
+ * @returns The requested Profile(s).
+ */
+ public async getProfilesByParam(p: number| ResponseParameter):
Promise<Array<ResponseProfile>> {
+ let id: number;
+ if (typeof p === "number") {
+ id = p;
+ } else {
+ if (!p.id || p.id < 0) {
Review Comment:
`!p.id || p.id <0` is equivalent to `p.id <= 0` because the only `true`
value for `!p.id` is `0`, since its type won't allow any other "false-y" value.
But also, I don't think it's your responsibility to make sure the caller is
passing a valid ID; if that happens the request will fail and they'll get an
appropriate error message.
##########
experimental/traffic-portal/src/app/api/profile.service.ts:
##########
@@ -103,4 +130,64 @@ export class ProfileService extends APIService {
return
this.delete<ResponseProfile>(`profiles/${id}`).toPromise();
}
+ public async getParameters(idOrName: number | string):
Promise<ResponseParameter>;
+ public async getParameters(): Promise<Array<ResponseParameter>>;
+ /**
+ * Retrieves Parameters from the API.
+ *
+ * @param idOrName Specify either the integral, unique identifier
(number) of a specific Parameter to retrieve,
+ * or its name (string).
+ * @returns The requested Parameter(s).
+ */
+ public async getParameters(idOrName?: number | string):
Promise<Array<ResponseParameter> | ResponseParameter> {
+ const path = "parameters";
+ if (idOrName !== undefined) {
+ let params;
+ switch (typeof idOrName) {
+ case "number":
+ params = {id: idOrName};
+ break;
+ case "string":
+ params = {name: idOrName};
+ }
+ const r = await this.get<[ResponseParameter]>(path,
undefined, params).toPromise();
+ if (r.length !== 1) {
+ throw new Error(`Traffic Ops responded with
${r.length} Types by identifier ${idOrName}`);
+ }
+ return r[0];
+ }
+ return this.get<Array<ResponseParameter>>(path).toPromise();
+ }
+
+ /**
+ * Deletes an existing parameter.
+ *
+ * @param typeOrId Id of the parameter to delete.
+ * @returns The deleted parameter.
+ */
+ public async deleteParameter(typeOrId: number | ResponseParameter):
Promise<void> {
+ const id = typeof(typeOrId) === "number" ? typeOrId :
typeOrId.id;
+ return this.delete(`parameters/${id}`).toPromise();
+ }
+
+ /**
+ * Creates a new parameter.
+ *
+ * @param parameter The parameter to create.
+ * @returns The created parameter.
+ */
+ public async createParameter(parameter: RequestParameter):
Promise<ResponseParameter> {
+ return this.post<ResponseParameter>("parameters",
parameter).toPromise();
+ }
+
+ /**
+ * Replaces the current definition of a parameter with the one given.
+ *
+ * @param parameter The new parameter.
+ * @returns The updated parameter.
+ */
+ public async updateParameter(parameter: ResponseParameter):
Promise<ResponseParameter> {
+ const path = `parameters/${parameter.id}`;
+ return this.put<ResponseParameter>(path, parameter).toPromise();
+ }
Review Comment:
all of the methods added here need tests
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]