-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: service name and service info fns
- Loading branch information
Showing
12 changed files
with
346 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
packages/arcgis-rest-portal/src/services/get-unique-service-name.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import { isServiceNameAvailable } from "./is-service-name-available"; | ||
|
||
/** | ||
* Given a starting name, return a service name that is unqieu within | ||
* the current users organization | ||
* | ||
* @export | ||
* @param {string} name | ||
* @param {UserSession} session | ||
* @param {number} step | ||
* @return {*} {Promise<string>} | ||
*/ | ||
export function getUniqueServiceName( | ||
name: string, | ||
type: string, | ||
session: UserSession, | ||
step: number | ||
): Promise<string> { | ||
let nameToCheck = name; | ||
if (step) { | ||
nameToCheck = `${name}_${step}`; | ||
} | ||
return isServiceNameAvailable(nameToCheck, type, session).then((response) => { | ||
if (response.available) { | ||
return nameToCheck; | ||
} else { | ||
step = step + 1; | ||
return getUniqueServiceName(name, type, session, step); | ||
} | ||
}); | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/arcgis-rest-portal/src/services/is-service-name-available.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import { request } from "@esri/arcgis-rest-request"; | ||
import { IServiceNameAvailable } from "@esri/arcgis-rest-types"; | ||
|
||
/** | ||
* Determine if a specific service name is available in the current user's organization | ||
* | ||
* @export | ||
* @param {string} name | ||
* @param {UserSession} session | ||
* @return {*} {Promise<IServiceNameAvailable>} | ||
*/ | ||
export function isServiceNameAvailable( | ||
name: string, | ||
type: string, | ||
session: UserSession | ||
): Promise<IServiceNameAvailable> { | ||
const url = `${session.portal}/portals/self/isServiceNameAvailable`; | ||
return request(url, { | ||
params: { | ||
name, | ||
type, | ||
}, | ||
httpMethod: "GET", | ||
authentication: session, | ||
}); | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/arcgis-rest-portal/test/services/get-unique-service-name.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import * as isServiceNameAvailableModule from "../../src/services/is-service-name-available"; | ||
|
||
import { getUniqueServiceName } from "../../src/services/get-unique-service-name"; | ||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils"; | ||
|
||
describe("get-unique-service-name:", () => { | ||
const MOCK_USER_SESSION = new UserSession({ | ||
clientId: "clientId", | ||
redirectUri: "https://example-app.com/redirect-uri", | ||
token: "fake-token", | ||
tokenExpires: TOMORROW, | ||
refreshToken: "refreshToken", | ||
refreshTokenExpires: TOMORROW, | ||
refreshTokenTTL: 1440, | ||
username: "casey", | ||
password: "123456", | ||
portal: "https://myorg.maps.arcgis.com/sharing/rest", | ||
}); | ||
it("does single check if unique", () => { | ||
const spy = spyOn( | ||
isServiceNameAvailableModule, | ||
"isServiceNameAvailable" | ||
).and.callFake(() => Promise.resolve({ available: true })); | ||
return getUniqueServiceName( | ||
"myService", | ||
"Feature Service", | ||
MOCK_USER_SESSION, | ||
0 | ||
).then((result) => { | ||
expect(result).toBe("myService", "should return name"); | ||
expect(spy.calls.count()).toBe(1, "should check one name"); | ||
}); | ||
}); | ||
|
||
it("makes multiple calls if already taken", () => { | ||
let callNum = 1; | ||
const spy = spyOn( | ||
isServiceNameAvailableModule, | ||
"isServiceNameAvailable" | ||
).and.callFake(() => { | ||
const result = callNum === 2; | ||
callNum++; | ||
return Promise.resolve({ available: result }); | ||
}); | ||
return getUniqueServiceName( | ||
"myService", | ||
"Feature Service", | ||
MOCK_USER_SESSION, | ||
0 | ||
).then((result) => { | ||
expect(result).toBe("myService_1", "should return name"); | ||
expect(spy.calls.count()).toBe(2, "should check two names"); | ||
}); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
packages/arcgis-rest-portal/test/services/is-service-name-available.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import * as requestModule from "@esri/arcgis-rest-request"; | ||
import { isServiceNameAvailable } from "../../src/services/is-service-name-available"; | ||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils"; | ||
|
||
describe("is-service-name-available", () => { | ||
const MOCK_USER_SESSION = new UserSession({ | ||
clientId: "clientId", | ||
redirectUri: "https://example-app.com/redirect-uri", | ||
token: "fake-token", | ||
tokenExpires: TOMORROW, | ||
refreshToken: "refreshToken", | ||
refreshTokenExpires: TOMORROW, | ||
refreshTokenTTL: 1440, | ||
username: "casey", | ||
password: "123456", | ||
portal: "https://myorg.maps.arcgis.com/sharing/rest", | ||
}); | ||
|
||
it("returns server response", () => { | ||
const spy = spyOn(requestModule, "request").and.callFake(() => | ||
Promise.resolve({ available: true }) | ||
); | ||
return isServiceNameAvailable( | ||
"someService", | ||
"Feature Service", | ||
MOCK_USER_SESSION | ||
).then((result) => { | ||
expect(result.available).toBe(true, "should return the api response"); | ||
expect(spy.calls.count()).toBe(1, "should make one request"); | ||
expect(spy.calls.argsFor(0)[0]).toBe( | ||
`${MOCK_USER_SESSION.portal}/portals/self/isServiceNameAvailable` | ||
); | ||
const opts = spy.calls.argsFor(0)[1]; | ||
|
||
expect(opts.params.name).toBe( | ||
"someService", | ||
"passes in the service name" | ||
); | ||
expect(opts.params.type).toBe("Feature Service", "passes in the type"); | ||
}); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
packages/arcgis-rest-service-admin/src/get-service-admin-info.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
// TODO: Move to service-admin package in rest-js | ||
|
||
import { request } from "@esri/arcgis-rest-request"; | ||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import { IServiceInfo } from "@esri/arcgis-rest-types"; | ||
|
||
/** | ||
* Given a Feature Service url, fetch the service admin information. | ||
* | ||
* The response from this call includes all the detailed information | ||
* for each layer/table in the service as well as some admin properties | ||
* | ||
* @export | ||
* @param {string} serviceUrl | ||
* @param {UserSession} session | ||
* @return {*} {Promise<IServiceInfo>} | ||
*/ | ||
export function getServiceAdminInfo( | ||
serviceUrl: string, | ||
session: UserSession | ||
): Promise<IServiceInfo> { | ||
const serviceAdminUrl = serviceUrl.replace( | ||
"/rest/services", | ||
"/rest/admin/services" | ||
); | ||
|
||
return request(serviceAdminUrl, { | ||
authentication: session, | ||
params: { | ||
f: "json", | ||
}, | ||
}); | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/arcgis-rest-service-admin/src/get-view-sources.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import { request } from "@esri/arcgis-rest-request"; | ||
import { IViewServiceSources } from "@esri/arcgis-rest-types"; | ||
|
||
/** | ||
* Return the sources response for a view service item | ||
* | ||
* @param {string} viewServiceUrl | ||
* @param {UserSession} session | ||
* @return {*} {Promise<Record<string, unknown>>} | ||
*/ | ||
export function getViewSources( | ||
viewServiceUrl: string, | ||
session: UserSession | ||
): Promise<IViewServiceSources> { | ||
return request(`${viewServiceUrl}/sources`, { authentication: session }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
export * from "./create"; | ||
export * from "./addTo"; | ||
export * from "./update"; | ||
export * from "./get-service-admin-info"; | ||
export * from "./get-view-sources"; | ||
|
||
export { | ||
IFeatureServiceDefinition, | ||
IExtent, | ||
ISpatialReference, | ||
ILayer, | ||
ILayerDefinition, | ||
ITable | ||
ITable, | ||
} from "@esri/arcgis-rest-types"; |
37 changes: 37 additions & 0 deletions
37
packages/arcgis-rest-service-admin/test/get-service-admin-info.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
|
||
import * as requestModule from "@esri/arcgis-rest-request"; | ||
import { getServiceAdminInfo } from "../src/get-service-admin-info"; | ||
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils"; | ||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
|
||
describe("get-service-admin-info: ", () => { | ||
const MOCK_USER_SESSION = new UserSession({ | ||
clientId: "clientId", | ||
redirectUri: "https://example-app.com/redirect-uri", | ||
token: "fake-token", | ||
tokenExpires: TOMORROW, | ||
refreshToken: "refreshToken", | ||
refreshTokenExpires: TOMORROW, | ||
refreshTokenTTL: 1440, | ||
username: "casey", | ||
password: "123456", | ||
portal: "https://myorg.maps.arcgis.com/sharing/rest", | ||
}); | ||
it("makes request to the admin url", () => { | ||
const spy = spyOn(requestModule, "request").and.callFake(() => | ||
Promise.resolve({ foo: "bar" }) | ||
); | ||
return getServiceAdminInfo( | ||
"https://servicesqa.arcgis.com/orgid/arcgis/rest/services/mysevice/FeatureServer", | ||
MOCK_USER_SESSION | ||
).then((result) => { | ||
expect(result.foo).toBe("bar", "should return the api response"); | ||
expect(spy.calls.count()).toBe(1, "should make one request"); | ||
expect(spy.calls.argsFor(0)[0]).toBe( | ||
"https://servicesqa.arcgis.com/orgid/arcgis/rest/admin/services/mysevice/FeatureServer" | ||
); | ||
}); | ||
}); | ||
}); |
40 changes: 40 additions & 0 deletions
40
packages/arcgis-rest-service-admin/test/get-view-sources.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* Copyright (c) 2018-2020 Environmental Systems Research Institute, Inc. | ||
* Apache-2.0 */ | ||
import * as requestModule from "@esri/arcgis-rest-request"; | ||
import { getViewSources } from "../src/get-view-sources"; | ||
|
||
import { UserSession } from "@esri/arcgis-rest-auth"; | ||
import { TOMORROW } from "@esri/arcgis-rest-auth/test/utils"; | ||
|
||
describe("get-view-sources: ", () => { | ||
const MOCK_USER_SESSION = new UserSession({ | ||
clientId: "clientId", | ||
redirectUri: "https://example-app.com/redirect-uri", | ||
token: "fake-token", | ||
tokenExpires: TOMORROW, | ||
refreshToken: "refreshToken", | ||
refreshTokenExpires: TOMORROW, | ||
refreshTokenTTL: 1440, | ||
username: "casey", | ||
password: "123456", | ||
portal: "https://myorg.maps.arcgis.com/sharing/rest", | ||
}); | ||
it("makes request to the admin url", () => { | ||
const spy = spyOn(requestModule, "request").and.callFake(() => | ||
Promise.resolve({ currentVersion: 1234 }) | ||
); | ||
return getViewSources( | ||
"https://servicesqa.arcgis.com/orgid/arcgis/rest/services/mysevice/FeatureServer", | ||
MOCK_USER_SESSION | ||
).then((result) => { | ||
expect(result.currentVersion).toBe( | ||
1234, | ||
"should return the api response" | ||
); | ||
expect(spy.calls.count()).toBe(1, "should make one request"); | ||
expect(spy.calls.argsFor(0)[0]).toBe( | ||
"https://servicesqa.arcgis.com/orgid/arcgis/rest/services/mysevice/FeatureServer/sources" | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": [ | ||
"src/**/*.ts" | ||
] | ||
"src/**/*.ts"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters