Skip to content

Commit

Permalink
API cleanup (#120)
Browse files Browse the repository at this point in the history
* unexport PulpAPI

* unused exports

* remove FeatureFlagsAPI, SettingsAPI, MyNamespaceAPI, and related types

* inline some types

* task list - import src/api

* cleanup unused exports, is_anonymous, settings, flags

* move caching logic to utils

* convert api to objects

drop HubAPI

* move auth handler to PulpAPI

* template strings in api urls

    rg base.delete | cut -d: -f1 | while read fn; do echo fn=$fn ; api=`rg apiPath' =' "$fn" | cut -d\' -f2 ` ; echo api=$api ;  perl -i -npe 's#base.delete\(([^,]+)\)#base.http.delete(`'"$api"'\${$1}/`)#g' "$fn"        ; done

* fixup GenericPulpAPI - make get not add pagination

* tag apis that used to be HubAPI with a FIXME

* fixup comments

* drop most AppContext.user

* drop context selectedRepo & updateTitle

* fix collections versions list api
  • Loading branch information
himdel authored Oct 26, 2024
1 parent 03a5340 commit de631f0
Show file tree
Hide file tree
Showing 98 changed files with 924 additions and 1,411 deletions.
19 changes: 1 addition & 18 deletions src/actions/ansible-repository-collection-version-add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,7 @@ const AddCollectionVersionModal = ({
};

// @ts-expect-error: TS2525: Initializer provides no value for this binding element and the binding element has no default value.
const query = ({ params } = {}) => {
const newParams = { ...params };
newParams.ordering = newParams.sort;
delete newParams.sort;

return CollectionVersionAPI.list({
...newParams,
}).then(
({
data: {
meta: { count },
data: results,
},
}) => ({
data: { count, results },
}),
);
};
const query = ({ params } = {}) => CollectionVersionAPI.list(params);

const [modalState, setModalState] = useState({});

Expand Down
19 changes: 10 additions & 9 deletions src/api/activities.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { HubAPI } from './hub';
import { PulpAPI } from './pulp';

class API extends HubAPI {
apiPath = 'v3/plugin/execution-environments/repositories/';
const base = new PulpAPI();

list(id, page) {
return super.list({ page: page }, this.apiPath + id + '/_content/history/');
}
}

export const ActivitiesAPI = new API();
// FIXME HubAPI
export const ActivitiesAPI = {
listRepo: (id, params?) =>
base.list(
`v3/plugin/execution-environments/repositories/${id}/_content/history/`,
params,
),
};
13 changes: 7 additions & 6 deletions src/api/ansible-distribution.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { PulpAPI } from './pulp';

class API extends PulpAPI {
apiPath = 'distributions/ansible/ansible/';
const base = new PulpAPI();

// list(params?)
// delete(pk)
}
export const AnsibleDistributionAPI = {
create: (data) => base.http.post(`distributions/ansible/ansible/`, data),

export const AnsibleDistributionAPI = new API();
delete: (id) => base.http.delete(`distributions/ansible/ansible/${id}/`),

list: (params?) => base.list(`distributions/ansible/ansible/`, params),
};
47 changes: 21 additions & 26 deletions src/api/ansible-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,32 @@ function smartUpdate(
return remote;
}

class API extends PulpAPI {
apiPath = 'remotes/ansible/collection/';
const base = new PulpAPI();

// create(data)
// delete(uuid)
// list(params?)
export const AnsibleRemoteAPI = {
addRole: (id, role) =>
base.http.post(`remotes/ansible/collection/${id}/add_role/`, role),

smartUpdate(pk, newValue: AnsibleRemoteType, oldValue: AnsibleRemoteType) {
const reducedData = smartUpdate(newValue, oldValue);
return super.update(pk, reducedData);
}
create: (data) => base.http.post(`remotes/ansible/collection/`, data),

update(_id, _obj) {
throw 'use smartUpdate()';
}
delete: (id) => base.http.delete(`remotes/ansible/collection/${id}/`),

listRoles(id, params?) {
return super.list(params, this.apiPath + id + '/list_roles/');
}
get: (id) => base.http.get(`remotes/ansible/collection/${id}/`),

addRole(id, role) {
return super.create(role, this.apiPath + id + '/add_role/');
}
list: (params?) => base.list(`remotes/ansible/collection/`, params),

myPermissions(id, params?) {
return super.list(params, this.apiPath + id + '/my_permissions/');
}
listRoles: (id, params?) =>
base.list(`remotes/ansible/collection/${id}/list_roles/`, params),

removeRole(id, role) {
return super.create(role, this.apiPath + id + '/remove_role/');
}
}
myPermissions: (id, params?) =>
base.list(`remotes/ansible/collection/${id}/my_permissions/`, params),

removeRole: (id, role) =>
base.http.post(`remotes/ansible/collection/${id}/remove_role/`, role),

export const AnsibleRemoteAPI = new API();
smartUpdate: (id, newValue: AnsibleRemoteType, oldValue: AnsibleRemoteType) =>
base.http.put(
`remotes/ansible/collection/${id}/`,
smartUpdate(newValue, oldValue),
),
};
114 changes: 53 additions & 61 deletions src/api/ansible-repository.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,73 @@
import { PulpAPI } from './pulp';

class API extends PulpAPI {
apiPath = 'repositories/ansible/ansible/';
const base = new PulpAPI();

// list(params?)

listVersions(pulp_id: string, params?) {
return this.list(params, this.apiPath + pulp_id + '/versions/');
}

// delete(pulp_id: string)

sync(pulp_id: string, body = {}) {
return this.http.post(this.apiPath + pulp_id + '/sync/', body);
}

revert(pulp_id: string, version_href) {
return this.http.post(this.apiPath + pulp_id + '/modify/', {
base_version: version_href,
});
}

addContent(pulp_id: string, collection_version_hrefs) {
return this.http.post(this.apiPath + pulp_id + '/modify/', {
export const AnsibleRepositoryAPI = {
addContent: (id: string, collection_version_hrefs) =>
base.http.post(`repositories/ansible/ansible/${id}/modify/`, {
add_content_units: collection_version_hrefs,
});
}
}),

removeContent(pulp_id: string, collection_version_href) {
return this.http.post(this.apiPath + pulp_id + '/modify/', {
remove_content_units: [collection_version_href],
});
}

listRoles(pulp_id: string, params?) {
return super.list(params, this.apiPath + pulp_id + '/list_roles/');
}

addRole(pulp_id: string, role) {
return super.create(role, this.apiPath + pulp_id + '/add_role/');
}
addRole: (id: string, role) =>
base.http.post(`repositories/ansible/ansible/${id}/add_role/`, role),

myPermissions(pulp_id: string, params?) {
return super.list(params, this.apiPath + pulp_id + '/my_permissions/');
}

removeRole(pulp_id: string, role) {
return super.create(role, this.apiPath + pulp_id + '/remove_role/');
}

copyCollectionVersion(
pulp_id: string,
copyCollectionVersion: (
id: string,
body: {
collection_versions: string[];
destination_repositories: string[];
signing_service?: string;
},
) {
return this.http.post(
this.apiPath + pulp_id + '/copy_collection_version/',
) =>
base.http.post(
`repositories/ansible/ansible/${id}/copy_collection_version/`,
body,
);
}
),

create: (data) => base.http.post(`repositories/ansible/ansible/`, data),

moveCollectionVersion(
pulp_id: string,
delete: (id) => base.http.delete(`repositories/ansible/ansible/${id}/`),

list: (params?) => base.list(`repositories/ansible/ansible/`, params),

listRoles: (id: string, params?) =>
base.list(`repositories/ansible/ansible/${id}/list_roles/`, params),

listVersions: (id: string, params?) =>
base.list(`repositories/ansible/ansible/${id}/versions/`, params),

moveCollectionVersion: (
id: string,
body: {
collection_versions: string[];
destination_repositories: string[];
signing_service?: string;
},
) {
return this.http.post(
this.apiPath + pulp_id + '/move_collection_version/',
) =>
base.http.post(
`repositories/ansible/ansible/${id}/move_collection_version/`,
body,
);
}
}
),

myPermissions: (id: string, params?) =>
base.list(`repositories/ansible/ansible/${id}/my_permissions/`, params),

removeContent: (id: string, collection_version_href) =>
base.http.post(`repositories/ansible/ansible/${id}/modify/`, {
remove_content_units: [collection_version_href],
}),

removeRole: (id: string, role) =>
base.http.post(`repositories/ansible/ansible/${id}/remove_role/`, role),

revert: (id: string, version_href) =>
base.http.post(`repositories/ansible/ansible/${id}/modify/`, {
base_version: version_href,
}),

sync: (id: string, body = {}) =>
base.http.post(`repositories/ansible/ansible/${id}/sync/`, body),

export const AnsibleRepositoryAPI = new API();
update: (id: string, data) =>
base.http.put(`repositories/ansible/ansible/${id}/`, data),
};
9 changes: 0 additions & 9 deletions src/api/application-info.ts

This file was deleted.

57 changes: 5 additions & 52 deletions src/api/base.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
import axios from 'axios';
import Cookies from 'js-cookie';
import { config } from 'src/ui-config';
import { ParamHelper } from 'src/utilities';

export class BaseAPI {
apiPath: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
http: any;
http: { delete; get; interceptors; patch; post; put };
sortParam: string; // translate ?sort into sortParam in list()
mapPageToOffset: boolean;

// a request URL is created from:
// * API_BASE_PATH - pulp api prefix, ends in trailing slash
// * apiPath - set by leaf API classes
// any extra id or params added by custom methods
constructor() {
this.http = axios.create({
// adapter + withCredentials ensures no popup on http basic auth fail
adapter: 'fetch',
withCredentials: false,

// baseURL set to API_BASE_PATH in authHandler
// baseURL gets set in PulpAPI
paramsSerializer: {
serialize: (params) => ParamHelper.getQueryString(params),
},
});

this.http.interceptors.request.use((request) => this.authHandler(request));
}

public mapParams(params) {
mapParams(params) {
const newParams = { ...params };

if (this.mapPageToOffset) {
Expand Down Expand Up @@ -59,44 +49,7 @@ export class BaseAPI {
};
}

list(params?: object, apiPath?: string) {
return this.http.get(this.getPath(apiPath), this.mapParams(params));
}

get(id: string, apiPath?: string) {
return this.http.get(this.getPath(apiPath) + id + '/');
}

update(id: string | number, data, apiPath?: string) {
return this.http.put(this.getPath(apiPath) + id + '/', data);
}

create(data, apiPath?: string) {
return this.http.post(this.getPath(apiPath), data);
}

delete(id: string | number, apiPath?: string) {
return this.http.delete(this.getPath(apiPath) + id + '/');
}

patch(id: string | number, data, apiPath?: string) {
return this.http.patch(this.getPath(apiPath) + id + '/', data);
}

private getPath(apiPath?: string) {
return apiPath || this.apiPath || '';
}

private async authHandler(request) {
if (!request.auth) {
request.auth = JSON.parse(
window.sessionStorage.credentials ||
window.localStorage.credentials ||
'{}',
);
}
request.baseURL = config.API_BASE_PATH;
request.headers['X-CSRFToken'] = Cookies.get('csrftoken');
return request;
list(url: string, params?) {
return this.http.get(url, this.mapParams(params));
}
}
17 changes: 10 additions & 7 deletions src/api/certificate-upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ interface UploadProps {
signed_collection: string;
}

class API extends PulpAPI {
apiPath = 'content/ansible/collection_signatures/';
const base = new PulpAPI();

export const CertificateUploadAPI = {
// Returns /pulp/api/v3/tasks/0be64cb4-3b7e-4a6b-b35d-c3b589923a90/
upload(data: UploadProps): Promise<{ data: { task: string } }> {
upload: (data: UploadProps): Promise<{ data: { task: string } }> => {
const formData = new FormData();
formData.append('file', data.file);
formData.append('repository', data.repository);
Expand All @@ -23,8 +23,11 @@ class API extends PulpAPI {
'Content-Type': 'multipart/form-data',
},
};
return this.http.post(this.apiPath, formData, config);
}
}

export const CertificateUploadAPI = new API();
return base.http.post(
`content/ansible/collection_signatures/`,
formData,
config,
);
},
};
Loading

0 comments on commit de631f0

Please sign in to comment.