Skip to content

Commit

Permalink
Fix getting right URL from settings and catch wrong API url status wh…
Browse files Browse the repository at this point in the history
…en logging in.
  • Loading branch information
Tom Offringa committed Jul 3, 2017
1 parent a5ae042 commit ce9eaf3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
23 changes: 6 additions & 17 deletions src/js/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,16 @@ class Api {
constructor(app) {
this.app = app
this.OK_STATUS = [200, 201, 202, 204]
this.NOTOK_STATUS = [401, 403]
this.NOTOK_STATUS = [401, 403, 'Network Error']
this.UNAUTHORIZED_STATUS = [401]
this.setupClient(this.app.store.get('username'), this.app.store.get('password'))
}


getPlatformUrl() {
let platformUrl = this.app.store.get('platformUrl')
if (platformUrl && platformUrl.length && platformUrl.lastIndexOf('/') !== platformUrl.length - 1) {
// Force trailing slash.
platformUrl = platformUrl + '/'
} else {
// Set a default platform url when it's not set.
platformUrl = 'https://partner.voipgrid.nl/'
this.app.store.set('platformUrl', platformUrl)
}
return platformUrl
}


/**
* Set a http client with or without basic authentication.
*/
setupClient(username, password) {
let clientOptions = {baseURL: this.getPlatformUrl()}
let clientOptions = {baseURL: this.app.store.get('platformUrl')}
if (username && password) {
this.app.logger.info(`${this}Set api client with basic auth for user ${username}`)
clientOptions.auth = {
Expand All @@ -51,6 +36,10 @@ class Api {
this.client.interceptors.response.use(function(response) {
return response
}, (err) => {
// Catch Network Errors.
if(err.message == 'Network Error') {
return Promise.resolve({'status': 'Network Error'})
}
// Reject all status codes from 500.
if (err.response.status >= 500) {
return Promise.reject(err)
Expand Down
23 changes: 22 additions & 1 deletion src/js/lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ class App extends Skeleton {
_init() {
this.settings = {
analyticsId: 'UA-60726618-9',
platformUrl: 'https://partner.voipgrid.nl/',
realm: 'websocket.voipgrid.nl',
platformUrl: this.getPlatformUrl(),
c2d: 'true',
}
if (this.env.extension) {
Expand All @@ -82,6 +82,27 @@ class App extends Skeleton {
}


/**
* Get platform URL from storage or set default.
*/
getPlatformUrl() {
let platformUrl = this.store.get('platformUrl')

if(!platformUrl) {
// Set a default platform url when it's not set.
platformUrl = 'https://partner.voipgrid.nl/'
this.store.set('platformUrl', platformUrl)
}

// Force trailing slash.
if (platformUrl && platformUrl.length && platformUrl.lastIndexOf('/') !== platformUrl.length - 1) {
platformUrl = platformUrl + '/'
}

return platformUrl
}


/**
* Reload all modules that have this method implemented.
*/
Expand Down

0 comments on commit ce9eaf3

Please sign in to comment.