/**
 * @author xiangshouding
 * @file protocol/http.ts
 */

import {Map} from '../types/lang';
import {Address, Protocol} from './protocol';
import * as request from 'request';

type Method = 'GET' | 'POST' | 'DELETE' | 'OPTION';

export class Http implements Protocol {
    method: Method = 'GET';
    addr: Address;
    options: Map<string>;
    timeout: number = 2000; // 2s
    path: string;

    constructor(addr: Address, options?: Map<any>) {
        this.options = options || {
            method: 'GET'
        };
        
        if (!this.options['url'] && !this.options['uri']) {
            let path = this.options['path'] || '';
            path = (path.indexOf('/') === 0 ? '' : '/') + path;
            this.options['url'] = `http://${addr.getAddress()}${path}`;
        }
    }

    createConnection() {}

    request(cb: (err: any, service: any, conn: any) => void) {
        return request(this.options, cb);
    }
}