Last active
June 4, 2019 14:28
-
-
Save claustres/8e96d446a3b94c167f63244eb1e9baa0 to your computer and use it in GitHub Desktop.
Kano/Weacast feathers client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import config from 'config' | |
import feathers from '@feathersjs/client' | |
import reactive from 'feathers-reactive' | |
... | |
// Initialize a Feathers client with given options | |
async setupClient (options) { | |
let client = feathers() | |
// Initialize whatever you need based on your client config | |
if (options.websocket) client.configure(feathers.socketio(...)) | |
if (options.reactive) client.configure(reactive(...)) | |
// ... | |
return client | |
} | |
// Initialize the main application client to access internal services | |
async setupKano () { | |
// Initialize Feathers client | |
this.api = this.setupClient(config.kano) | |
try { | |
// Authenticate user | |
const response = await this.api.authenticate() | |
const payload = await this.api.passport.verifyJWT(response.accessToken) | |
this.user = await this.api.service('users').get(payload.userId) | |
} catch (error) { | |
logger.error('Cannot initialize Kano API', error) | |
} | |
} | |
// Initialize the weacast application client to access external services | |
async setupWeacast () { | |
// Initialize Feathers client | |
this.weacastApi = this.setupClient(config.weacast) | |
// Ensure we also logout from weacast on app logout | |
this.api.on('logout', () => this.weacastApi.logout()) | |
try { | |
// Transfer app token to Weacast as we use shared tokens | |
const accessToken = await this.api.passport.getJWT() | |
const weacastAccessToken = await this.weacastApi.passport.getJWT() | |
if (weacastAccessToken) await this.weacastApi.authenticate() | |
else await this.weacastApi.authenticate({ strategy: 'jwt', accessToken }) | |
} catch (error) { | |
logger.error('Cannot initialize Weacast API', error) | |
} | |
} | |
async main () { | |
// Initialize all the clients we need | |
await this.setupKano() | |
await this.setupWeacast() | |
// Now we can access Weacast API whenever required | |
const response = await this.weacastApi.service('forecasts').find() | |
this.forecastModels = response.data | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment