Skip to content

Commit

Permalink
feat(getItemInfo): add a function to fetch an info file for an item
Browse files Browse the repository at this point in the history
AFFECTS PACKAGES:
@esri/arcgis-rest-portal

ISSUES CLOSED: #738
  • Loading branch information
tomwayson committed Sep 10, 2020
1 parent d6ec9fc commit a9dd7d6
Show file tree
Hide file tree
Showing 4 changed files with 404 additions and 4 deletions.
42 changes: 41 additions & 1 deletion packages/arcgis-rest-portal/src/items/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
IItemDataOptions,
IItemRelationshipOptions,
IUserItemOptions,
determineOwner
determineOwner,
IGetItemInfoOptions
} from "./helpers";

/**
Expand Down Expand Up @@ -306,3 +307,42 @@ export function getItemParts(
return request(url, requestOptions);
});
}

/**
* ```
* import { getItemInfo } from "@esri/arcgis-rest-portal";
* // get the "Info Card" for the item
* getItemInfo("ae7")
* .then(itemInfoXml) // XML document as a string
* // or get the contents of a specific file
* getItemInfo("ae7", { fileName: "metadata/metadata.xml", authentication })
* .then(itemMetadataXml) // XML document as a string
* ```
* Get an info file for an item. See the [REST Documentation](https://developers.arcgis.com/rest/users-groups-and-items/item-info-file.htm) for more information. Currently only supports text files.
* @param id - Item Id
* @param requestOptions - Options for the request, optionally including the file name which defaults to `iteminfo.xml`
* @returns A Promise that will resolve with the contents of the info file for the item.
*/
export function getItemInfo(
id: string,
requestOptions?: IGetItemInfoOptions
): Promise<any> {
const { fileName = "iteminfo.xml" } = requestOptions || {};
const url = `${getItemBaseUrl(
id,
requestOptions as IRequestOptions
)}/info/${fileName}`;
// default to a GET request and force rawResponse
const options: IItemDataOptions = {
...{ httpMethod: "GET", params: {} },
...requestOptions,
rawResponse: true
};
// otherwise f=json will be appended by default
options.params.f = null;

return request(url, options).then(response => {
// assume this is XML, so parse as text (not JSON)
return response.text();
});
}
12 changes: 11 additions & 1 deletion packages/arcgis-rest-portal/src/items/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ export interface IItemInfoOptions extends IUserItemOptions {
file: any;
}

export interface IGetItemInfoOptions extends IUserItemOptions {
/**
* Name of the info file, optionally including the folder path
*/
fileName?: string;
}

export interface IItemResourceOptions extends IUserItemOptions {
/**
* New resource filename.
Expand Down Expand Up @@ -249,7 +256,10 @@ export function determineOwner(requestOptions: any): Promise<string> {
return Promise.resolve(requestOptions.owner);
} else if (requestOptions.item && requestOptions.item.owner) {
return Promise.resolve(requestOptions.item.owner);
} else if (requestOptions.authentication && requestOptions.authentication.getUsername) {
} else if (
requestOptions.authentication &&
requestOptions.authentication.getUsername
) {
return requestOptions.authentication.getUsername();
} else {
return Promise.reject(
Expand Down
48 changes: 46 additions & 2 deletions packages/arcgis-rest-portal/test/items/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,24 @@ import {
getItemGroups,
getItemStatus,
getItemParts,
getRelatedItems
getRelatedItems,
getItemInfo
} from "../../src/items/get";

import {
ItemResponse,
ItemDataResponse,
ItemGroupResponse,
RelatedItemsResponse
RelatedItemsResponse,
ItemInfoResponse,
ItemMetadataResponse
} from "../mocks/items/item";

import { GetItemResourcesResponse } from "../mocks/items/resources";

import { UserSession } from "@esri/arcgis-rest-auth";
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils";
import { IGetItemInfoOptions } from "../../src/items/helpers";

describe("get base url", () => {
it("should return base url when passed a portal url", () => {
Expand Down Expand Up @@ -167,6 +171,46 @@ describe("get", () => {
});
});

it("should return item info by id", done => {
fetchMock.once("*", ItemInfoResponse);

getItemInfo("3ef")
.then(response => {
expect(response).toBe(ItemInfoResponse);
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://www.arcgis.com/sharing/rest/content/items/3ef/info/iteminfo.xml"
);
expect(options.method).toBe("GET");
done();
})
.catch(e => {
fail(e);
});
});

it("should return item metadata", done => {
fetchMock.once("*", ItemMetadataResponse);
const fileName = "metadata/metadata.xml";
getItemInfo("3ef", {
fileName
} as IGetItemInfoOptions)
.then(response => {
expect(response).toBe(ItemMetadataResponse);
expect(fetchMock.called()).toEqual(true);
const [url, options]: [string, RequestInit] = fetchMock.lastCall("*");
expect(url).toEqual(
"https://www.arcgis.com/sharing/rest/content/items/3ef/info/metadata/metadata.xml"
);
expect(options.method).toBe("GET");
done();
})
.catch(e => {
fail(e);
});
});

describe("Authenticated methods", () => {
// setup a UserSession to use in all these tests
const MOCK_USER_SESSION = new UserSession({
Expand Down
Loading

0 comments on commit a9dd7d6

Please sign in to comment.