Skip to content

Commit

Permalink
fix: connecting to non-appbase apps
Browse files Browse the repository at this point in the history
  • Loading branch information
lakhansamani committed Feb 25, 2019
1 parent fe7fd8c commit f0d7d5c
Showing 1 changed file with 127 additions and 80 deletions.
207 changes: 127 additions & 80 deletions app/shared/appbase.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,157 +3,220 @@ import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
declare var Appbase;

@Injectable()
function parse_url(url: string) {
var pattern = RegExp(
'^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\\?([^#]*))?(#(.*))?',
);
var matches = url.match(pattern);
var hasAuth = matches[4].indexOf('@') > -1;
var href = '';
var auth = '';
var username = '';
var password = '';
if (hasAuth) {
var urlSplit = matches[4].split('@');
auth = urlSplit[0];
href = matches[2] + '://' + urlSplit[1];
var authSplit = auth.split(':');
username = authSplit[0];
password = authSplit[1];
} else {
href = matches[2] + '://' + matches[4];
}

return {
scheme: matches[2],
href: href,
auth: auth,
path: matches[5],
query: matches[7],
fragment: matches[9],
username: username,
password: password,
};
}

@Injectable()
export class AppbaseService {
constructor(private http: Http) {}
public requestParam: any = {
url: null,
auth: null
auth: null,
};
public config: any = {
username: null,
password: null
password: null,
};
public appbaseRef: any;
public resultStream: any = null;
setAppbase(config: any) {
this.config.username = config.username;
this.config.password = config.password;
this.requestParam.pureurl = config.url;
var parsedUrl = parse_url(config.url);
this.config.username = parsedUrl.username;
this.config.password = parsedUrl.password;
this.requestParam.pureurl = parsedUrl.href;
if (config.appname) {
this.requestParam.url = config.url + '/' + config.appname;
} else {
this.requestParam.url = config.url;
}

let appbaseRef: any = {
url: "https://scalr.api.appbase.io",
app: config.appname
}

if (config.username) {
appbaseRef.credentials = `${config.username}:${config.password}`;
this.requestParam.auth = "Basic " + btoa(config.username + ':' + config.password);
url: parsedUrl.href,
app: config.appname,
};
console.log('setting up appbase');
if (parsedUrl.username) {
appbaseRef.credentials = `${parsedUrl.username}:${
parsedUrl.password
}`;
this.requestParam.auth =
'Basic ' + btoa(parsedUrl.username + ':' + parsedUrl.password);
} else {
this.requestParam.auth = '';
}

this.appbaseRef = new Appbase(appbaseRef);
}
get(path: string) {
let headersObj: any = {
'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/json;charset=UTF-8',
};

if (this.requestParam.auth) {
headersObj.Authorization = this.requestParam.auth
headersObj.Authorization = this.requestParam.auth;
}

let headers = new Headers(headersObj);
var request_url = this.requestParam.url.replace(this.config.username + ':' + this.config.password + '@', '');
var request_url = this.requestParam.url.replace(
this.config.username + ':' + this.config.password + '@',
'',
);
var request_path = request_url + path + '/';
return this.http.get(request_path, { headers: headers }).toPromise();
}
getMappings() {
var self = this;
return new Promise((resolve, reject) => {
getRequest('/_mapping').then(function(res) {
let mappingData = res.json();
getRequest('/_alias').then(function(res) {
let aliasData = res.json();
for (let index in aliasData) {
for (let alias in aliasData[index].aliases) {
mappingData[alias] = mappingData[index];
}
}
resolve(mappingData);
}).catch(function(e) {
resolve(mappingData);
getRequest('/_mapping')
.then(function(res) {
let mappingData = res.json();
getRequest('/_alias')
.then(function(res) {
let aliasData = res.json();
for (let index in aliasData) {
for (let alias in aliasData[index].aliases) {
mappingData[alias] = mappingData[index];
}
}
resolve(mappingData);
})
.catch(function(e) {
resolve(mappingData);
});
})
.catch(function(e) {
reject(e);
});
}).catch(function(e) {
reject(e);
})
});

function getRequest(path) {
let headersObj: any = {
'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/json;charset=UTF-8',
};

if (self.requestParam.auth) {
headersObj.Authorization = self.requestParam.auth
headersObj.Authorization = self.requestParam.auth;
}

let headers = new Headers(headersObj);
var request_url = self.requestParam.url.replace(self.config.username + ':' + self.config.password + '@', '');
var request_url = self.requestParam.url.replace(
self.config.username + ':' + self.config.password + '@',
'',
);
var request_path = request_url + path + '/';
console.log(request_path);
return self.http.get(request_path, { headers: headers }).toPromise();
return self.http
.get(request_path, { headers: headers })
.toPromise();
}
}
getVersion() {
let headersObj: any = {
'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/json;charset=UTF-8',
};

if (this.requestParam.auth) {
headersObj.Authorization = this.requestParam.auth
headersObj.Authorization = this.requestParam.auth;
}

let headers = new Headers(headersObj);
var request_url = this.requestParam.url.replace(this.config.username + ':' + this.config.password + '@', '');
var request_url = this.requestParam.url.replace(
this.config.username + ':' + this.config.password + '@',
'',
);
var request_path = request_url + '/_settings?human';
console.log(request_path);
return this.http.get(request_path, { headers: headers }).toPromise()
return this.http.get(request_path, { headers: headers }).toPromise();
}
post(path: string, data: any) {
let requestData = JSON.stringify(data);
let headersObj: any = {
'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/json;charset=UTF-8',
};

if (this.requestParam.auth) {
headersObj.Authorization = this.requestParam.auth
headersObj.Authorization = this.requestParam.auth;
}

let headers = new Headers(headersObj);
return this.http.post(this.requestParam.url + path, requestData, { headers: headers }).toPromise()
return this.http
.post(this.requestParam.url + path, requestData, {
headers: headers,
})
.toPromise();
}
posturl(url: string, data: any) {
let requestData = JSON.stringify(data);
let headersObj: any = {
'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/json;charset=UTF-8',
};

if (this.requestParam.auth) {
headersObj.Authorization = this.requestParam.auth
headersObj.Authorization = this.requestParam.auth;
}

let headers = new Headers(headersObj);
return this.http.post(url, requestData, { headers: headers }).toPromise()
return this.http
.post(url, requestData, { headers: headers })
.toPromise();
}
put(path: string, data: any) {
let headersObj: any = {
'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/json;charset=UTF-8',
};

if (this.requestParam.auth) {
headersObj.Authorization = this.requestParam.auth
headersObj.Authorization = this.requestParam.auth;
}

let headers = new Headers(headersObj);
return this.http.put(this.requestParam.url + path, data, { headers: headers }).toPromise()
return this.http
.put(this.requestParam.url + path, data, { headers: headers })
.toPromise();
}
delete(path: string) {
let headersObj: any = {
'Content-Type': 'application/json;charset=UTF-8'
'Content-Type': 'application/json;charset=UTF-8',
};

if (this.requestParam.auth) {
headersObj.Authorization = this.requestParam.auth
headersObj.Authorization = this.requestParam.auth;
}

let headers = new Headers(headersObj);
return this.http.delete(this.requestParam.url + path, { headers: headers }).toPromise()
return this.http
.delete(this.requestParam.url + path, { headers: headers })
.toPromise();
}
public handleError(error: any) {
console.error('An error occurred', error);
Expand All @@ -165,28 +228,12 @@ export class AppbaseService {
}
filterurl(url: string) {
if (url) {
var parsedUrl = parse_url(url);
var obj = {
username: null,
password: null,
url: url
username: parsedUrl.username,
password: parsedUrl.password,
url: parsedUrl.href,
};
var urlsplit = url.split(':');
try {
obj.username = urlsplit[1].replace('//', '');
var httpPrefix = url.split('://');
if (urlsplit[2]) {
var pwsplit = urlsplit[2].split('@');
obj.password = pwsplit[0];
if (url.indexOf('@') !== -1) {
obj.url = httpPrefix[0] + '://' + pwsplit[1];
if (urlsplit[3]) {
obj.url += ':' + urlsplit[3];
}
}
}
} catch (e) {
console.log(e);
}
return obj;
} else {
return null;
Expand All @@ -198,7 +245,7 @@ export class AppbaseService {
}
this.resultStream = this.appbaseRef.searchStream({
type: type,
body: body
body: body,
});
return this.resultStream;
}
Expand Down

0 comments on commit f0d7d5c

Please sign in to comment.