Created
May 14, 2013 03:06
-
-
Save airtonix/5573372 to your computer and use it in GitHub Desktop.
Tasty Pie Model Service. Handles apikey authentication (storing the key in html5 local storage)
This file contains 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
/* | |
Angular Tasty Pie Service | |
------------------------- | |
Requires | |
-------- | |
- https://github.com/grevory/angular-local-storage | |
Features | |
-------- | |
- supports urlpatterns | |
Usage | |
----- | |
// simple pk urlconf | |
app.factory("modelThing", function($tastyPieHttpModule) { return $tastyPieHttpModule(api('thing/:id')); }); | |
// additional endpoints | |
// login & logout | |
app.factory("modelUser", function($tastyPieHttpModule, $q, $log, $http) { | |
var modelUser = $tastyPieHttpModule(api('user/:id')); | |
modelUser.login = function(params){ | |
var path = this.constructUrl(params, api('user/login')); | |
var success = function(response){ | |
if(!response.data) return $q.reject(response); | |
if("success" in response.data && !response.data.success){ | |
$log.info('Login Failed: ', response); | |
modelUser.wipeCredentials(); | |
return $q.reject(response); | |
}else if('apikey' in response.data){ | |
$log.info("Login Successful", response) | |
modelUser.setCredentials(response.data.username, response.data.apikey); | |
return new modelUser(response.data); | |
} | |
}; | |
var failed = function(response){ | |
var status = response.status; | |
$log.error('Response status: ', status, response); | |
return $q.reject(response); | |
}; | |
var request = $http({ | |
method: 'POST', | |
headers: this.getHeaders(), | |
url: path, | |
data: params.data | |
}); | |
return request.then(success, failed); | |
}; | |
modelUser.logout = function(params){ | |
if(this.) | |
var params = params?params:{} | |
params.path = this.constructUrl(params, api('user/logout')); | |
modelUser.get(params). | |
then(function(response){ | |
modelUser.wipeCredentials(); | |
}); | |
}; | |
// modelUser.hasFeatures = function(params){ | |
// var output = false; | |
// var index = 0; | |
// var code; | |
// while(output!==true){ | |
// code = arguments[index] | |
// output = data.features.all.indexOf(code)>0 | |
// index++; | |
// if(index > arguments.length) break; | |
// } | |
// return output; | |
// }; | |
return modelUser; | |
}); | |
Todo | |
---- | |
- configurable authentication method. valid options would be : | |
basic_auth, api_key, token | |
14/05/13 - api-key auth supported. | |
*/ | |
(function () { | |
'use strict'; | |
var debug = function(){ | |
if (window.debugMode) { | |
console.log(arguments); | |
} | |
}; | |
angular.module('tastyPieHttpModule', ['LocalStorageModule']). | |
factory('$tastyPieHttpModule', function($http, $localStorageService) { | |
return function(urlconf){ | |
var ApiResource = function(data){ | |
angular.extend(this, data); | |
}; | |
ApiResource.getHeaders = function(){ | |
var headers = {} | |
headers['Content-Type']='application/json'; | |
var credentials = ApiResource.getCredentials(); | |
if(credentials) headers['Authorization']=credentials; | |
return headers; | |
}; | |
ApiResource.setCredentials = function(username, apikey){ | |
$localStorageService.add('AuthorizationHeader', 'ApiKey '+username+':'+apikey) | |
}; | |
ApiResource.getCredentials = function(){ | |
return $localStorageService.get('AuthorizationHeader'); | |
}; | |
ApiResource.wipeCredentials = function(){ | |
return $localStorageService.remove('AuthorizationHeader'); | |
}; | |
ApiResource.constructUrl = function(params, urlpattern){ | |
if(params && 'path' in params){ | |
return params.path; | |
} | |
var output = urlpattern; | |
var value = ""; | |
if (typeof(params) == 'object'){ | |
for(var key in params){ | |
if(params.hasOwnProperty(key)){ | |
value = params[key]; | |
}else{ | |
value=""; | |
} | |
output = output.replace(":"+key, value); | |
} | |
} | |
return output; | |
}; | |
ApiResource.get = function(params){ | |
var resource = this; | |
var path = resource.constructUrl(params, urlconf); | |
return $http({ | |
method: "GET", | |
url: path, | |
headers: resource.getHeaders() | |
}).then( | |
function(response){ return new ApiResource(response.data);}, | |
function(reason){ debug("Error retreiving resource"); throw reason; } | |
); | |
}; | |
ApiResource.remove = function(params){ | |
var resource = this; | |
var path = resource.constructUrl(params, urlconf); | |
return $http({ | |
method: "DELETE", | |
url: path, | |
headers: resource.getHeaders() | |
}); | |
}; | |
ApiResource.create = function(params){ | |
var resource = this; | |
var path = resource.constructUrl(params, urlconf); | |
return $http({ | |
method: 'POST', | |
url: path, | |
data: resource, | |
headers: resource.getHeaders() | |
}).then(function(response){ | |
return $http({ | |
method: "GET", | |
url: response.headers('location'), | |
headers: resource.getHeaders() | |
}).then(function(response){ | |
return new ApiResource(response.data); | |
}); | |
}, function(reason){ debug("Error creating resource"); throw reason; }); | |
}; | |
return ApiResource; | |
}; | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment