Skip to content

Commit

Permalink
remove target from hubapi url (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
sansth1010 authored Feb 10, 2025
1 parent cc35f8b commit fd6e96b
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 61 deletions.
38 changes: 0 additions & 38 deletions src/helpers/get-ogc-items-stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,43 +126,5 @@ describe('getOgcItemsStream function', () => {
expect(axios.get).toBeCalledTimes(1);
expect(axios.get).toHaveBeenNthCalledWith(1, 'https://my-site.hub.arcgis.com/api/search/v1/collections/all/items?limit=0&startindex=1');
});

it('should target the given Hub API URL if specified', async () => {
// Setup
const siteUrl = 'https://my-site.hub.arcgis.com'
const ogcSearchRequestOpts = {
queryParams: {
limit: 150
},
collectionKey: 'all',
hubApiUrl: 'https://hubogctest.arcgis.com'
};
(axios.get as jest.Mock).mockResolvedValue({
data: {
numberMatched: 324,
}
});

// Test
const streams: PagingStream[] = await getOgcItemsStream(siteUrl, ogcSearchRequestOpts, hubsite);
expect(streams).toHaveLength(2);
expect(getPagingStreamSpy).toHaveBeenCalledTimes(2);
expect(getPagingStreamSpy).toHaveBeenNthCalledWith(
1,
'https://my-site.hub.arcgis.com/api/search/v1/collections/all/items?limit=100&startindex=1&target=hubogctest.arcgis.com',
hubsite,
1
);

expect(getPagingStreamSpy).toHaveBeenNthCalledWith(
2,
'https://my-site.hub.arcgis.com/api/search/v1/collections/all/items?limit=50&startindex=101&target=hubogctest.arcgis.com',
hubsite,
1
);

expect(axios.get).toBeCalledTimes(1);
expect(axios.get).toHaveBeenNthCalledWith(1, 'https://my-site.hub.arcgis.com/api/search/v1/collections/all/items?limit=0&startindex=1&target=hubogctest.arcgis.com');
});
});

23 changes: 0 additions & 23 deletions src/helpers/get-ogc-items-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ const buildSearchRequestUrl = (siteUrl: string, ogcSearchRequestOpts: SearchRequ
const searchRequest = _.cloneDeep(ogcSearchRequestOpts.queryParams);
searchRequest.limit = Math.min(searchRequest.limit, MAX_LIMIT);
searchRequest.startindex = searchRequest.startindex || 1;

if (ogcSearchRequestOpts.hubApiUrl) {
searchRequest.target = getDomainFromUrl(ogcSearchRequestOpts.hubApiUrl);
}

const searchParams = new URLSearchParams(searchRequest).toString();
return `${siteUrl}/api/search/v1/collections/${ogcSearchRequestOpts.collectionKey}/items?${searchParams}`;
};
Expand All @@ -50,11 +45,6 @@ const getTotalCount = async (siteUrl: string, ogcSearchRequestOpts: SearchReques
const searchRequest = _.cloneDeep(ogcSearchRequestOpts.queryParams);
searchRequest.limit = 0;
searchRequest.startindex = 1;

if (ogcSearchRequestOpts.hubApiUrl) {
searchRequest.target = getDomainFromUrl(ogcSearchRequestOpts.hubApiUrl);
}

const searchParams = new URLSearchParams(searchRequest).toString();
const fetchUrl = `${siteUrl}/api/search/v1/collections/${ogcSearchRequestOpts.collectionKey}/items?${searchParams}`;
const res = await axios.get(fetchUrl);
Expand All @@ -69,16 +59,3 @@ const getTotalCount = async (siteUrl: string, ogcSearchRequestOpts: SearchReques
const getPagesPerBatch = (limit: number, requestIndex: number, requests: any, pagesPerBatch: number) => {
return limit ? (requestIndex + 1 === requests.length ? 1 : pagesPerBatch) : pagesPerBatch;
};

function getDomainFromUrl(url: string): string {
try {
// Check if the URL is a domain-only string
if (!url.startsWith('http://') && !url.startsWith('https://')) {
url = `http://${url}`;
}
const parsedUrl = new URL(url);
return parsedUrl.hostname;
} catch (error) {
throw new Error(`Invalid Hub API URL: ${url}`);
}
}

0 comments on commit fd6e96b

Please sign in to comment.