Skip to content

Commit

Permalink
feat: Add generic UseFmeService activity
Browse files Browse the repository at this point in the history
  • Loading branch information
rcooney committed Dec 16, 2020
1 parent 9657b90 commit 73f6058
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 17 deletions.
19 changes: 2 additions & 17 deletions src/activities/RunFmeDataDownload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { IActivityHandler } from "@geocortex/workflow/runtime/IActivityHandler";
import { FmeService } from "../FmeService";
import { objectToQueryString } from "../utils";

/** An interface that defines the inputs of the activity. */
export interface RunFmeDataDownloadInputs {
Expand Down Expand Up @@ -71,7 +72,7 @@ export class RunFmeDataDownload implements IActivityHandler {
service.server.runDataDownload(
repository,
workspace,
this.objectToQueryString(parameters),
objectToQueryString(parameters),
(result) => {
return resolve({
result,
Expand All @@ -80,20 +81,4 @@ export class RunFmeDataDownload implements IActivityHandler {
);
});
}

objectToQueryString(data?: {}): string {
if (!data) {
return "";
}
return Object.keys(data)
.map((k) => {
const value = data[k];
const valueToEncode =
value === undefined || value === null ? "" : value;
return `${encodeURIComponent(k)}=${encodeURIComponent(
valueToEncode
)}`;
})
.join("&");
}
}
94 changes: 94 additions & 0 deletions src/activities/UseFmeService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import type { IActivityHandler } from "@geocortex/workflow/runtime/IActivityHandler";
import { FmeService } from "../FmeService";
import { objectToQueryString } from "../utils";

/** An interface that defines the inputs of the activity. */
export interface UseFmeServiceInputs {
/**
* @displayName FME Service
* @description The FME service.
* @required
*/
service: FmeService;

/**
* @description The FME service operation.
* @required
*/
path:
| "healthcheck"
| "info"
| "notifications/publications"
| "notifications/publishers"
| "notifications/subscribers"
| "notifications/subscriptions"
| "notifications/topics"
| "repositories"
| "resources/connections"
| "schedules"
| "schedules/categories"
| string;
/**
* @description The HTTP method to use to make the request. The default is GET.
*/
method?: "GET" | "POST" | "PUSH" | "DELETE";
/**
* @description The content type of the request. This is required when the method is POST or PUT.
*/
contentType?: "application/x-www-form-urlencoded" | "application/json";
/**
* @description The body parameters to pass to the service operation when the method is POST or PUT.
*/
parameters?: string | object;
}

/** An interface that defines the outputs of the activity. */
export interface UseFmeServiceOutputs {
/**
* @description The result of the service operation.
*/
result: any;
}

/**
* @displayName Use FME Service
* @category FME
* @description Utility activity to generically access any FME Server REST API operation.
* @helpUrl https://docs.safe.com/fme/html/FME_REST/apidoc/v3/#!
*/
export class UseFmeService implements IActivityHandler {
async execute(inputs: UseFmeServiceInputs): Promise<UseFmeServiceOutputs> {
const { contentType, method, parameters, path, service } = inputs;
if (!service) {
throw new Error("service is required");
}
if (!path) {
throw new Error("path is required");
}

let body: string | undefined;
if (typeof parameters === "object") {
if (contentType === "application/json") {
body = JSON.stringify(parameters);
} else if (contentType === "application/x-www-form-urlencoded") {
body = objectToQueryString(parameters);
}
} else {
body = parameters;
}

return new Promise((resolve) => {
service.server.customRequest(
`${service.url}/fmerest/v3/${path}`,
method || "GET",
(result) => {
return resolve({
result,
});
},
body!,
contentType!
);
});
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from "./activities/GetFmeWorkspaceParameters";
export * from "./activities/RunFmeDataDownload";
export * from "./activities/RunFmeJob";
export * from "./activities/RunFmeJobAsync";
export * from "./activities/UseFmeService";
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function objectToQueryString(data?: {}): string {
if (!data) {
return "";
}
return Object.keys(data)
.map((k) => {
const value = data[k];
const valueToEncode =
value === undefined || value === null ? "" : value;
return `${encodeURIComponent(k)}=${encodeURIComponent(
valueToEncode
)}`;
})
.join("&");
}

0 comments on commit 73f6058

Please sign in to comment.