<commit_before>import { Promise }    from 'es6-promise';
import { HttpClient } from '../http-client';
import { Scope }      from '../string-literal';

export interface AccessToken {
    access_token : string
    token_type   : string
    expires_in   : number
    scope        : string
    created_at   : number
}

export interface AccessTokenInfo {
    resource_owner_id  : number
    scopes             : Scope[]
    expires_in_seconds : number
    application        : {uid: string}
    created_at         : number
}

export class AuthorizationService {
    constructor( private client: HttpClient ) {
    }

    authorize() {
        throw Error('Not Implemented');
    }

    token(
      client_id     : string,
      client_secret : string,
      grant_type    : string,
      redirect_uri  : string,
      code          : string ): Promise<AccessToken> {
        return this.client.post('https://api.annict.com/oauth/token', {
          client_id, client_secret, grant_type, redirect_uri, code
        })
        .then(response => response.json());
    }<commit_msg>implement /oauth/authorize redirection on browser mode<commit_after>import { Promise }    from 'es6-promise';
import { HttpClient } from '../http-client';
import { Scope }      from '../string-literal';
import * as qs        from 'qs';

declare const BROWSER : string;

export interface AccessToken {
    access_token : string
    token_type   : string
    expires_in   : number
    scope        : string
    created_at   : number
}

export interface AccessTokenInfo {
    resource_owner_id  : number
    scopes             : Scope[]
    expires_in_seconds : number
    application        : {uid: string}
    created_at         : number
}

export class AuthorizationService {
    constructor( private client: HttpClient ) {
    }

    authorize(
      client_id     : string,
      response_type : string   = 'code',
      redirect_uri  : string   = 'urn:ietf:wg:oauth:2.0:oob',
      scope         : string[] = ['read'] ) {
        if(BROWSER) {
            window.location.assign(
                'https://api.annict.com/oauth/authorize?'
                + qs.stringify({
                    client_id,
                    response_type,
                    redirect_uri,
                    scope: scope.join(' ')
                })
            );
        }
        else {
            throw Error('Not Implemented');
        }
    }

    token(
      client_id     : string,
      client_secret : string,
      grant_type    : string,
      redirect_uri  : string,
      code          : string ): Promise<AccessToken> {
        return this.client.post('https://api.annict.com/oauth/token', {
          client_id, client_secret, grant_type, redirect_uri, code
        })
        .then(response => response.json());
    }
{'fraction_non_alphanumeric': 0.10085836909871244, 'fraction_numerical': 0.001430615164520744, 'mean_word_length': 1.8835051546391752, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3089578', 'n_tokens_mistral': 799, 'n_tokens_neox': 783, 'n_words': 177}