From 06b596959a8bcfde9d8349b805feb7bb8ca8b004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 1 Oct 2023 00:27:32 +0200 Subject: [PATCH] add get entry by name. --- OpenApi.yml | 23 +++++++++++ client/src/api/api.ts | 88 ++++++++++++++++++++++++++++++++++++---- internal/api/api.go | 3 +- internal/api/rooms.go | 14 +++++++ internal/room/manager.go | 18 ++++---- internal/types/room.go | 2 +- 6 files changed, 130 insertions(+), 18 deletions(-) diff --git a/OpenApi.yml b/OpenApi.yml index f6a06d9..35b7563 100644 --- a/OpenApi.yml +++ b/OpenApi.yml @@ -111,6 +111,29 @@ paths: description: OK '500': description: Internal server error + /api/rooms/{roomName}/by-name: + get: + tags: + - rooms + summary: Get room entry by name + operationId: roomGetByName + parameters: + - in: path + name: roomName + required: true + schema: + type: string + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: '#/components/schemas/RoomEntry' + '400': + description: Bad request + '500': + description: Internal server error /api/rooms/{roomId}/settings: get: tags: diff --git a/client/src/api/api.ts b/client/src/api/api.ts index e0b19f9..c5d9626 100644 --- a/client/src/api/api.ts +++ b/client/src/api/api.ts @@ -1010,6 +1010,40 @@ export const RoomsApiAxiosParamCreator = function (configuration?: Configuration + setSearchParams(localVarUrlObj, localVarQueryParameter); + let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; + localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + + return { + url: toPathString(localVarUrlObj), + options: localVarRequestOptions, + }; + }, + /** + * + * @summary Get room entry by name + * @param {string} roomName + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + roomGetByName: async (roomName: string, options: AxiosRequestConfig = {}): Promise => { + // verify required parameter 'roomName' is not null or undefined + assertParamExists('roomGetByName', 'roomName', roomName) + const localVarPath = `/api/rooms/{roomName}/by-name` + .replace(`{${"roomName"}}`, encodeURIComponent(String(roomName))); + // use dummy base URL string because the URL constructor only accepts absolute URLs. + const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL); + let baseOptions; + if (configuration) { + baseOptions = configuration.baseOptions; + } + + const localVarRequestOptions = { method: 'GET', ...baseOptions, ...options}; + const localVarHeaderParameter = {} as any; + const localVarQueryParameter = {} as any; + + + setSearchParams(localVarUrlObj, localVarQueryParameter); let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; @@ -1023,10 +1057,11 @@ export const RoomsApiAxiosParamCreator = function (configuration?: Configuration * * @summary Recreate room * @param {string} roomId + * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - roomRecreate: async (roomId: string, options: AxiosRequestConfig = {}): Promise => { + roomRecreate: async (roomId: string, roomSettings?: RoomSettings, options: AxiosRequestConfig = {}): Promise => { // verify required parameter 'roomId' is not null or undefined assertParamExists('roomRecreate', 'roomId', roomId) const localVarPath = `/api/rooms/{roomId}/recreate` @@ -1044,9 +1079,12 @@ export const RoomsApiAxiosParamCreator = function (configuration?: Configuration + localVarHeaderParameter['Content-Type'] = 'application/json'; + setSearchParams(localVarUrlObj, localVarQueryParameter); let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {}; localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers}; + localVarRequestOptions.data = serializeDataIfNeeded(roomSettings, localVarRequestOptions, configuration) return { url: toPathString(localVarUrlObj), @@ -1326,15 +1364,27 @@ export const RoomsApiFp = function(configuration?: Configuration) { const localVarAxiosArgs = await localVarAxiosParamCreator.roomGet(roomId, options); return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); }, + /** + * + * @summary Get room entry by name + * @param {string} roomName + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + async roomGetByName(roomName: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.roomGetByName(roomName, options); + return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); + }, /** * * @summary Recreate room * @param {string} roomId + * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - async roomRecreate(roomId: string, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { - const localVarAxiosArgs = await localVarAxiosParamCreator.roomRecreate(roomId, options); + async roomRecreate(roomId: string, roomSettings?: RoomSettings, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise> { + const localVarAxiosArgs = await localVarAxiosParamCreator.roomRecreate(roomId, roomSettings, options); return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration); }, /** @@ -1444,15 +1494,26 @@ export const RoomsApiFactory = function (configuration?: Configuration, basePath roomGet(roomId: string, options?: any): AxiosPromise { return localVarFp.roomGet(roomId, options).then((request) => request(axios, basePath)); }, + /** + * + * @summary Get room entry by name + * @param {string} roomName + * @param {*} [options] Override http request option. + * @throws {RequiredError} + */ + roomGetByName(roomName: string, options?: any): AxiosPromise { + return localVarFp.roomGetByName(roomName, options).then((request) => request(axios, basePath)); + }, /** * * @summary Recreate room * @param {string} roomId + * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} */ - roomRecreate(roomId: string, options?: any): AxiosPromise { - return localVarFp.roomRecreate(roomId, options).then((request) => request(axios, basePath)); + roomRecreate(roomId: string, roomSettings?: RoomSettings, options?: any): AxiosPromise { + return localVarFp.roomRecreate(roomId, roomSettings, options).then((request) => request(axios, basePath)); }, /** * @@ -1558,16 +1619,29 @@ export class RoomsApi extends BaseAPI { return RoomsApiFp(this.configuration).roomGet(roomId, options).then((request) => request(this.axios, this.basePath)); } + /** + * + * @summary Get room entry by name + * @param {string} roomName + * @param {*} [options] Override http request option. + * @throws {RequiredError} + * @memberof RoomsApi + */ + public roomGetByName(roomName: string, options?: AxiosRequestConfig) { + return RoomsApiFp(this.configuration).roomGetByName(roomName, options).then((request) => request(this.axios, this.basePath)); + } + /** * * @summary Recreate room * @param {string} roomId + * @param {RoomSettings} [roomSettings] * @param {*} [options] Override http request option. * @throws {RequiredError} * @memberof RoomsApi */ - public roomRecreate(roomId: string, options?: AxiosRequestConfig) { - return RoomsApiFp(this.configuration).roomRecreate(roomId, options).then((request) => request(this.axios, this.basePath)); + public roomRecreate(roomId: string, roomSettings?: RoomSettings, options?: AxiosRequestConfig) { + return RoomsApiFp(this.configuration).roomRecreate(roomId, roomSettings, options).then((request) => request(this.axios, this.basePath)); } /** diff --git a/internal/api/api.go b/internal/api/api.go index 353999d..52e7f59 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -48,11 +48,12 @@ func (manager *ApiManagerCtx) Mount(r chi.Router) { r.Route("/rooms/{roomId}", func(r chi.Router) { r.Get("/", manager.roomGetEntry) - r.Delete("/", manager.roomGenericAction(manager.rooms.Remove)) + r.Get("/by-name", manager.roomGetEntryByName) r.Get("/settings", manager.roomGetSettings) r.Get("/stats", manager.roomGetStats) + r.Delete("/", manager.roomGenericAction(manager.rooms.Remove)) r.Post("/start", manager.roomGenericAction(manager.rooms.Start)) r.Post("/stop", manager.roomGenericAction(manager.rooms.Stop)) r.Post("/restart", manager.roomGenericAction(manager.rooms.Restart)) diff --git a/internal/api/rooms.go b/internal/api/rooms.go index 78ae9cc..586d02e 100644 --- a/internal/api/rooms.go +++ b/internal/api/rooms.go @@ -133,6 +133,20 @@ func (manager *ApiManagerCtx) roomGetEntry(w http.ResponseWriter, r *http.Reques json.NewEncoder(w).Encode(response) } +func (manager *ApiManagerCtx) roomGetEntryByName(w http.ResponseWriter, r *http.Request) { + // roomId is actually room name here + roomName := chi.URLParam(r, "roomId") + + response, err := manager.rooms.GetEntryByName(roomName) + if err != nil { + http.Error(w, err.Error(), 500) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(response) +} + func (manager *ApiManagerCtx) roomGetSettings(w http.ResponseWriter, r *http.Request) { roomId := chi.URLParam(r, "roomId") diff --git a/internal/room/manager.go b/internal/room/manager.go index 9f43249..29ff3f6 100644 --- a/internal/room/manager.go +++ b/internal/room/manager.go @@ -82,15 +82,6 @@ func (manager *RoomManagerCtx) List(labels map[string]string) ([]types.RoomEntry return result, nil } -func (manager *RoomManagerCtx) FindByName(name string) (*types.RoomEntry, error) { - container, err := manager.containerByName(name) - if err != nil { - return nil, err - } - - return manager.containerToEntry(*container) -} - func (manager *RoomManagerCtx) Create(settings types.RoomSettings) (string, error) { if settings.Name != "" && !dockerNames.RestrictedNamePattern.MatchString(settings.Name) { return "", fmt.Errorf("invalid container name, must match %s", dockerNames.RestrictedNameChars) @@ -504,6 +495,15 @@ func (manager *RoomManagerCtx) GetEntry(id string) (*types.RoomEntry, error) { return manager.containerToEntry(*container) } +func (manager *RoomManagerCtx) GetEntryByName(name string) (*types.RoomEntry, error) { + container, err := manager.containerByName(name) + if err != nil { + return nil, err + } + + return manager.containerToEntry(*container) +} + func (manager *RoomManagerCtx) Remove(id string) error { _, err := manager.inspectContainer(id) if err != nil { diff --git a/internal/types/room.go b/internal/types/room.go index 9ebf778..f9198e6 100644 --- a/internal/types/room.go +++ b/internal/types/room.go @@ -283,10 +283,10 @@ type RoomMember struct { type RoomManager interface { Config() RoomsConfig List(labels map[string]string) ([]RoomEntry, error) - FindByName(name string) (*RoomEntry, error) Create(settings RoomSettings) (string, error) GetEntry(id string) (*RoomEntry, error) + GetEntryByName(name string) (*RoomEntry, error) GetSettings(id string) (*RoomSettings, error) GetStats(id string) (*RoomStats, error) Remove(id string) error