Last active
August 29, 2015 14:05
-
-
Save jkrems/1ffe063e998dd349ac87 to your computer and use it in GitHub Desktop.
gofer-blog
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
var entityId = 'xy'; | |
var loadEntity = cached.deferred(_.partial(myService.fetch, '/entity/' + entityId)); | |
cached('myService').getOrElse(entityId, loadEntity, function(err, entity) { | |
// 80% of time this works every time | |
}); |
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
// will be logged with serviceName=myService, endpointName=cats, methodName=index | |
myService.cats.index(cb); | |
// will be logged with serviceName=myService, endpointName=cats, methodName=put | |
myService.cats.save('kitty', { fur: 'fluffy' }, cb); |
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
var goferFor = require('gofer'); | |
var MyService = goferFor('myService'); // serviceName = myService | |
MyService.registerEndpoints({ | |
// endpointName = cats | |
cats: function(request) { | |
return { | |
index: function(cb) { | |
return request({ uri: '/cats', methodName: 'index' }, cb); | |
}, | |
show: function(id, cb) { | |
return request({ uri: '/cats/' + id, methodName: 'show' }, cb); | |
}, | |
save: function(id, data, cb) { | |
return request({ uri: '/cats/' + id, json: data }, cb); | |
} | |
}; | |
} | |
}); |
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
globalDefaults: | |
connectTimeout: 100 | |
timeout: 2000 | |
myApi: | |
baseUrl: "https://my-api" | |
qs: | |
client_id: "SOME_CLIENT_ID" | |
github: | |
baseUrl: "https://api.github.com/v3" | |
clientId: "XXX" | |
clientSecret: "XYZ" |
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
var http = require('http'); | |
var Hub = require('gofer/hub'); | |
var MyService = require('my-service-gofer'); | |
var OtherService = require('other-service-gofer'); | |
var hub = new Hub(); | |
hub.on('success', function() {}); | |
hub.on('socketQueueing', function() {}); | |
// ... | |
http.createServer(function(req, res) { | |
var requestId = req.headers['x-request-id'] || generateUUID(); | |
var config = { globalDefaults: { requestId: requestId } }; | |
var myService = new MyService(config, hub); | |
var otherService = new OtherService(config, hub); | |
myService.fetch('/some-url').pipe(res); | |
}).listen(process.env.PORT || 3000); |
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
myApi.fetch({ uri: '/cats' }); |
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
request({ | |
connectTimeout: 100, // from globalDefaults | |
timeout: 2000, | |
baseUrl: 'https://my-api', // from service defaults | |
qs: { client_id: 'SOME_CLIENT_ID' }, | |
uri: '/cats' // explicit options | |
}); |
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
function addOAuth2Token(options) { | |
if (options.accessToken) { | |
options.headers = options.headers || {}; | |
options.headers.authorization = 'token ' + options.accessToken; | |
delete options.accessToken; | |
} | |
return options; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment