Skip to content

Instantly share code, notes, and snippets.

@fesor
Last active August 29, 2015 14:07
Show Gist options
  • Save fesor/8c27d4890f384ecdcc74 to your computer and use it in GitHub Desktop.
Save fesor/8c27d4890f384ecdcc74 to your computer and use it in GitHub Desktop.
ApiClientFactory PoC
angular.module('app', [])
.constant('server', {endpoint: 'http://test.com'})
.config(function(server, apiClientFactoryProvider) {
apiClientFactoryProvider.register({
'api': {
endpoint: server.endpoint, // will be concatinated with url
// global interceptors are bad, right? Heres a local one!
// Fully compatible with global interceptors
interceptors: ['myAuthInterceptor', 'noInternetConnectionInterceptor']
},
'twitter': {
interceptors: ['noInternetConnectionInterceptor']
}
});
apiClientProvider('facebook');
})
// registered api services can be injected via it's name + prefix
.factory('someService', function (twitterClient) {
var endpoint = 'http://api.twitter.com/v3';
return {
foo: function () {
return otherServiceApi({
method: 'GET'
url: endpoint + '/foo/'
});
},
bar: function (data) {
return otherServiceApi.post(endpoint + '/bar/', data, {
responseTransformer: angular.noop // config compatible with $http
});
}
};
})
.factory('multiApiService', function (apiClientFactory) {
var facebook = apiClientFactory.get('facebook),
twitter = apiClientFactory.get('twitter');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment