parent
6fec8bbd1a
commit
035bfa3731
|
|
@ -1,4 +1,6 @@
|
|||
import {
|
||||
ApiClientError,
|
||||
ApiError,
|
||||
ApiResponse,
|
||||
LibreCcmApiClient,
|
||||
ListView,
|
||||
|
|
@ -32,14 +34,12 @@ export class UsersApiClient {
|
|||
}
|
||||
|
||||
async getUsers(limit: number, offset: number): Promise<ListView<User>> {
|
||||
const url = `${this.#USERS_API_PREFIX}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#USERS_API_PREFIX}`,
|
||||
{
|
||||
const response: ApiResponse = await this.#apiClient.get(url, {
|
||||
limit: limit.toString(),
|
||||
offset: offset.toString(),
|
||||
}
|
||||
);
|
||||
});
|
||||
if (response.ok) {
|
||||
const result: Record<
|
||||
string,
|
||||
|
|
@ -60,86 +60,139 @@ export class UsersApiClient {
|
|||
offset: result.offset as number,
|
||||
};
|
||||
} else {
|
||||
throw `Failed to get users:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"get",
|
||||
`Failed to get users`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get users: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(`Failed to get users: ${err}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async getUser(userIdentifier: string | number): Promise<User> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#USERS_API_PREFIX}/${buildIdentifierParam(
|
||||
const url = `${this.#USERS_API_PREFIX}/${buildIdentifierParam(
|
||||
userIdentifier
|
||||
)}`
|
||||
);
|
||||
)}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(url);
|
||||
|
||||
if (response.ok) {
|
||||
return buildUserFromRecord(
|
||||
(await response.json()) as Record<string, unknown>
|
||||
);
|
||||
} else {
|
||||
throw `Failed to get user ${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"get",
|
||||
`Failed to get user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get user ${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to get user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async addUser(user: User): Promise<string> {
|
||||
const url = `${this.#USERS_API_PREFIX}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.post(
|
||||
`${this.#USERS_API_PREFIX}`,
|
||||
url,
|
||||
JSON.stringify(user)
|
||||
);
|
||||
if (response.ok) {
|
||||
return user.name;
|
||||
} else {
|
||||
throw `Failed to add user ${user.name}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"post",
|
||||
`Failed to add user ${user.name}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to add user ${user.name}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to add user ${user.name}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async updateUser(userIdentifier: string, user: User): Promise<void> {
|
||||
const url = `${this.#USERS_API_PREFIX}/${buildIdentifierParam(
|
||||
userIdentifier
|
||||
)}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#USERS_API_PREFIX}/${buildIdentifierParam(
|
||||
userIdentifier
|
||||
)}`,
|
||||
url,
|
||||
JSON.stringify(user)
|
||||
);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to update user ${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"put",
|
||||
`Failed to update user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to update user ${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to update user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async deleteUser(userIdentifier: string): Promise<void> {
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#USERS_API_PREFIX}/${buildIdentifierParam(
|
||||
const url = `${this.#USERS_API_PREFIX}/${buildIdentifierParam(
|
||||
userIdentifier
|
||||
)}`
|
||||
);
|
||||
)}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to delete user ${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"delete",
|
||||
`Failed to delete user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to delete user ${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to delete user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -148,16 +201,13 @@ export class UsersApiClient {
|
|||
limit: number,
|
||||
offset: number
|
||||
): Promise<ListView<UserGroupMembership>> {
|
||||
try {
|
||||
const userParam = buildIdentifierParam(userIdentifier);
|
||||
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#USERS_API_PREFIX}/${userParam}/groups`,
|
||||
{
|
||||
const url = `${this.#USERS_API_PREFIX}/${userParam}/groups`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(url, {
|
||||
limit: limit.toString(),
|
||||
offset: offset.toString(),
|
||||
}
|
||||
);
|
||||
});
|
||||
if (response.ok) {
|
||||
const result: Record<
|
||||
string,
|
||||
|
|
@ -178,13 +228,22 @@ export class UsersApiClient {
|
|||
offset: result.limit as number,
|
||||
};
|
||||
} else {
|
||||
throw `Failed to get group memberships of
|
||||
user ${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"get",
|
||||
`Failed to get group memberships of user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get group memberships of user
|
||||
${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to get group memberships of user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -192,25 +251,32 @@ export class UsersApiClient {
|
|||
userIdentifier: string | number,
|
||||
groupIdentifier: string | number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const userParam = buildIdentifierParam(userIdentifier);
|
||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#USERS_API_PREFIX}/${userParam}/groups/${groupParam}`
|
||||
);
|
||||
const url = `${
|
||||
this.#USERS_API_PREFIX
|
||||
}/${userParam}/groups/${groupParam}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(url);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to add group membership for group ${groupIdentifier}
|
||||
to user
|
||||
${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"put",
|
||||
`Failed to add group membership for group ${groupIdentifier} to user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to add group membership for group ${groupIdentifier}
|
||||
to user
|
||||
${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to add group membership for group ${groupIdentifier} to user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -218,25 +284,32 @@ export class UsersApiClient {
|
|||
userIdentifier: string | number,
|
||||
groupIdentifier: string | number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const userParam = buildIdentifierParam(userIdentifier);
|
||||
const groupParam = buildIdentifierParam(groupIdentifier);
|
||||
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#USERS_API_PREFIX}/${userParam}/groups/${groupParam}`
|
||||
);
|
||||
const url = `${
|
||||
this.#USERS_API_PREFIX
|
||||
}/${userParam}/groups/${groupParam}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to remote group membership for group ${groupIdentifier}
|
||||
from user
|
||||
${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"delete",
|
||||
`Failed to remote group membership for group ${groupIdentifier} from user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to remove group membership for group ${groupIdentifier}
|
||||
from user
|
||||
${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to remove group membership for group ${groupIdentifier} from user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -244,11 +317,9 @@ export class UsersApiClient {
|
|||
userIdentifier: string
|
||||
): Promise<ListView<PartyRoleMembership>> {
|
||||
const userParam: string = buildIdentifierParam(userIdentifier);
|
||||
|
||||
const url = `${this.#USERS_API_PREFIX}/${userParam}/roles`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.get(
|
||||
`${this.#USERS_API_PREFIX}/${userParam}/roles`
|
||||
);
|
||||
const response: ApiResponse = await this.#apiClient.get(url);
|
||||
if (response.ok) {
|
||||
const result: Record<
|
||||
string,
|
||||
|
|
@ -269,11 +340,22 @@ export class UsersApiClient {
|
|||
offset: result.offset as number,
|
||||
};
|
||||
} else {
|
||||
throw `Failed to get roles of user ${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"get",
|
||||
`Failed to get roles of user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to get roles of user ${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to get roles of user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -281,22 +363,30 @@ export class UsersApiClient {
|
|||
userIdentifier: string | number,
|
||||
roleIdentifier: string | number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const userParam = buildIdentifierParam(userIdentifier);
|
||||
const roleParam = buildIdentifierParam(roleIdentifier);
|
||||
const response: ApiResponse = await this.#apiClient.put(
|
||||
`${this.#USERS_API_PREFIX}/${userParam}/roles/${roleParam}`
|
||||
);
|
||||
const url = `${this.#USERS_API_PREFIX}/${userParam}/roles/${roleParam}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.put(url);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to add role ${roleIdentifier} to
|
||||
user ${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"put",
|
||||
`Failed to add role ${roleIdentifier} to user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to add role ${roleIdentifier} to
|
||||
user ${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to add role ${roleIdentifier} to user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -304,23 +394,30 @@ export class UsersApiClient {
|
|||
userIdentifier: string | number,
|
||||
roleIdentifier: string | number
|
||||
): Promise<void> {
|
||||
try {
|
||||
const userParam = buildIdentifierParam(userIdentifier);
|
||||
const roleParam = buildIdentifierParam(roleIdentifier);
|
||||
const response: ApiResponse = await this.#apiClient.delete(
|
||||
`${this.#USERS_API_PREFIX}/${userParam}/roles/${roleParam}`
|
||||
);
|
||||
const url = `${this.#USERS_API_PREFIX}/${userParam}/roles/${roleParam}`;
|
||||
try {
|
||||
const response: ApiResponse = await this.#apiClient.delete(url);
|
||||
if (response.ok) {
|
||||
return;
|
||||
} else {
|
||||
throw `Failed to remove role ${roleIdentifier} from
|
||||
user ${userIdentifier}:
|
||||
${response.status} ${response.statusText}`;
|
||||
throw new ApiError(
|
||||
response.status,
|
||||
response.statusText,
|
||||
"delete",
|
||||
`Failed to remove role ${roleIdentifier} from user ${userIdentifier}`,
|
||||
url
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
throw `Failed to remote role ${roleIdentifier} from
|
||||
user ${userIdentifier}: ${err}`;
|
||||
if (err instanceof ApiError) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new ApiClientError(
|
||||
`Failed to remote role ${roleIdentifier} from user ${userIdentifier}: ${err}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue