Skip to content

Commit

Permalink
Refactor client API calls to use GalaxyApi factory
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Aug 2, 2024
1 parent 57ec7d4 commit 01df758
Show file tree
Hide file tree
Showing 79 changed files with 259 additions and 253 deletions.
8 changes: 5 additions & 3 deletions client/src/api/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ function getBaseUrl() {
return isTest ? window.location.origin : getAppRoot(undefined, true);
}

function createApiClient() {
function apiClientFactory() {
return createClient<GalaxyApiPaths>({ baseUrl: getBaseUrl() });
}

export type GalaxyApiClient = ReturnType<typeof createApiClient>;
export type GalaxyApiClient = ReturnType<typeof apiClientFactory>;

let client: GalaxyApiClient;

/**
* Returns the Galaxy API client.
*
* It can be used to make requests to the Galaxy API using the OpenAPI schema.
*
* See: https://openapi-ts.dev/openapi-fetch/
*/
export function GalaxyApi(): GalaxyApiClient {
if (!client) {
client = createApiClient();
client = apiClientFactory();
}

return client;
Expand Down
16 changes: 5 additions & 11 deletions client/src/api/client/serverMock.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type HistoryDetailed, type HistorySummary, type MessageException } from "@/api";
import { GalaxyApi, type GalaxyApiClient } from "@/api/client";
import { GalaxyApi } from "@/api";
import { useServerMock } from "@/api/client/__mocks__";

const TEST_HISTORY_SUMMARY: HistorySummary = {
Expand Down Expand Up @@ -51,15 +51,9 @@ server.use(
);

describe("useServerMock", () => {
let client: GalaxyApiClient;

beforeAll(() => {
client = GalaxyApi();
});

it("mocks the Galaxy Server", async () => {
{
const { data, error } = await client.GET("/api/histories/{history_id}", {
const { data, error } = await GalaxyApi().GET("/api/histories/{history_id}", {
params: {
path: { history_id: "test" },
query: { view: "summary" },
Expand All @@ -73,7 +67,7 @@ describe("useServerMock", () => {
}

{
const { data, error } = await client.GET("/api/histories/{history_id}", {
const { data, error } = await GalaxyApi().GET("/api/histories/{history_id}", {
params: {
path: { history_id: "test" },
query: { view: "detailed" },
Expand All @@ -87,7 +81,7 @@ describe("useServerMock", () => {
}

{
const { data, error } = await client.GET("/api/histories/{history_id}", {
const { data, error } = await GalaxyApi().GET("/api/histories/{history_id}", {
params: {
path: { history_id: "must-fail" },
},
Expand All @@ -100,7 +94,7 @@ describe("useServerMock", () => {
}

{
const { data, error } = await client.GET("/api/configuration");
const { data, error } = await GalaxyApi().GET("/api/configuration");

expect(data).toBeUndefined();

Expand Down
8 changes: 4 additions & 4 deletions client/src/api/datasetCollections.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { client, type CollectionEntry, type DCESummary, type HDCADetailed, type HDCASummary, isHDCA } from "@/api";
import { type CollectionEntry, type DCESummary, GalaxyApi, type HDCADetailed, type HDCASummary, isHDCA } from "@/api";
import { rethrowSimple } from "@/utils/simple-error";

const DEFAULT_LIMIT = 50;
Expand All @@ -8,7 +8,7 @@ const DEFAULT_LIMIT = 50;
* @param params.id The ID of the collection (HDCA) to fetch.
*/
export async function fetchCollectionDetails(params: { id: string }): Promise<HDCADetailed> {
const { data, error } = await client.GET("/api/dataset_collections/{id}", {
const { data, error } = await GalaxyApi().GET("/api/dataset_collections/{id}", {
params: { path: params },
});

Expand All @@ -23,7 +23,7 @@ export async function fetchCollectionDetails(params: { id: string }): Promise<HD
* @param params.id The ID of the collection (HDCA) to fetch.
*/
export async function fetchCollectionSummary(params: { id: string }): Promise<HDCASummary> {
const { data, error } = await client.GET("/api/dataset_collections/{id}", {
const { data, error } = await GalaxyApi().GET("/api/dataset_collections/{id}", {
params: {
path: params,
query: { view: "collection" },
Expand All @@ -46,7 +46,7 @@ export async function fetchCollectionElements(params: {
/** The maximum number of elements to fetch. */
limit?: number;
}): Promise<DCESummary[]> {
const { data, error } = await client.GET("/api/dataset_collections/{hdca_id}/contents/{parent_id}", {
const { data, error } = await GalaxyApi().GET("/api/dataset_collections/{hdca_id}/contents/{parent_id}", {
params: {
path: { hdca_id: params.hdcaId, parent_id: params.collectionId },
query: { instance_type: "history", offset: params.offset, limit: params.limit },
Expand Down
10 changes: 5 additions & 5 deletions client/src/api/datasets.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios from "axios";

import { client, type components, type GalaxyApiPaths, type HDADetailed } from "@/api";
import { type components, GalaxyApi, type GalaxyApiPaths, type HDADetailed } from "@/api";
import { withPrefix } from "@/utils/redirect";
import { rethrowSimple } from "@/utils/simple-error";

export async function fetchDatasetDetails(params: { id: string }): Promise<HDADetailed> {
const { data, error } = await client.GET("/api/datasets/{dataset_id}", {
const { data, error } = await GalaxyApi().GET("/api/datasets/{dataset_id}", {
params: {
path: {
dataset_id: params.id,
Expand All @@ -21,7 +21,7 @@ export async function fetchDatasetDetails(params: { id: string }): Promise<HDADe
}

export async function undeleteDataset(datasetId: string) {
const { data, error } = await client.PUT("/api/datasets/{dataset_id}", {
const { data, error } = await GalaxyApi().PUT("/api/datasets/{dataset_id}", {
params: {
path: { dataset_id: datasetId },
},
Expand All @@ -36,7 +36,7 @@ export async function undeleteDataset(datasetId: string) {
}

export async function purgeDataset(datasetId: string) {
const { data, error } = await client.DELETE("/api/datasets/{dataset_id}", {
const { data, error } = await GalaxyApi().DELETE("/api/datasets/{dataset_id}", {
params: {
path: { dataset_id: datasetId },
query: { purge: true },
Expand All @@ -57,7 +57,7 @@ export async function copyDataset(
type: CopyDatasetParamsType["path"]["type"] = "dataset",
source: CopyDatasetBodyType["source"] = "hda"
) {
const { data, error } = await client.POST("/api/histories/{history_id}/contents/{type}s", {
const { data, error } = await GalaxyApi().POST("/api/histories/{history_id}/contents/{type}s", {
params: {
path: { history_id: historyId, type },
},
Expand Down
4 changes: 2 additions & 2 deletions client/src/api/dbKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
* but now it is used to get the list of more generic "dbkeys".
*/

import { client } from "@/api";
import { GalaxyApi } from "@/api";
import { rethrowSimple } from "@/utils/simple-error";

export async function getDbKeys() {
const { data, error } = await client.GET("/api/genomes");
const { data, error } = await GalaxyApi().GET("/api/genomes");
if (error) {
rethrowSimple(error);
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/api/histories.archived.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { client, type components, type GalaxyApiPaths } from "@/api";
import { type components, GalaxyApi, type GalaxyApiPaths } from "@/api";
import { rethrowSimple } from "@/utils/simple-error";

export type ArchivedHistorySummary = components["schemas"]["ArchivedHistorySummary"];
Expand Down Expand Up @@ -38,7 +38,7 @@ const DEFAULT_PAGE_SIZE = 10;
export async function fetchArchivedHistories(options: GetArchivedHistoriesOptions): Promise<ArchivedHistoriesResult> {
const params = optionsToApiParams(options);

const { response, data, error } = await client.GET("/api/histories/archived", {
const { response, data, error } = await GalaxyApi().GET("/api/histories/archived", {
params: {
query: params,
},
Expand Down
8 changes: 4 additions & 4 deletions client/src/api/histories.export.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { client, type ModelStoreFormat, type ObjectExportTaskResponse } from "@/api";
import { GalaxyApi, type ModelStoreFormat, type ObjectExportTaskResponse } from "@/api";
import { type ExportRecord, ExportRecordModel } from "@/components/Common/models/exportRecordModel";
import { DEFAULT_EXPORT_PARAMS } from "@/composables/shortTermStorage";
import { rethrowSimple } from "@/utils/simple-error";
Expand All @@ -18,7 +18,7 @@ export const AVAILABLE_EXPORT_FORMATS: { id: ModelStoreFormat; name: string }[]
* @returns a promise with a list of export records associated with the given history.
*/
export async function fetchHistoryExportRecords(historyId: string) {
const { data, error } = await client.GET("/api/histories/{history_id}/exports", {
const { data, error } = await GalaxyApi().GET("/api/histories/{history_id}/exports", {
params: {
path: { history_id: historyId },
headers: {
Expand Down Expand Up @@ -50,7 +50,7 @@ export async function exportHistoryToFileSource(
) {
const exportDirectoryUri = `${exportDirectory}/${fileName}.${exportParams.modelStoreFormat}`;

const { data, error } = await client.POST("/api/histories/{history_id}/write_store", {
const { data, error } = await GalaxyApi().POST("/api/histories/{history_id}/write_store", {
params: {
path: { history_id: historyId },
},
Expand All @@ -76,7 +76,7 @@ export async function exportHistoryToFileSource(
* @returns A promise with the async task response that can be used to track the import progress.
*/
export async function reimportHistoryFromRecord(record: ExportRecord) {
const { data, error } = await client.POST("/api/histories/from_store_async", {
const { data, error } = await GalaxyApi().POST("/api/histories/from_store_async", {
body: {
store_content_uri: record.importUri,
model_store_format: record.modelStoreFormat,
Expand Down
8 changes: 4 additions & 4 deletions client/src/api/notifications.broadcast.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { client, type GalaxyApiPaths } from "@/api";
import { GalaxyApi, type GalaxyApiPaths } from "@/api";
import { rethrowSimple } from "@/utils/simple-error";

// TODO: Move these functions to broadcastStore and refactor other calls to go through the store

export async function fetchAllBroadcasts() {
const { data, error } = await client.GET("/api/notifications/broadcast");
const { data, error } = await GalaxyApi().GET("/api/notifications/broadcast");
if (error) {
rethrowSimple(error);
}
Expand All @@ -14,7 +14,7 @@ export async function fetchAllBroadcasts() {
type CreateBroadcastNotificationRequestBody =
GalaxyApiPaths["/api/notifications/broadcast"]["post"]["requestBody"]["content"]["application/json"];
export async function createBroadcast(broadcast: CreateBroadcastNotificationRequestBody) {
const { data, error } = await client.POST("/api/notifications/broadcast", {
const { data, error } = await GalaxyApi().POST("/api/notifications/broadcast", {
body: broadcast,
});

Expand All @@ -28,7 +28,7 @@ export async function createBroadcast(broadcast: CreateBroadcastNotificationRequ
type UpdateBroadcastNotificationRequestBody =
GalaxyApiPaths["/api/notifications/broadcast/{notification_id}"]["put"]["requestBody"]["content"]["application/json"];
export async function updateBroadcast(id: string, broadcast: UpdateBroadcastNotificationRequestBody) {
const { data, error } = await client.PUT("/api/notifications/broadcast/{notification_id}", {
const { data, error } = await GalaxyApi().PUT("/api/notifications/broadcast/{notification_id}", {
params: {
path: { notification_id: id },
},
Expand Down
10 changes: 5 additions & 5 deletions client/src/api/objectStores.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { client, type components } from "@/api";
import { type components, GalaxyApi } from "@/api";
import { rethrowSimple } from "@/utils/simple-error";

export type UserConcreteObjectStore = components["schemas"]["UserConcreteObjectStoreModel"];

export type ObjectStoreTemplateType = "aws_s3" | "azure_blob" | "boto3" | "disk" | "generic_s3";

export async function getSelectableObjectStores() {
const { data, error } = await client.GET("/api/object_stores", {
const { data, error } = await GalaxyApi().GET("/api/object_stores", {
params: {
query: { selectable: true },
},
Expand All @@ -23,7 +23,7 @@ export async function getObjectStoreDetails(id: string) {
if (id.startsWith("user_objects://")) {
const userObjectStoreId = id.substring("user_objects://".length);

const { data, error } = await client.GET("/api/object_store_instances/{user_object_store_id}", {
const { data, error } = await GalaxyApi().GET("/api/object_store_instances/{user_object_store_id}", {
params: { path: { user_object_store_id: userObjectStoreId } },
});

Expand All @@ -33,7 +33,7 @@ export async function getObjectStoreDetails(id: string) {

return data;
} else {
const { data, error } = await client.GET("/api/object_stores/{object_store_id}", {
const { data, error } = await GalaxyApi().GET("/api/object_stores/{object_store_id}", {
params: { path: { object_store_id: id } },
});

Expand All @@ -46,7 +46,7 @@ export async function getObjectStoreDetails(id: string) {
}

export async function updateObjectStore(datasetId: string, objectStoreId: string) {
const { error } = await client.PUT("/api/datasets/{dataset_id}/object_store_id", {
const { error } = await GalaxyApi().PUT("/api/datasets/{dataset_id}/object_store_id", {
params: { path: { dataset_id: datasetId } },
body: { object_store_id: objectStoreId },
});
Expand Down
6 changes: 3 additions & 3 deletions client/src/api/remoteFiles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { client, type components } from "@/api";
import { type components, GalaxyApi } from "@/api";
import { rethrowSimple } from "@/utils/simple-error";

/** The browsing mode:
Expand Down Expand Up @@ -34,7 +34,7 @@ export interface FilterFileSourcesOptions {
* @returns The list of available (browsable) file sources from the server.
*/
export async function fetchFileSources(options: FilterFileSourcesOptions = {}): Promise<BrowsableFilesSourcePlugin[]> {
const { data, error } = await client.GET("/api/remote_files/plugins", {
const { data, error } = await GalaxyApi().GET("/api/remote_files/plugins", {
params: {
query: {
browsable_only: true,
Expand Down Expand Up @@ -77,7 +77,7 @@ export async function browseRemoteFiles(
query?: string,
sortBy?: string
): Promise<BrowseRemoteFilesResult> {
const { response, data, error } = await client.GET("/api/remote_files", {
const { response, data, error } = await GalaxyApi().GET("/api/remote_files", {
params: {
query: {
format: "uri",
Expand Down
4 changes: 2 additions & 2 deletions client/src/api/tags.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { client, type components } from "@/api";
import { type components, GalaxyApi } from "@/api";
import { rethrowSimple } from "@/utils/simple-error";

type TaggableItemClass = components["schemas"]["TaggableItemClass"];

export async function updateTags(itemId: string, itemClass: TaggableItemClass, itemTags?: string[]): Promise<void> {
const { error } = await client.PUT("/api/tags", {
const { error } = await GalaxyApi().PUT("/api/tags", {
body: {
item_id: itemId,
item_class: itemClass,
Expand Down
6 changes: 3 additions & 3 deletions client/src/api/users.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { client } from "@/api";
import { GalaxyApi } from "@/api";
import { toQuotaUsage } from "@/components/User/DiskUsage/Quota/model";
import { rethrowSimple } from "@/utils/simple-error";

export { type QuotaUsage } from "@/components/User/DiskUsage/Quota/model";

export async function fetchCurrentUserQuotaUsages() {
const { data, error } = await client.GET("/api/users/{user_id}/usage", {
const { data, error } = await GalaxyApi().GET("/api/users/{user_id}/usage", {
params: { path: { user_id: "current" } },
});

Expand All @@ -21,7 +21,7 @@ export async function fetchCurrentUserQuotaSourceUsage(quotaSourceLabel?: string
quotaSourceLabel = "__null__";
}

const { data, error } = await client.GET("/api/users/{user_id}/usage/{label}", {
const { data, error } = await GalaxyApi().GET("/api/users/{user_id}/usage/{label}", {
params: { path: { user_id: "current", label: quotaSourceLabel } },
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BAlert, BButton, BSpinner, BTab, BTabs } from "bootstrap-vue";
import { storeToRefs } from "pinia";
import { computed, ref, watch } from "vue";
import { client } from "@/api";
import { GalaxyApi } from "@/api";
import { updateContentFields } from "@/components/History/model/queries";
import { DatatypesProvider, DbKeyProvider, SuitableConvertersProvider } from "@/components/providers";
import { useConfig } from "@/composables/config";
Expand Down Expand Up @@ -96,7 +96,7 @@ async function clickedSave(attribute: string, newValue: any) {
const dbKey = newValue.id as string;
const { error } = await client.POST("/api/dataset_collections/{id}/copy", {
const { error } = await GalaxyApi().POST("/api/dataset_collections/{id}/copy", {
params: { path: { id: props.collectionId } },
body: { dbkey: dbKey },
});
Expand Down Expand Up @@ -130,7 +130,7 @@ async function clickedDatatypeChange(selectedDatatype: any) {
return;
}
const { error } = await client.PUT("/api/histories/{history_id}/contents/bulk", {
const { error } = await GalaxyApi().PUT("/api/histories/{history_id}/contents/bulk", {
params: { path: { history_id: currentHistoryId.value } },
body: {
items: [
Expand Down
4 changes: 2 additions & 2 deletions client/src/components/Common/ExportRDMForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { BButton, BCard, BFormGroup, BFormInput, BFormRadio, BFormRadioGroup } from "bootstrap-vue";
import { computed, ref } from "vue";
import { client } from "@/api";
import { GalaxyApi } from "@/api";
import { type BrowsableFilesSourcePlugin, type CreatedEntry, type FilterFileSourcesOptions } from "@/api/remoteFiles";
import { useToast } from "@/composables/toast";
import localize from "@/utils/localization";
Expand Down Expand Up @@ -74,7 +74,7 @@ function doExport() {
}
async function doCreateRecord() {
const { data, error } = await client.POST("/api/remote_files", {
const { data, error } = await GalaxyApi().POST("/api/remote_files", {
body: {
target: sourceUri.value,
name: recordName.value,
Expand Down
Loading

0 comments on commit 01df758

Please sign in to comment.