Skip to content

Commit 722b1d2

Browse files
himdelpatchback[bot]
authored andcommitted
API: expand getUIPath, simplify base classes (#4525)
* API: expand getUIPath, simplify base classes * separate apiPath (which can have slashes) from the rest (which shouldnt, except EE names) * getUsedDependencies - remove cancelToken, only practical for longer-running requests like upload * mapParams - replace mapPageToOffset calls with mapParams to also include sort * No-Issue * mapParams - make mapPageToOffset a bool * constructor cant read class variables * used by depenendencies - use HubListToolbar mostly because the onChange-based filter would really need a cancel token, but we want unified filters everywhere too (cherry picked from commit 71a1f11)
1 parent 2b40610 commit 722b1d2

29 files changed

+133
-202
lines changed

src/api/active-user.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { HubAPI } from './hub';
22
import { UserType } from './response-types/user';
33

44
class API extends HubAPI {
5-
apiPath = this.getUIPath('me/');
5+
apiPath = '_ui/v1/me/';
66

77
getUser(): Promise<UserType> {
88
return this.http.get(this.apiPath).then((result) => result.data);
@@ -28,13 +28,13 @@ class API extends HubAPI {
2828
// Note: This does not reset the app's authentication state. That has to be done
2929
// separately by setting the user state in the app's root component
3030
logout() {
31-
return this.http.post(this.getUIPath('auth/logout/'), {});
31+
return this.http.post('_ui/v1/auth/logout/', {});
3232
}
3333

3434
// Note: This does not reset the app's authentication state. That has to be done
3535
// separately by setting the user state in the app's root component
3636
login(username, password) {
37-
const loginURL = this.getUIPath('auth/login/');
37+
const loginURL = '_ui/v1/auth/login/';
3838

3939
// Make a get request to the login endpoint to set CSRF tokens before making
4040
// the authentication reqest

src/api/application-info.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { HubAPI } from './hub';
22

33
class API extends HubAPI {
4-
apiPath = '';
5-
64
get() {
75
return super.get('');
86
}

src/api/base.ts

+33-31
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,17 @@ export class BaseAPI {
77
apiPath: string;
88
// eslint-disable-next-line @typescript-eslint/no-explicit-any
99
http: any;
10-
sortParam = 'sort'; // translate ?sort into sortParam in list()
11-
10+
sortParam: string; // translate ?sort into sortParam in list()
11+
mapPageToOffset: boolean;
12+
13+
// a request URL is created from:
14+
// * API_HOST - optional, for use with different hostname
15+
// * apiBaseUrl - api/pulp prefix, ends in trailing slash
16+
// * apiPath - set by leaf API classes
17+
// any extra id or params added by custom methods
1218
constructor(apiBaseUrl) {
1319
this.http = axios.create({
14-
baseURL: apiBaseUrl,
20+
baseURL: API_HOST + apiBaseUrl,
1521
paramsSerializer: {
1622
serialize: (params) => ParamHelper.getQueryString(params),
1723
},
@@ -20,43 +26,39 @@ export class BaseAPI {
2026
this.http.interceptors.request.use((request) => this.authHandler(request));
2127
}
2228

23-
// The api uses offset/limit OR page/page_size for pagination
24-
// the UI uses page/page size and maps to whatever the api expects
25-
// (override mapPageToOffset for page)
26-
public mapPageToOffset(p) {
27-
// Need to copy the object to make sure we aren't accidentally
28-
// setting page state
29-
const params = { ...p };
29+
public mapParams(params) {
30+
const newParams = { ...params };
3031

31-
const pageSize =
32-
parseInt(params['page_size']) || Constants.DEFAULT_PAGE_SIZE;
33-
const page = parseInt(params['page']) || 1;
32+
if (this.mapPageToOffset) {
33+
// The api uses offset/limit OR page/page_size for pagination
34+
// the UI uses page/page size and maps to whatever the api expects
3435

35-
delete params['page'];
36-
delete params['page_size'];
36+
const pageSize =
37+
parseInt(newParams['page_size'], 10) || Constants.DEFAULT_PAGE_SIZE;
38+
const page = parseInt(newParams['page'], 10) || 1;
3739

38-
params['offset'] = page * pageSize - pageSize;
39-
params['limit'] = pageSize;
40+
delete newParams['page'];
41+
delete newParams['page_size'];
4042

41-
return params;
42-
}
43+
newParams['offset'] = page * pageSize - pageSize;
44+
newParams['limit'] = pageSize;
45+
}
46+
47+
if (this.sortParam && newParams['sort'] && this.sortParam !== 'sort') {
48+
// The api uses sort/ordering/order_by for sort
49+
// the UI uses sort and maps to whatever the api expects
4350

44-
// The api uses sort/ordering/order_by for sort
45-
// the UI uses sort and maps to whatever the api expects
46-
// (set sortParam)
47-
public mapSort(params) {
48-
const newParams = { ...params };
49-
if (newParams['sort'] && this.sortParam !== 'sort') {
5051
newParams[this.sortParam] = newParams['sort'];
5152
delete newParams['sort'];
5253
}
53-
return newParams;
54+
55+
return {
56+
params: newParams,
57+
};
5458
}
5559

5660
list(params?: object, apiPath?: string) {
57-
return this.http.get(this.getPath(apiPath), {
58-
params: this.mapSort(this.mapPageToOffset(params)),
59-
});
61+
return this.http.get(this.getPath(apiPath), this.mapParams(params));
6062
}
6163

6264
get(id: string, apiPath?: string) {
@@ -79,8 +81,8 @@ export class BaseAPI {
7981
return this.http.patch(this.getPath(apiPath) + id + '/', data);
8082
}
8183

82-
getPath(apiPath?: string) {
83-
return apiPath || this.apiPath;
84+
private getPath(apiPath?: string) {
85+
return apiPath || this.apiPath || '';
8486
}
8587

8688
private async authHandler(request) {

src/api/collection-version.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,10 @@ export class API extends HubAPI {
3030
return super.get(id, 'pulp/api/v3/content/ansible/collection_versions/');
3131
}
3232

33-
getUsedDependenciesByCollection(
34-
namespace,
35-
collection,
36-
params = {},
37-
cancelToken = undefined,
38-
) {
33+
getUsedDependenciesByCollection(namespace, collection, params = {}) {
3934
return this.http.get(
40-
`${this.apiPath}?dependency=${namespace}.${collection}`,
41-
{ params: this.mapPageToOffset(params), cancelToken: cancelToken?.token },
35+
this.apiPath + `?dependency=${namespace}.${collection}`,
36+
this.mapParams(params),
4237
);
4338
}
4439

src/api/collection.ts

+4-11
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function filterListItem(item: CollectionListType) {
3333
}
3434

3535
export class API extends HubAPI {
36-
apiPath = this.getUIPath('repo/');
36+
apiPath = '_ui/v1/repo/';
3737
cachedCollection: CollectionDetailType;
3838

3939
list(params?, repo?: string) {
@@ -144,17 +144,10 @@ export class API extends HubAPI {
144144
);
145145
}
146146

147-
getUsedDependenciesByCollection(
148-
namespace,
149-
collection,
150-
params = {},
151-
cancelToken = undefined,
152-
) {
147+
getUsedDependenciesByCollection(namespace, collection, params = {}) {
153148
return this.http.get(
154-
this.getUIPath(
155-
`collection-versions/?dependency=${namespace}.${collection}`,
156-
),
157-
{ params: this.mapPageToOffset(params), cancelToken: cancelToken?.token },
149+
`_ui/v1/collection-versions/?dependency=${namespace}.${collection}`,
150+
this.mapParams(params),
158151
);
159152
}
160153

src/api/container-tag.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ class API extends PulpAPI {
44
apiPath = 'repositories/container/container-push/';
55

66
tag(repositoryID: string, tag: string, digest: string) {
7-
return this.http.post(`${this.apiPath}${repositoryID}/tag/`, {
7+
return this.http.post(this.apiPath + `${repositoryID}/tag/`, {
88
digest: digest,
99
tag: tag,
1010
});
1111
}
1212

1313
untag(repositoryID: string, tag: string) {
14-
return this.http.post(`${this.apiPath}${repositoryID}/untag/`, {
14+
return this.http.post(this.apiPath + `${repositoryID}/untag/`, {
1515
tag: tag,
1616
});
1717
}

src/api/controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HubAPI } from './hub';
22

33
export class API extends HubAPI {
4-
apiPath = this.getUIPath('controllers/');
4+
apiPath = '_ui/v1/controllers/';
55

66
// list(params?)
77
}

src/api/execution-environment-registry.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function smartUpdate(remote: RemoteType, unmodifiedRemote: RemoteType) {
3232
}
3333

3434
class API extends HubAPI {
35-
apiPath = this.getUIPath('execution-environments/registries/');
35+
apiPath = '_ui/v1/execution-environments/registries/';
3636

3737
// list(params?)
3838
// create(data)

src/api/execution-environment-remote.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HubAPI } from './hub';
22

33
class API extends HubAPI {
4-
apiPath = this.getUIPath('execution-environments/remotes/');
4+
apiPath = '_ui/v1/execution-environments/remotes/';
55

66
// list(params?)
77
// create(data)

src/api/execution-environment.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,41 @@ import { HubAPI } from './hub';
22

33
class API extends HubAPI {
44
apiPath = 'v3/plugin/execution-environments/repositories/';
5+
56
readme(name) {
6-
return this.http.get(this.apiPath + name + '/_content/readme/');
7+
return this.http.get(this.apiPath + `${name}/_content/readme/`);
78
}
89

910
saveReadme(name, readme) {
10-
return this.http.put(this.apiPath + name + '/_content/readme/', readme);
11+
return this.http.put(this.apiPath + `${name}/_content/readme/`, readme);
1112
}
1213

1314
images(name, params) {
14-
return this.http.get(this.apiPath + name + '/_content/images/', {
15-
params: this.mapPageToOffset(params),
16-
});
15+
return this.http.get(
16+
this.apiPath + `${name}/_content/images/`,
17+
this.mapParams(params),
18+
);
1719
}
1820

1921
image(name, digest) {
20-
return this.http.get(`${this.apiPath}${name}/_content/images/${digest}/`);
22+
return this.http.get(this.apiPath + `${name}/_content/images/${digest}/`);
2123
}
2224

2325
tags(name, params) {
24-
return this.http.get(this.apiPath + name + '/_content/tags/', {
25-
params: this.mapPageToOffset(params),
26-
});
26+
return this.http.get(
27+
this.apiPath + `${name}/_content/tags/`,
28+
this.mapParams(params),
29+
);
2730
}
2831

2932
deleteImage(name, manifest) {
3033
return this.http.delete(
31-
`${this.apiPath}${name}/_content/images/${manifest}/`,
34+
this.apiPath + `${name}/_content/images/${manifest}/`,
3235
);
3336
}
3437

3538
deleteExecutionEnvironment(name) {
36-
return this.http.delete(`${this.apiPath}${name}/`);
39+
return this.http.delete(this.apiPath + `${name}/`);
3740
}
3841
}
3942

src/api/feature-flags.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HubAPI } from './hub';
22

33
class API extends HubAPI {
4-
apiPath = this.getUIPath('feature-flags/');
4+
apiPath = '_ui/v1/feature-flags/';
55

66
get() {
77
return this.http.get(this.apiPath);

src/api/generic-pulp.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { PulpAPI } from './pulp';
22

33
export class API extends PulpAPI {
4-
apiPath = '';
5-
6-
get(id: string, apiPath?: string) {
7-
return this.http.get(this.getPath(apiPath) + id);
4+
// base get adds a trailing slash
5+
get(url: string) {
6+
return this.http.get(url);
87
}
98
}
109

src/api/group-role.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ class API extends PulpAPI {
44
apiPath = 'groups/';
55

66
listRoles(groupId, params?) {
7-
return super.list(params, `${this.apiPath}${groupId}/roles/`);
7+
return super.list(params, this.apiPath + `${groupId}/roles/`);
88
}
99

1010
removeRole(groupId, roleId) {
11-
return this.http.delete(`${this.apiPath}${groupId}/roles/${roleId}/`);
11+
return this.http.delete(this.apiPath + `${groupId}/roles/${roleId}/`);
1212
}
1313

1414
addRoleToGroup(groupId, role) {
15-
return this.http.post(`${this.apiPath}${groupId}/roles/`, {
15+
return this.http.post(this.apiPath + `${groupId}/roles/`, {
1616
role: role.name,
1717
// required field, can be empty
1818
content_object: null,

src/api/group.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HubAPI } from './hub';
22

33
class API extends HubAPI {
4-
apiPath = this.getUIPath('groups/');
4+
apiPath = '_ui/v1/groups/';
55
}
66

77
export const GroupAPI = new API();

src/api/hub.ts

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import { BaseAPI } from './base';
22

33
export class HubAPI extends BaseAPI {
4-
UI_API_VERSION = 'v1';
4+
mapPageToOffset = true; // offset & limit
5+
sortParam = 'sort';
56

67
constructor() {
7-
super(API_HOST + API_BASE_PATH);
8-
}
9-
10-
// Use this function to get paths in the _ui API. That will ensure the API version
11-
// gets updated when it changes
12-
getUIPath(url: string) {
13-
return `_ui/${this.UI_API_VERSION}/${url}`;
8+
super(API_BASE_PATH);
149
}
1510
}

src/api/import.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HubAPI } from './hub';
22

33
export class API extends HubAPI {
4-
apiPath = this.getUIPath('imports/collections/');
4+
apiPath = '_ui/v1/imports/collections/';
55

66
get(id, path?) {
77
// call this to generate more task messages

src/api/legacy.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import { BaseAPI } from './base';
22

33
export class LegacyAPI extends BaseAPI {
4+
mapPageToOffset = false; // page & page_size
45
sortParam = 'order_by';
56

67
constructor() {
7-
super(API_HOST + API_BASE_PATH);
8-
}
9-
10-
public mapPageToOffset(p) {
11-
// override BaseAPI's function to persist page & page_size
12-
return p;
8+
super(API_BASE_PATH);
139
}
1410
}

src/api/my-distribution.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HubAPI } from './hub';
22

33
class API extends HubAPI {
4-
apiPath = this.getUIPath('my-distributions/');
4+
apiPath = '_ui/v1/my-distributions/';
55
}
66

77
export const MyDistributionAPI = new API();

src/api/my-namespace.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HubAPI } from './hub';
22

33
class API extends HubAPI {
4-
apiPath = this.getUIPath('my-namespaces/');
4+
apiPath = '_ui/v1/my-namespaces/';
55

66
get(id: string, params = {}) {
77
return this.http.get(this.apiPath + id + '/', { params });

0 commit comments

Comments
 (0)