Skip to content

An angular module that integrates thrift javascript lib with angular's http service

Notifications You must be signed in to change notification settings

massaroni/angular-thrift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

angular-thrift

An angular module that integrates thrift javascript lib with angular's http service

This is designed for sending json-formatted thrift v0.9 rpc calls to a server, from within a web browser with limited i/o functionality (no sockets, just http).

Usage Instructions

The provided ngThrift module has 3 dependencies that you have to set up: it depends on 2 user-defined modules, and angular services generated by a python script that serve as angular-injected clients for each of your thrift services, defined in your thrift IDL files.

  1. Implement the "ngThrift.http" module, with a user-defined service HttpErrorHandlerService, so that the user can inject a callback function to handle connection errors in thrift api calls. For example:
angular.module('ngThrift.http', []);
angular.module('ngThrift.http').service('HttpErrorHandlerService', ['$log', function ($log) {
    this.onConnectionError = function (status, retryCallback, noRetryCallback) {
        $log.debug('thrift http connection error');
        $log.debug(status);
        $log.debug('retrying');
        retryCallback();
    };
});
  1. Implement the "auth" module, with the AuthenticationService service, because ngThrift doesn't use http headers for authentication.
angular.module('auth', []);
angular.module('auth').service('AuthenticationService', ['$log', function ($log) {
    var accessToken;

    /**
     * security exceptions indicate that the client's credentials or access token is denied by the server
     */
    this.isSecurityException = function (ex) {
        return ex instanceof MySecurityException;
    };

    /**
     * authenticate with the server
     */
    this.reAuthenticate = function (successCallback, failCallback) {
        $log.debug('refreshing my access token, or re-logging-in...');
        // do some work
        successCallback();
    };

    /**
     * replace any authentication credentials in this array.
     * this framework doesn't use http headers for authentication, so you probably send over your access token in a thrift rpc argument.
     */
    this.updateSecurityCredentials = function (args) {
        for (var i = 0; i < args; i++) {
            var arg = args[i];
            if (arg instanceof MyAccessToken) {
                arg[i] = accessToken;
            }
        }
    };

    this.onCannotAuthenticate = function () {
        $log.warn('cant authenticate :(');
    };
});
  1. Generate angularjs thrift client services with Dave's python script, in this Twitter Commons fork: https://github.com/davearata/twitter-commons

LICENSE

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.