Skip to content

Commit

Permalink
fix makeRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Kull Zacharias (IT-SCF-F-BIM) committed May 25, 2024
1 parent 75cc252 commit 16b791a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
25 changes: 16 additions & 9 deletions src/BitbucketServerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ interface LoginInput {
password: string;
}

interface RequestParams {
path: string
method?: string
body?: any
}

function LoginForm(props: {provider: BitbucketServerProvider, defaults: LoginInput, login: (LoginInput) => Promise<void>, setProvider: (DataProvider) => void, showError: (string) => void}) {
const { handleSubmit, register, formState } = useForm<LoginInput>({
defaultValues: props.defaults,
});
const { errors } = formState;

function submit(x: LoginInput) {
new Promise(() => console.log("start login"))
Promise.resolve()
.then(() => props.login(x))
.then(() => props.setProvider(props.provider))
.catch(e => {
console.log("submit", String(e));
props.showError(String(e));
props.provider.reset();
throw e;
Expand Down Expand Up @@ -75,16 +80,17 @@ export class BitbucketServerProvider extends DataProvider {
}
}

private makeRequest(path: string = "", method: string = 'GET', body?: any): Promise<any> {
private makeRequest(request: RequestParams): Promise<any> {
const {path, method, body} = request;
// if project isn't specified, user name is taken as project
const project = this.loginInput!.project || this.loginInput!.user;
const repoUrl = this.baseUrl + "/projects/" + project + "/repos/" + this.loginInput!.repo;
console.log("makeRequest", repoUrl+path)
// prepare headers
const headers = new Headers();
headers.append("Content-Type", "application/json");
headers.append("Authorization", this.authVal!);
// fetch
return fetch(repoUrl+path, {method: method, headers: headers, body: (body ? JSON.stringify(body) : undefined)})
return fetch(repoUrl+path, {method: method, headers: headers, body: body})
.then( response => {
if (response.ok) return response;
else throw Error( "Http error: " + response.status + " " + response.statusText + " (" + method + " "+repoUrl+path +")");
Expand All @@ -96,6 +102,7 @@ export class BitbucketServerProvider extends DataProvider {
};

login(that: BitbucketServerProvider, input: LoginInput): Promise<void> {
console.log("login");
that.loginInput = input;
that.authVal = "Basic " + btoa( that.loginInput!.user + ":" + that.loginInput!.password );
// test request
Expand All @@ -105,7 +112,7 @@ export class BitbucketServerProvider extends DataProvider {
getData() {
if(!this.loginInput) throw Error( "Connection parameters not set.");
if(!this.data) {
this.data = this.makeRequest("/raw/"+this.loginInput!.path+"?at="+this.loginInput!.branch)
this.data = this.makeRequest({path: "/raw/"+this.loginInput!.path+"?at="+this.loginInput!.branch})
.then( response => response.json())
.then( data => {
if (isArray(data)) return data as [];
Expand All @@ -122,7 +129,7 @@ export class BitbucketServerProvider extends DataProvider {
getSchema() {
if(!this.loginInput) throw Error( "Connection parameters not set.");
if(!this.config) {
this.config = this.makeRequest("/raw/"+this.getSchemaPath()+"?at="+this.loginInput!.branch)
this.config = this.makeRequest({path: "/raw/"+this.getSchemaPath()+"?at="+this.loginInput!.branch})
.then( response => response.json())
.then( data => {
if (isObject(data)) return data as {}
Expand All @@ -133,7 +140,7 @@ export class BitbucketServerProvider extends DataProvider {
};

rememberDataCommitId() {
this.dataCommitId = this.makeRequest("/commits?path="+this.loginInput!.path+"&until="+this.loginInput!.branch+"&limit=1")
this.dataCommitId = this.makeRequest({path: "/commits?path="+this.loginInput!.path+"&until="+this.loginInput!.branch+"&limit=1"})
.then( response => response.json())
.then( data => {
if (data.values.length>0) {
Expand Down Expand Up @@ -161,7 +168,7 @@ export class BitbucketServerProvider extends DataProvider {
postData.append("message", msg);
postData.append("branch", this.loginInput!.branch!);
postData.append("sourceCommitId", commitId!);
this.dataCommitId = this.makeRequest("/browse/"+this.loginInput!.path, "PUT", postData)
this.dataCommitId = this.makeRequest({path: "/browse/"+this.loginInput!.path, method: "PUT", body: postData})
.then( response => response.json())
.then( data => {
console.log("new commitId", data);
Expand Down
4 changes: 0 additions & 4 deletions src/DataProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ export abstract class DataProvider {
const idCols: string[] = this.getMetadata(schema, "idCols");
return data.forEach((e: any, idx) => this.addDataIdProperty(e, idCols.map(c => e[c]).join("-")));
})
.catch(e => {
console.log("calcDataId Promise.all", String(e));
throw e;
});
}
addDataIdProperty(e: any, id: string) {
// Object.defineProperty is used to create a property with enumerable: false, so that the artifical RECORD_ID property is not exported on save.
Expand Down
2 changes: 1 addition & 1 deletion src/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function Table(props: {show: boolean}) {
showCommitDialog(false); // also refreshes button state
// if msg is empty, commit dialog was cancelled
if (msg) {
new Promise(() => {})
Promise.resolve()
.then(() => provider.saveData(msg))
.then(() => showSnackbar({msg: "Commit successfull", severity: "info"}))
.catch(e => {
Expand Down

0 comments on commit 16b791a

Please sign in to comment.