Skip to content

Commit

Permalink
- added ability to use "?prop=label" when getting Ktbs roots
Browse files Browse the repository at this point in the history
- added HTTP requests header "X-Requested-With": "XMLHttpRequest" in order to let server recognize API requests (will be usefull to improve authentication workflow : see #4)
  • Loading branch information
hsaladin committed Jul 7, 2020
1 parent 4d6da86 commit 1a5fe5c
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 52 deletions.
56 changes: 31 additions & 25 deletions ktbs-api/Ktbs.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,27 +100,6 @@ export class Ktbs extends Resource {
throw new KtbsError("This Ktbs service does not support builtin method \"" + builtin_method_id + "\"");
}

/**
* Gets the URIs of the bases in the Ktbs root
* \return Array of URL
* \protected
*/
_get_bases_uris() {
if(!this._bases_uris) {
this._bases_uris = new Array();

if(this._JSONData.hasBase instanceof Array) {
for(let i = 0; i < this._JSONData.hasBase.length; i++) {
let base_uri_string = this._JSONData.hasBase[i];
let base_uri = this.resolve_link_uri(base_uri_string);
this._bases_uris.push(base_uri);
}
}
}

return this._bases_uris;
}

/**
* Gets the bases in the Ktbs root
* \return Array of Base
Expand All @@ -129,11 +108,24 @@ export class Ktbs extends Resource {
get bases() {
if(!this._bases) {
this._bases = new Array();
let bases_uris = this._get_bases_uris();

for(let i = 0; i < bases_uris.length; i++) {
let base_uri = bases_uris[i];
let base = ResourceMultiton.get_resource(Base, base_uri);
for(let i = 0; i < this._JSONData.hasBase.length; i++) {
const base_data = this._JSONData.hasBase[i];
let base;

if(base_data instanceof Object) {
const base_uri = this.resolve_link_uri(base_data["@id"]);
const base_label = base_data["label"];
base = ResourceMultiton.get_resource(Base, base_uri);

if(base_label && !base.label)
base.label = base_label;
}
else {
const base_uri = this.resolve_link_uri(base_data);
base = ResourceMultiton.get_resource(Base, base_uri);
}

this._bases.push(base);
}
}
Expand All @@ -157,4 +149,18 @@ export class Ktbs extends Resource {
delete(abortSignal = null, credentials = null) {
throw new KtbsError("Ktbs roots can not be deleted");
}

/**
* Gets the uri to query in order to read resource's data (For some resource types, this might be different from the resource URI, for instance if we need to add some query parameters. In such case, descending resource types must override this method)
* \return URL
* \protected
*/
get _data_read_uri() {
if(!this._dataReadUri) {
this._dataReadUri = new URL(this.uri);
this._dataReadUri.searchParams.append("prop", "label");
}

return this._dataReadUri;
}
}
60 changes: 33 additions & 27 deletions ktbs-api/Resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ export class Resource {

if(window.localStorage)
this._removeOwnCredentialsFromStorage(localStorage);

this._credentials = null;
}

/**
Expand Down Expand Up @@ -514,21 +516,21 @@ export class Resource {
* \public
*/
get credentials() {
let credentials = null;

if(window.sessionStorage)
credentials = this._getOwnCredentialsFromStorage(window.sessionStorage);

if((credentials == null) && (window.localStorage))
credentials = this._getOwnCredentialsFromStorage(window.localStorage);

if((credentials == null) && (this._use_parent_credentials) && (window.sessionStorage))
credentials = this._getParentsCredentialsFromStorage(window.sessionStorage);

if((credentials == null) && (this._use_parent_credentials) && (window.localStorage))
credentials = this._getParentsCredentialsFromStorage(window.localStorage);
if(!this._credentials) {
if(window.sessionStorage)
this._credentials = this._getOwnCredentialsFromStorage(window.sessionStorage);

if(!this._credentials && (window.localStorage))
this._credentials = this._getOwnCredentialsFromStorage(window.localStorage);

if(!this._credentials && (this._use_parent_credentials) && (window.sessionStorage))
this._credentials = this._getParentsCredentialsFromStorage(window.sessionStorage);

if(!this._credentials && (this._use_parent_credentials) && (window.localStorage))
this._credentials = this._getParentsCredentialsFromStorage(window.localStorage);
}

return credentials;
return this._credentials;
}

/**
Expand Down Expand Up @@ -574,17 +576,18 @@ export class Resource {
let fetchParameters = {
method: "GET",
headers: new Headers({
"Accept": "application/json"
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest"
}),
cache: "default"
};

if(credentials == null)
if(!credentials && this.credentials)
credentials = this.credentials;

if((credentials != null) && credentials.id && credentials.password)
if(credentials && credentials.id && credentials.password)
fetchParameters.headers.append("Authorization", "Basic " + btoa(credentials.id + ":" + credentials.password));

if(this._etag)
fetchParameters.headers.append("If-None-Match", this._etag);

Expand Down Expand Up @@ -780,15 +783,16 @@ export class Resource {
let fetchParameters = {
method: "POST",
headers: new Headers({
"content-type": "application/json"
"content-type": "application/json",
"X-Requested-With": "XMLHttpRequest"
}),
body: postBody
};

if(credentials == null)
if(!credentials && this.credentials)
credentials = this.credentials;

if((credentials != null) && credentials.id && credentials.password)
if(credentials && credentials.id && credentials.password)
fetchParameters.headers.append("Authorization", "Basic " + btoa(credentials.id + ":" + credentials.password));

if(abortSignal)
Expand Down Expand Up @@ -900,15 +904,16 @@ export class Resource {
headers: new Headers({
"Accept": "application/json",
"content-type": "application/json",
"If-Match": this._etag
"If-Match": this._etag,
"X-Requested-With": "XMLHttpRequest"
}),
body: JSON.stringify(this._getPutData())
};

if(credentials == null)
if(!credentials && this.credentials)
credentials = this.credentials;

if((credentials != null) && credentials.id && credentials.password)
if(credentials && credentials.id && credentials.password)
fetchParameters.headers.append("Authorization", "Basic " + btoa(credentials.id + ":" + credentials.password));

if(abortSignal)
Expand Down Expand Up @@ -974,17 +979,18 @@ export class Resource {
let fetchParameters = {
method: "DELETE",
headers: new Headers({
"Accept": "application/json"
"Accept": "application/json",
"X-Requested-With": "XMLHttpRequest"
}),
};

if(this._etag)
fetchParameters.headers.append("If-Match", this._etag);

if(credentials == null)
if(!credentials && this.credentials)
credentials = this.credentials;

if((credentials != null) && credentials.id && credentials.password)
if(credentials && credentials.id && credentials.password)
fetchParameters.headers.append("Authorization", "Basic " + btoa(credentials.id + ":" + credentials.password));

if(abortSignal)
Expand Down

0 comments on commit 1a5fe5c

Please sign in to comment.