Bug 32939: Use APIClient to replace PATCH requests

Signed-off-by: Matt Blenkinsop <matt.blenkinsop@ptfs-europe.com>
Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Jonathan Druart 2023-02-21 13:09:23 +01:00 committed by Tomas Cohen Arazi
parent 7d95acb402
commit 821808ec31
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
5 changed files with 70 additions and 57 deletions

View file

@ -170,26 +170,16 @@ export default {
},
edit_selected(is_selected) {
this.updating_is_selected = true
fetch(
"/api/v1/erm/eholdings/ebsco/packages/" +
this.erm_package.package_id,
{
method: "PATCH",
body: JSON.stringify({ is_selected }),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
}
)
.then(checkError)
.then(result => {
const client = APIClient.erm
client.EBSCOPackages.patch(this.erm_package.package_id, {
is_selected,
}).then(
result => {
// Refresh the page. We should not need that actually.
this.getPackage(this.erm_package.package_id)
})
.catch(error => {
setError(error)
})
},
error => {}
)
},
add_to_holdings() {
this.edit_selected(true)

View file

@ -116,7 +116,6 @@
<script>
import { inject } from "vue"
import { storeToRefs } from "pinia"
import { checkError } from "../../fetch/erm.js"
import { APIClient } from "../../fetch/api-client.js"
export default {
@ -169,26 +168,16 @@ export default {
},
edit_selected(is_selected) {
this.updating_is_selected = true
fetch(
"/api/v1/erm/eholdings/ebsco/resources/" +
this.resource.resource_id,
{
method: "PATCH",
body: JSON.stringify({ is_selected }),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
}
)
.then(checkError)
.then(result => {
const client = APIClient.erm
client.EBSCOResources.patch(this.resource.resource_id, {
is_selected,
}).then(
result => {
// Refresh the page. We should not need that actually.
this.getResource(this.resource.resource_id)
})
.catch(error => {
setError(error)
})
},
error => {}
)
},
add_to_holdings() {
this.edit_selected(true)

View file

@ -65,22 +65,14 @@ export default {
return
}
if (!list_id) return
await fetch("/api/v1/erm/eholdings/local/titles/import", {
method: "POST",
body: JSON.stringify({ list_id, package_id: this.package_id }),
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
})
.then(checkError)
const client = APIClient.erm
client.localTitles
.import({ list_id, package_id: this.package_id })
.then(
result => {
this.job_id = result.job_id
},
error => {
setError(error)
}
error => {}
)
},
build_datatable: function () {

View file

@ -155,6 +155,11 @@ export class ERMAPIClient extends HttpClient {
...(query && { q: JSON.stringify(query) }),
}),
}),
import: (body) =>
this.post({
endpoint: "eholdings/local/titles/import",
body,
}),
};
}
@ -170,7 +175,6 @@ export class ERMAPIClient extends HttpClient {
};
}
get EBSCOPackages() {
return {
get: (id) =>
@ -191,6 +195,11 @@ export class ERMAPIClient extends HttpClient {
"x-koha-embed": "resources+count,vendor.name",
},
}),
patch: (id, body) =>
this.patch({
endpoint: "eholdings/ebsco/packages/" + id,
body,
}),
};
}
@ -220,6 +229,11 @@ export class ERMAPIClient extends HttpClient {
"x-koha-embed": "title,package,vendor",
},
}),
patch: (id, body) =>
this.patch({
endpoint: "eholdings/ebsco/packages/" + id,
body,
}),
};
}
}

View file

@ -47,27 +47,42 @@ class HttpClient {
}
post(params = {}) {
const body = params.body
? typeof str === "string"
? params.body
: JSON.stringify(params.body)
: undefined;
return this._fetchJSON(params.endpoint, params.headers, {
...params.options,
body: params.body ? JSON.stringify(params.body) : undefined,
body,
method: "POST",
});
}
put(params = {}) {
const body = params.body
? typeof str === "string"
? params.body
: JSON.stringify(params.body)
: undefined;
return this._fetchJSON(params.endpoint, params.headers, {
...params.options,
body: params.body ? JSON.stringify(params.body) : undefined,
body,
method: "PUT",
});
}
delete(params = {}) {
return this._fetchJSON(params.endpoint, params.headers, {
parseResponse: false,
...params.options,
method: "DELETE",
}, true);
return this._fetchJSON(
params.endpoint,
params.headers,
{
parseResponse: false,
...params.options,
method: "DELETE",
},
true
);
}
count(params = {}) {
@ -84,6 +99,19 @@ class HttpClient {
);
}
patch(params = {}) {
const body = params.body
? typeof str === "string"
? params.body
: JSON.stringify(params.body)
: undefined;
return this._fetchJSON(params.endpoint, params.headers, {
...params.options,
body,
method: "PATCH",
});
}
checkError(response, return_response = 0) {
if (response.status >= 200 && response.status <= 299) {
return return_response ? response : response.json();