From b03c005984ac6125ddc3b03a2dbed83a5541b949 Mon Sep 17 00:00:00 2001 From: Mike Delez <60604010+mdelez@users.noreply.github.com> Date: Wed, 15 Feb 2023 11:23:05 +0100 Subject: [PATCH] feat(zio-http)!: additional zio http support (DEV-1705) (#497) * feat(zio-http): fix zioApiUrl getter * refactor(zio-http): refactor zio-http support * fix test * add more functionality to the test-framework app and add an e2e test * remove problematic test due to CORS.... --- src/api/admin/groups/groups-endpoint.spec.ts | 2 +- src/api/admin/lists/lists-endpoint.spec.ts | 2 +- .../permissions/permissions-endpoint.spec.ts | 2 +- .../admin/projects/projects-endpoint-admin.ts | 24 +-- .../admin/projects/projects-endpoint.spec.ts | 2 +- src/api/admin/users/users-endpoint.spec.ts | 2 +- src/api/endpoint.ts | 31 ++- src/api/system/health/health-endpoint.spec.ts | 2 +- src/api/v2/list/lists-endpoint.spec.ts | 2 +- .../v2/ontology/ontologies-endpoint.spec.ts | 2 +- .../v2/resource/resources-endpoint.spec.ts | 2 +- src/api/v2/search/search-endpoint.spec.ts | 2 +- src/api/v2/values/values-endpoint.spec.ts | 2 +- src/cache/ListNodeV2Cache.spec.ts | 2 +- .../ontology-cache/OntologyCache.spec.ts | 4 +- src/knora-api-config.spec.ts | 80 ++++++-- src/knora-api-config.ts | 21 +- .../resources/ResourcesConversionUtil.spec.ts | 2 +- test-framework/src/app/app.component.html | 42 +++- test-framework/src/app/app.component.ts | 184 ++++++++++++++++-- 20 files changed, 322 insertions(+), 90 deletions(-) diff --git a/src/api/admin/groups/groups-endpoint.spec.ts b/src/api/admin/groups/groups-endpoint.spec.ts index b0656f5cb..da3063857 100644 --- a/src/api/admin/groups/groups-endpoint.spec.ts +++ b/src/api/admin/groups/groups-endpoint.spec.ts @@ -11,7 +11,7 @@ import { ApiResponseData } from "../../../models/api-response-data"; describe("GroupsEndpoint", () => { - const config = new KnoraApiConfig("http", "localhost", 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig("http", "localhost", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); beforeEach(() => { diff --git a/src/api/admin/lists/lists-endpoint.spec.ts b/src/api/admin/lists/lists-endpoint.spec.ts index 2f69f26d0..9d07e8df9 100644 --- a/src/api/admin/lists/lists-endpoint.spec.ts +++ b/src/api/admin/lists/lists-endpoint.spec.ts @@ -24,7 +24,7 @@ import { ApiResponseData } from "../../../models/api-response-data"; describe("ListsEndpoint", () => { - const config = new KnoraApiConfig("http", "localhost", 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig("http", "localhost", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); beforeEach(() => { diff --git a/src/api/admin/permissions/permissions-endpoint.spec.ts b/src/api/admin/permissions/permissions-endpoint.spec.ts index f5abfde45..072adb34f 100644 --- a/src/api/admin/permissions/permissions-endpoint.spec.ts +++ b/src/api/admin/permissions/permissions-endpoint.spec.ts @@ -22,7 +22,7 @@ import { ApiResponseData } from "../../../models/api-response-data"; describe("PermissionsEndpoint", () => { - const config = new KnoraApiConfig("http", "localhost", 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig("http", "localhost", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); beforeEach(() => { diff --git a/src/api/admin/projects/projects-endpoint-admin.ts b/src/api/admin/projects/projects-endpoint-admin.ts index cf92dcb21..d1bc1cf4d 100755 --- a/src/api/admin/projects/projects-endpoint-admin.ts +++ b/src/api/admin/projects/projects-endpoint-admin.ts @@ -24,9 +24,9 @@ export class ProjectsEndpointAdmin extends Endpoint { /** * Returns a list of all projects. */ - getProjects(zioRequest: boolean = false): Observable | ApiResponseError> { + getProjects(): Observable | ApiResponseError> { - return this.httpGet("", undefined, zioRequest).pipe( + return this.httpGet("", undefined).pipe( map(ajaxResponse => ApiResponseData.fromAjaxResponse(ajaxResponse, ProjectsResponse, this.jsonConvert)), catchError(error => this.handleError(error)) ); @@ -38,9 +38,9 @@ export class ProjectsEndpointAdmin extends Endpoint { * * @param project The project to be created. */ - createProject(project: Project, zioRequest: boolean = false): Observable | ApiResponseError> { + createProject(project: Project): Observable | ApiResponseError> { - return this.httpPost("", this.jsonConvert.serializeObject(project), undefined, undefined, zioRequest).pipe( + return this.httpPost("", this.jsonConvert.serializeObject(project), undefined, undefined).pipe( map(ajaxResponse => ApiResponseData.fromAjaxResponse(ajaxResponse, ProjectResponse, this.jsonConvert)), catchError(error => this.handleError(error)) ); @@ -108,9 +108,9 @@ export class ProjectsEndpointAdmin extends Endpoint { * @param property The name of the property by which the project is identified. * @param value The value of the property by which the project is identified. */ - getProject(property: "iri" | "shortname" | "shortcode", value: string, zioRequest: boolean = false): Observable | ApiResponseError> { + getProject(property: "iri" | "shortname" | "shortcode", value: string): Observable | ApiResponseError> { - return this.httpGet("/" + encodeURIComponent(property) + "/" + encodeURIComponent(value), undefined, zioRequest).pipe( + return this.httpGet("/" + encodeURIComponent(property) + "/" + encodeURIComponent(value), undefined).pipe( map(ajaxResponse => ApiResponseData.fromAjaxResponse(ajaxResponse, ProjectResponse, this.jsonConvert)), catchError(error => this.handleError(error)) ); @@ -122,9 +122,9 @@ export class ProjectsEndpointAdmin extends Endpoint { * * @param iri The IRI of the project. */ - getProjectByIri(iri: string, zioRequest: boolean = false): Observable | ApiResponseError> { + getProjectByIri(iri: string): Observable | ApiResponseError> { - return this.getProject("iri", iri, zioRequest); + return this.getProject("iri", iri); } @@ -133,9 +133,9 @@ export class ProjectsEndpointAdmin extends Endpoint { * * @param shortname The shortname of the project. */ - getProjectByShortname(shortname: string, zioRequest: boolean = false): Observable | ApiResponseError> { + getProjectByShortname(shortname: string): Observable | ApiResponseError> { - return this.getProject("shortname", shortname, zioRequest); + return this.getProject("shortname", shortname); } @@ -144,9 +144,9 @@ export class ProjectsEndpointAdmin extends Endpoint { * * @param shortcode The shortcode of the project. */ - getProjectByShortcode(shortcode: string, zioRequest: boolean = false): Observable | ApiResponseError> { + getProjectByShortcode(shortcode: string): Observable | ApiResponseError> { - return this.getProject("shortcode", shortcode , zioRequest); + return this.getProject("shortcode", shortcode); } diff --git a/src/api/admin/projects/projects-endpoint.spec.ts b/src/api/admin/projects/projects-endpoint.spec.ts index f177bcdd3..7a84e10de 100644 --- a/src/api/admin/projects/projects-endpoint.spec.ts +++ b/src/api/admin/projects/projects-endpoint.spec.ts @@ -13,7 +13,7 @@ import { ApiResponseData } from "../../../models/api-response-data"; describe("ProjectsEndpoint", () => { - const config = new KnoraApiConfig("http", "localhost", 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig("http", "localhost", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); beforeEach(() => { diff --git a/src/api/admin/users/users-endpoint.spec.ts b/src/api/admin/users/users-endpoint.spec.ts index 3dec2ffff..fd0794b9e 100644 --- a/src/api/admin/users/users-endpoint.spec.ts +++ b/src/api/admin/users/users-endpoint.spec.ts @@ -12,7 +12,7 @@ import { ApiResponseError } from "../../../models/api-response-error"; describe("UsersEndpoint", () => { - const config = new KnoraApiConfig("http", "localhost", 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig("http", "localhost", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); beforeEach(() => { diff --git a/src/api/endpoint.ts b/src/api/endpoint.ts index dd7721c36..aa5e2de08 100644 --- a/src/api/endpoint.ts +++ b/src/api/endpoint.ts @@ -65,13 +65,11 @@ export class Endpoint { * * @param path the relative URL for the request * @param headerOpts additional headers, if any. - * @param zioRequest optional boolean to specify whether or not to use the zio http port */ - protected httpGet(path?: string, headerOpts?: IHeaderOptions, zioRequest: boolean = false): Observable { - + protected httpGet(path?: string, headerOpts?: IHeaderOptions): Observable { if (path === undefined) path = ""; - return ajax(this.setAjaxRequest(path, "GET", zioRequest, undefined, this.constructHeader(undefined, headerOpts))) + return ajax(this.setAjaxRequest(path, "GET", undefined, this.constructHeader(undefined, headerOpts))) .pipe( retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors) ); @@ -85,13 +83,12 @@ export class Endpoint { * @param body the body of the request, if any. * @param contentType content content type of body, if any. * @param headerOpts additional headers, if any. - * @param zioRequest optional boolean to specify whether or not to use the zio http port, defaults to false */ - protected httpPost(path?: string, body?: any, contentType: "json" | "sparql" = "json", headerOpts?: IHeaderOptions, zioRequest: boolean = false): Observable { + protected httpPost(path?: string, body?: any, contentType: "json" | "sparql" = "json", headerOpts?: IHeaderOptions): Observable { if (path === undefined) path = ""; - return ajax(this.setAjaxRequest(path, "POST", zioRequest, body, this.constructHeader(contentType, headerOpts))) + return ajax(this.setAjaxRequest(path, "POST", body, this.constructHeader(contentType, headerOpts))) .pipe( retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors) ); @@ -105,13 +102,12 @@ export class Endpoint { * @param body the body of the request * @param contentType content content type of body, if any. * @param headerOpts additional headers, if any. - * @param zioRequest optional boolean to specify whether or not to use the zio http port, defaults to false */ - protected httpPut(path?: string, body?: any, contentType: "json" = "json", headerOpts?: IHeaderOptions, zioRequest: boolean = false): Observable { + protected httpPut(path?: string, body?: any, contentType: "json" = "json", headerOpts?: IHeaderOptions): Observable { if (path === undefined) path = ""; - return ajax(this.setAjaxRequest(path, "PUT", zioRequest, body, this.constructHeader(contentType, headerOpts))) + return ajax(this.setAjaxRequest(path, "PUT", body, this.constructHeader(contentType, headerOpts))) .pipe( retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors) ); @@ -125,13 +121,12 @@ export class Endpoint { * @param body the body of the request * @param contentType content content type of body, if any. * @param headerOpts additional headers, if any. - * @param zioRequest optional boolean to specify whether or not to use the zio http port, defaults to false */ - protected httpPatch(path?: string, body?: any, contentType: "json" = "json", headerOpts?: IHeaderOptions, zioRequest: boolean = false): Observable { + protected httpPatch(path?: string, body?: any, contentType: "json" = "json", headerOpts?: IHeaderOptions): Observable { if (path === undefined) path = ""; - return ajax(this.setAjaxRequest(path, "PATCH", zioRequest, body, this.constructHeader(contentType, headerOpts))) + return ajax(this.setAjaxRequest(path, "PATCH", body, this.constructHeader(contentType, headerOpts))) .pipe( retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors) ); @@ -143,13 +138,12 @@ export class Endpoint { * * @param path the relative URL for the request. * @param headerOpts additional headers, if any. - * @param zioRequest optional boolean to specify whether or not to use the zio http port, defaults to false */ - protected httpDelete(path?: string, headerOpts?: IHeaderOptions, zioRequest: boolean = false): Observable { + protected httpDelete(path?: string, headerOpts?: IHeaderOptions): Observable { if (path === undefined) path = ""; - return ajax(this.setAjaxRequest(path, "DELETE", zioRequest, undefined, this.constructHeader(undefined, headerOpts))) + return ajax(this.setAjaxRequest(path, "DELETE", undefined, this.constructHeader(undefined, headerOpts))) .pipe( retryOnError(this.delay, this.maxRetries, this.retryOnErrorStatus, this.knoraApiConfig.logErrors) ); @@ -233,14 +227,13 @@ export class Endpoint { * Sets ajax request * @param path string * @param method 'GET', 'POST', 'PUT', 'PATCH' or 'DELETE' - * @param zioRequest boolean * @param [body] any * @param [headers] IHeaderOptions * @returns AjaxRequest object */ - private setAjaxRequest(path: string, method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", zioRequest: boolean, body?: any, headers?: IHeaderOptions): AjaxRequest { + private setAjaxRequest(path: string, method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE", body?: any, headers?: IHeaderOptions): AjaxRequest { - let apiUrl = zioRequest ? this.knoraApiConfig.zioApiUrl : this.knoraApiConfig.apiUrl; + let apiUrl = this.knoraApiConfig.zioEndpointsList.includes(this.path) ? this.knoraApiConfig.zioApiUrl : this.knoraApiConfig.apiUrl; let ajaxRequest: AjaxRequest = { url: apiUrl + this.path + path, diff --git a/src/api/system/health/health-endpoint.spec.ts b/src/api/system/health/health-endpoint.spec.ts index 39e0368b7..22f2f254f 100644 --- a/src/api/system/health/health-endpoint.spec.ts +++ b/src/api/system/health/health-endpoint.spec.ts @@ -5,7 +5,7 @@ import { HealthResponse } from "../../../models/system/health-response"; describe("HealthEndpoint", () => { - const config = new KnoraApiConfig("http", "localhost", 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig("http", "localhost", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); const getServerFromResponseHeader = (resHeader: string) => { diff --git a/src/api/v2/list/lists-endpoint.spec.ts b/src/api/v2/list/lists-endpoint.spec.ts index 8cba12ccb..ce7dc9a01 100644 --- a/src/api/v2/list/lists-endpoint.spec.ts +++ b/src/api/v2/list/lists-endpoint.spec.ts @@ -5,7 +5,7 @@ import { ListNodeV2 } from "../../../models/v2/lists/list-node-v2"; describe("ListsEndpoint", () => { - const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig("http", "0.0.0.0", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); beforeEach(() => { diff --git a/src/api/v2/ontology/ontologies-endpoint.spec.ts b/src/api/v2/ontology/ontologies-endpoint.spec.ts index cad72db9c..649d15b3f 100644 --- a/src/api/v2/ontology/ontologies-endpoint.spec.ts +++ b/src/api/v2/ontology/ontologies-endpoint.spec.ts @@ -35,7 +35,7 @@ import { StringLiteralV2 } from "../../../models/v2/string-literal-v2"; describe("OntologiesEndpoint", () => { - const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig("http", "0.0.0.0", 3333, undefined, undefined, true); const knoraApiConnection = new KnoraApiConnection(config); beforeEach(() => { diff --git a/src/api/v2/resource/resources-endpoint.spec.ts b/src/api/v2/resource/resources-endpoint.spec.ts index af3eaf793..c92741742 100644 --- a/src/api/v2/resource/resources-endpoint.spec.ts +++ b/src/api/v2/resource/resources-endpoint.spec.ts @@ -32,7 +32,7 @@ import { CreateUriValue } from "../../../models/v2/resources/values/create/creat describe("ResourcesEndpoint", () => { - const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, undefined, "", true); + const config = new KnoraApiConfig("http", "0.0.0.0", 3333, undefined, "", true); let knoraApiConnection: KnoraApiConnection; let getResourceClassDefinitionFromCacheSpy: jasmine.Spy; diff --git a/src/api/v2/search/search-endpoint.spec.ts b/src/api/v2/search/search-endpoint.spec.ts index 2b331ec35..ac9e04fa0 100644 --- a/src/api/v2/search/search-endpoint.spec.ts +++ b/src/api/v2/search/search-endpoint.spec.ts @@ -12,7 +12,7 @@ import { SearchEndpointV2 } from "./search-endpoint-v2"; describe("SearchEndpoint", () => { - const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, undefined, "", true); + const config = new KnoraApiConfig("http", "0.0.0.0", 3333, undefined, "", true); let knoraApiConnection: KnoraApiConnection; let getResourceClassSpy: jasmine.Spy; diff --git a/src/api/v2/values/values-endpoint.spec.ts b/src/api/v2/values/values-endpoint.spec.ts index 43298c71c..a3b40aafb 100644 --- a/src/api/v2/values/values-endpoint.spec.ts +++ b/src/api/v2/values/values-endpoint.spec.ts @@ -64,7 +64,7 @@ import { UpdateValue } from "../../../models/v2/resources/values/update/update-v import { UpdateValuePermissions } from "../../../models/v2/resources/values/update/update-value-permissions"; import { WriteValueResponse } from "../../../models/v2/resources/values/write-value-response"; -const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, undefined, undefined, true); +const config = new KnoraApiConfig("http", "0.0.0.0", 3333, undefined, undefined, true); let knoraApiConnection: KnoraApiConnection; let getResourceClassDefinitionFromCacheSpy: jasmine.Spy; diff --git a/src/cache/ListNodeV2Cache.spec.ts b/src/cache/ListNodeV2Cache.spec.ts index 0c4f544fd..2afdd3359 100644 --- a/src/cache/ListNodeV2Cache.spec.ts +++ b/src/cache/ListNodeV2Cache.spec.ts @@ -6,7 +6,7 @@ import { ListNodeV2 } from "../models/v2/lists/list-node-v2"; describe("ListNodeV2Cache", () => { - const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, "", "", true); + const config = new KnoraApiConfig("http", "0.0.0.0", 3333, "", "", true); let knoraApiConnection: KnoraApiConnection; let getNodeSpy: jasmine.Spy; diff --git a/src/cache/ontology-cache/OntologyCache.spec.ts b/src/cache/ontology-cache/OntologyCache.spec.ts index 9f9111ce8..1ff85b6a3 100644 --- a/src/cache/ontology-cache/OntologyCache.spec.ts +++ b/src/cache/ontology-cache/OntologyCache.spec.ts @@ -19,7 +19,7 @@ describe("OntologyCache", () => { describe("successful HTTP request", () => { - const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, "", "", true); + const config = new KnoraApiConfig("http", "0.0.0.0", 3333, "", "", true); let knoraApiConnection: KnoraApiConnection; let getOntoSpy: jasmine.Spy; @@ -273,7 +273,7 @@ describe("OntologyCache", () => { describe("unsuccessful HTTP request", () => { - const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, "", "", true); + const config = new KnoraApiConfig("http", "0.0.0.0", 3333, "", "", true); let knoraApiConnection: KnoraApiConnection; let getOntoSpy: jasmine.Spy; diff --git a/src/knora-api-config.spec.ts b/src/knora-api-config.spec.ts index 0dc8e9342..3f1a455a5 100644 --- a/src/knora-api-config.spec.ts +++ b/src/knora-api-config.spec.ts @@ -8,10 +8,11 @@ describe("Test class KnoraApiConfig", () => { apiProtocol: "http" | "https"; apiHost: string; apiPort?: number | null; - zioApiPort?: number | null; apiPath?: string; jsonWebToken?: string; logErrors?: boolean; + zioPrefix?: "/zio" | ":5555"; + zioEndpoints?: string[] } it("should verify parameters", () => { @@ -39,33 +40,36 @@ describe("Test class KnoraApiConfig", () => { apiProtocol: "http", apiHost: "localhost", apiPort: 1234, - zioApiPort: 4321, apiPath: "/api", - jsonWebToken: "GAGA" + jsonWebToken: "GAGA", + zioPrefix: "/zio", + zioEndpoints: ['/admin/projects'], }, { apiProtocol: "http", apiHost: "localhost", apiPort: 1234, - zioApiPort: 4321, apiPath: "/api", jsonWebToken: "GAGA", - logErrors: false + logErrors: false, + zioPrefix: ":5555", + zioEndpoints: ['/admin/projects'], }, { apiProtocol: "http", apiHost: "localhost", apiPort: 1234, - zioApiPort: 4321, apiPath: "/api", jsonWebToken: "GAGA", - logErrors: true + logErrors: true, + zioPrefix: ":5555", + zioEndpoints: ['/admin/projects'], } ]; - params.forEach(({apiProtocol, apiHost, apiPort, zioApiPort, apiPath, jsonWebToken, logErrors}) => { + params.forEach(({apiProtocol, apiHost, apiPort, apiPath, jsonWebToken, logErrors, zioPrefix, zioEndpoints}) => { - const config = new KnoraApiConfig(apiProtocol, apiHost, apiPort, zioApiPort, apiPath, jsonWebToken, logErrors); + const config = new KnoraApiConfig(apiProtocol, apiHost, apiPort, apiPath, jsonWebToken, logErrors , zioPrefix, zioEndpoints); expect(config).toEqual(jasmine.any(KnoraApiConfig)); expect(config.apiProtocol).toEqual(apiProtocol); @@ -81,10 +85,11 @@ describe("Test class KnoraApiConfig", () => { expect(config.apiPort).toEqual(apiPort === undefined ? null : apiPort); } - expect(config.zioApiPort).toEqual(zioApiPort === undefined ? null : zioApiPort) expect(config.apiPath).toEqual(apiPath === undefined ? "" : apiPath); expect(config.jsonWebToken).toEqual(jsonWebToken === undefined ? "" : jsonWebToken); expect(config.logErrors).toEqual(logErrors === undefined ? false : logErrors); + expect(config.zioPrefix).toEqual(zioPrefix === undefined ? "/zio" : zioPrefix) + expect(config.zioEndpoints).toEqual(zioEndpoints === undefined ? [] : zioEndpoints) }); @@ -99,10 +104,7 @@ describe("Test class KnoraApiConfig", () => { apiProtocol: "http" | "https", apiHost: string, apiPort?: number | null, - zioApiPort?: number | null, - apiPath?: string, - jsonWebToken?: string, - logErrors?: boolean + apiPath?: string }; result: string; } @@ -127,21 +129,21 @@ describe("Test class KnoraApiConfig", () => { result: "https://localhost" }, { - param: {apiProtocol: "https", apiHost: "domain.com", apiPort: 1234, zioApiPort: 4321}, + param: {apiProtocol: "https", apiHost: "domain.com", apiPort: 1234}, result: "https://domain.com:1234" }, { - param: {apiProtocol: "https", apiHost: "domain.com", apiPort: 1234, zioApiPort: 4321, apiPath: "/api"}, + param: {apiProtocol: "https", apiHost: "domain.com", apiPort: 1234, apiPath: "/api"}, result: "https://domain.com:1234/api" }, { - param: {apiProtocol: "https", apiHost: "domain.com", apiPort: 1234, zioApiPort: 4321, apiPath: "/api"}, + param: {apiProtocol: "https", apiHost: "domain.com", apiPort: 1234, apiPath: "/api"}, result: "https://domain.com:1234/api" } ]; data.forEach(({param, result}) => { - const config = new KnoraApiConfig(param.apiProtocol, param.apiHost, param.apiPort, param.zioApiPort, param.apiPath); + const config = new KnoraApiConfig(param.apiProtocol, param.apiHost, param.apiPort, param.apiPath); expect(config.apiUrl).toBe(result); }); @@ -149,4 +151,46 @@ describe("Test class KnoraApiConfig", () => { }); + describe("Test property zioApiUrl", () => { + + interface IZioApiUrlData { + param: { + apiProtocol: "http" | "https", + apiHost: string, + zioPrefix?: "/zio" | ":5555", + apiPath?: string, + }; + result: string; + } + + it("should return correct value", () => { + + const data: IZioApiUrlData[] = [ + { + param: {apiProtocol: "http", apiHost: "domain.com", zioPrefix: "/zio"}, + result: "http://domain.com/zio" + }, + { + param: {apiProtocol: "http", apiHost: "domain.com", zioPrefix: ":5555"}, + result: "http://domain.com:5555" + }, + { + param: {apiProtocol: "http", apiHost: "domain.com", zioPrefix: "/zio", apiPath: "/api"}, + result: "http://domain.com/zio/api" + }, + { + param: {apiProtocol: "http", apiHost: "domain.com", zioPrefix: ":5555", apiPath: "/api"}, + result: "http://domain.com:5555/api" + } + ]; + + data.forEach(({param, result}) => { + const config = new KnoraApiConfig(param.apiProtocol, param.apiHost, null, param.apiPath, undefined, false, param.zioPrefix); + expect(config.zioApiUrl).toBe(result); + }); + + }); + + }); + }); diff --git a/src/knora-api-config.ts b/src/knora-api-config.ts index 9430be08d..90d77e0e1 100644 --- a/src/knora-api-config.ts +++ b/src/knora-api-config.ts @@ -22,30 +22,43 @@ export class KnoraApiConfig { ); } + /** + * The full zio http API URL + */ get zioApiUrl(): string { return ( (this.apiProtocol + "://" + this.apiHost) + - (this.apiPort !== null ? ":" + this.zioApiPort : "") + + this.zioPrefix + this.apiPath ); } + /** + * List of endpoints using zio http + */ + get zioEndpointsList(): string[] { + return this.zioEndpoints; + } + /** * @param apiProtocol the protocol of the API (http or https) * @param apiHost the DSP-API base URL - * @param zioApiPort the port of ZIO DSP-API endpoints * @param apiPort the port of DSP-API * @param apiPath the base path following host and port, if any. * @param jsonWebToken token to identify the user * @param logErrors determines whether errors should be logged to the console + * @param zioPrefix prefix for zio routes, ":5555" is used locally whereas "/zio" is used on servers + * @param zioEndpoints list of zio endpoints */ constructor(public apiProtocol: "http" | "https", public apiHost: string, public apiPort: number | null = null, - public zioApiPort: number | null = null, public apiPath: string = "", public jsonWebToken: string = "", - public logErrors: boolean = false) { + public logErrors: boolean = false, + public zioPrefix: "/zio" | ":5555" = "/zio", + public zioEndpoints: string[] = [] + ) { // Remove port in case it's the default one if (apiProtocol === KnoraApiConfig.PROTOCOL_HTTP && apiPort === KnoraApiConfig.DEFAULT_PORT_HTTP) { diff --git a/src/models/v2/resources/ResourcesConversionUtil.spec.ts b/src/models/v2/resources/ResourcesConversionUtil.spec.ts index d55c85cc9..116a48561 100644 --- a/src/models/v2/resources/ResourcesConversionUtil.spec.ts +++ b/src/models/v2/resources/ResourcesConversionUtil.spec.ts @@ -25,7 +25,7 @@ import { ReadUriValue } from "./values/read/read-uri-value"; describe("ResourcesConversionUtil", () => { - const config = new KnoraApiConfig("http", "0.0.0.0", 3333, 5555, undefined, "", true); + const config = new KnoraApiConfig("http", "0.0.0.0", 3333, undefined, "", true); let knoraApiConnection: KnoraApiConnection; let getResourceClassDefinitionFromCacheSpy: jasmine.Spy; diff --git a/test-framework/src/app/app.component.html b/test-framework/src/app/app.component.html index 89db71e28..0f15d8685 100644 --- a/test-framework/src/app/app.component.html +++ b/test-framework/src/app/app.component.html @@ -28,15 +28,43 @@

Users

Projects

-

-

-

-

-

- + +

+

+

+

+ + +

+

+ + +

+

+

+ + +

+

+

+ + +

+

+

+ + +

+

+

- Project Info: + Project:
Project count: {{projectCount}}
+
Project info: {{projectInfo}}
+
Project keywords count: {{projectKeywordsCount}}
+
All projects keywords count: {{allProjectsKeywordsCount}}
+
Project members count: {{projectMembersCount}}
+
Project admin members count: {{projectAdminMembersCount}}
diff --git a/test-framework/src/app/app.component.ts b/test-framework/src/app/app.component.ts index 7e4bdf33e..099b2ce80 100644 --- a/test-framework/src/app/app.component.ts +++ b/test-framework/src/app/app.component.ts @@ -1,5 +1,5 @@ import { Component, OnInit } from '@angular/core'; -import { Project, ProjectResponse, ProjectsResponse } from '@dasch-swiss/dsp-js'; +import { KeywordsResponse, MembersResponse, Project, ProjectResponse, ProjectRestrictedViewSettingsResponse, ProjectsResponse, UpdateProjectRequest } from '@dasch-swiss/dsp-js'; import { AdministrativePermissionResponse, AdministrativePermissionsResponse, @@ -157,12 +157,27 @@ export class AppComponent implements OnInit { listNodePosition = 0; listNodeParentIri = ''; + + allProjectsKeywordsCount = 0; + projectCount = 0; + projectInfo = ''; + projectKeywordsCount = 0; + projectMembersCount = 0; + projectAdminMembersCount = 0; // canDoResponse: boolean; ngOnInit() { - const config = new KnoraApiConfig('http', '0.0.0.0', 3333, 5555, undefined, undefined, true); + const config = new KnoraApiConfig( + 'http', + '0.0.0.0', + 3333, + undefined, + undefined, + true, + ':5555', + ['/admin/projects']); this.knoraApiConnection = new KnoraApiConnection(config); // console.log(this.knoraApiConnection); this.userCache = new UserCache(this.knoraApiConnection); @@ -250,8 +265,8 @@ export class AppComponent implements OnInit { ); } - getProjects(zio: boolean) { - this.knoraApiConnection.admin.projectsEndpoint.getProjects(zio).subscribe( + getProjects() { + this.knoraApiConnection.admin.projectsEndpoint.getProjects().subscribe( (response: ApiResponseData) => { console.log(response); this.projectCount = response.body.projects.length; @@ -260,41 +275,164 @@ export class AppComponent implements OnInit { ); } - getProjectByIri(iri: string, zio: boolean) { - this.knoraApiConnection.admin.projectsEndpoint.getProjectByIri(iri, zio).subscribe( + getProjectByIri(iri: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectByIri(iri).subscribe( (response: ApiResponseData) => { console.log(response); + this.projectInfo = response.body.project.id; }, err => console.error('Error:', err) ); } - getProjectByShortname(shortname: string, zio: boolean) { - this.knoraApiConnection.admin.projectsEndpoint.getProjectByShortname(shortname, zio).subscribe( + getProjectByShortname(shortname: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectByShortname(shortname).subscribe( (response: ApiResponseData) => { console.log(response); + this.projectInfo = response.body.project.shortname; }, err => console.error('Error:', err) ); } - getProjectByShortcode(shortcode: string, zio: boolean) { - this.knoraApiConnection.admin.projectsEndpoint.getProjectByShortcode(shortcode, zio).subscribe( + getProjectByShortcode(shortcode: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectByShortcode(shortcode).subscribe( (response: ApiResponseData) => { console.log(response); + this.projectInfo = response.body.project.shortcode; }, err => console.error('Error:', err) ); } - createProject(zio: boolean) { + getProjectKeywords(iri: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectKeywords(iri).subscribe( + (response: ApiResponseData) => { + console.log(response); + this.projectKeywordsCount = response.body.keywords.length; + }, + err => console.error('Error:', err) + ) + } + + getProjectMembersByIri(iri: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectMembersByIri(iri).subscribe( + (response: ApiResponseData) => { + console.log(response); + this.projectMembersCount = response.body.members.length; + }, + err => console.log('Error: ', err) + ) + } + + getProjectMembersByShortname(shortname: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectMembersByShortname(shortname).subscribe( + (response: ApiResponseData) => { + console.log(response); + this.projectMembersCount = response.body.members.length; + }, + err => console.log('Error: ', err) + ) + } + + getProjectMembersByShortcode(shortcode: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectMembersByShortcode(shortcode).subscribe( + (response: ApiResponseData) => { + console.log(response); + this.projectMembersCount = response.body.members.length; + }, + err => console.log('Error: ', err) + ) + } + + getProjectAdminMembersByIri(iri: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectAdminMembersByIri(iri).subscribe( + (response: ApiResponseData) => { + console.log(response); + this.projectAdminMembersCount = response.body.members.length; + }, + err => console.log('Error: ', err) + ) + } + + getProjectAdminMembersByShortname(shortname: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectAdminMembersByShortname(shortname).subscribe( + (response: ApiResponseData) => { + console.log(response); + this.projectAdminMembersCount = response.body.members.length; + }, + err => console.log('Error: ', err) + ) + } + + getProjectAdminMembersByShortcode(shortcode: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectAdminMembersByShortcode(shortcode).subscribe( + (response: ApiResponseData) => { + console.log(response); + this.projectAdminMembersCount = response.body.members.length; + }, + err => console.log('Error: ', err) + ) + } + + getProjectRestrictedViewSettingByIri(iri: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectRestrictedViewSettingByIri(iri).subscribe( + (response: ApiResponseData) => { + console.log(response); + }, + err => console.log('Error: ', err) + ) + } + + getProjectRestrictedViewSettingByShortname(shortname: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectRestrictedViewSettingByShortname(shortname).subscribe( + (response: ApiResponseData) => { + console.log(response); + }, + err => console.log('Error: ', err) + ) + } + + getProjectRestrictedViewSettingByShortcode(shortcode: string) { + this.knoraApiConnection.admin.projectsEndpoint.getProjectRestrictedViewSettingByShortcode(shortcode).subscribe( + (response: ApiResponseData) => { + console.log(response); + }, + err => console.log('Error: ', err) + ) + } + + updateProject() { + const upr = new UpdateProjectRequest(); + upr.description = [{ language: 'en', value: 'updated anything project description'}]; + + this.knoraApiConnection.admin.projectsEndpoint.updateProject('http://rdfh.ch/projects/0001', upr).subscribe( + (response: ApiResponseData) => { + console.log(response); + }, + err => console.error('Error:', err) + ) + } + + getAllProjectsKeywords() { + this.knoraApiConnection.admin.projectsEndpoint.getKeywords().subscribe( + (response: ApiResponseData) => { + console.log(response); + this.allProjectsKeywordsCount = response.body.keywords.length; + }, + err => console.error('Error:', err) + ) + } + + createProject() { const project = new Project(); - project.shortcode = "0123"; - project.shortname = "testProject"; - project.keywords = ["test"]; + project.shortcode = '4567'; + project.shortname = 'testProject'; + project.longname = 'Test Project 4567' + project.keywords = ['test']; project.description = [{ language: 'en', value: 'test project'}]; - this.knoraApiConnection.admin.projectsEndpoint.createProject(project, zio).subscribe( + this.knoraApiConnection.admin.projectsEndpoint.createProject(project).subscribe( (response: ApiResponseData) => { console.log(response); }, @@ -302,6 +440,22 @@ export class AppComponent implements OnInit { ) } + deleteProject() { + this.knoraApiConnection.admin.projectsEndpoint.getProjectByShortcode('4567').subscribe( + (response: ApiResponseData) => { + const projectIri = response.body.project.id; + + this.knoraApiConnection.admin.projectsEndpoint.deleteProject(projectIri).subscribe( + (response: ApiResponseData) => { + console.log(response); + }, + err => console.error('Error:', err) + ) + }, + err => console.error('Error:', err) + ) + } + getPermissions() { this.knoraApiConnection.admin.permissionsEndpoint.getProjectPermissions('http://rdfh.ch/projects/0001').subscribe(