package http_api

import (
    "crypto/tls"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net"
    "net/http"
    "net/url"
    "strconv"
    "strings"
    "time"
)

type deadlinedConn struct {
    Timeout time.Duration
    net.Conn
}

func (c *deadlinedConn) Read(b []byte) (n int, err error) {
    return c.Conn.Read(b)
}

func (c *deadlinedConn) Write(b []byte) (n int, err error) {
    return c.Conn.Write(b)
}

// A custom http.Transport with support for deadline timeouts
func NewDeadlineTransport(connectTimeout time.Duration, requestTimeout time.Duration) *http.Transport {
    transport := &http.Transport{
        Dial: func(netw, addr string) (net.Conn, error) {
            c, err := net.DialTimeout(netw, addr, connectTimeout)
            if err != nil {
                return nil, err
            }
            return &deadlinedConn{connectTimeout, c}, nil
        },
        ResponseHeaderTimeout: requestTimeout,
    }
    return transport
}

type Client struct {
    c *http.Client
}

func NewClient(tlsConfig *tls.Config, connectTimeout time.Duration, requestTimeout time.Duration) *Client {
    transport := NewDeadlineTransport(connectTimeout, requestTimeout)
    transport.TLSClientConfig = tlsConfig
    return &Client{
        c: &http.Client{
            Transport: transport,
            Timeout:   requestTimeout,
        },
    }
}

// NegotiateV1 is a helper function to perform a v1 HTTP request
// and fallback to parsing the old backwards-compatible response format
// storing the result in the value pointed to by v.
//
// TODO: deprecated, remove in 1.0 (replace calls with GETV1)
func (c *Client) NegotiateV1(endpoint string, v interface{}) error {
retry:
    req, err := http.NewRequest("GET", endpoint, nil)
    if err != nil {
        return err
    }

    req.Header.Add("Accept", "application/vnd.nsq; version=1.0")

    resp, err := c.c.Do(req)
    if err != nil {
        return err
    }

    body, err := ioutil.ReadAll(resp.Body)
    resp.Body.Close()
    if err != nil {
        return err
    }
    if resp.StatusCode != 200 {
        if resp.StatusCode == 403 && !strings.HasPrefix(endpoint, "https") {
            endpoint, err = httpsEndpoint(endpoint, body)
            if err != nil {
                return err
            }
            goto retry
        }
        return fmt.Errorf("got response %s %q", resp.Status, body)
    }

    if len(body) == 0 {
        body = []byte("{}")
    }

    // unwrap pre-1.0 api response
    if resp.Header.Get("X-NSQ-Content-Type") != "nsq; version=1.0" {
        var u struct {
            StatusCode int64           `json:"status_code"`
            Data       json.RawMessage `json:"data"`
        }
        err := json.Unmarshal(body, u)
        if err != nil {
            return err
        }
        if u.StatusCode != 200 {
            return fmt.Errorf("got 200 response, but api status code of %d", u.StatusCode)
        }
        body = u.Data
    }

    return json.Unmarshal(body, v)
}

// GETV1 is a helper function to perform a V1 HTTP request
// and parse our NSQ daemon's expected response format, with deadlines.
func (c *Client) GETV1(endpoint string, v interface{}) error {
retry:
    req, err := http.NewRequest("GET", endpoint, nil)
    if err != nil {
        return err
    }

    req.Header.Add("Accept", "application/vnd.nsq; version=1.0")

    resp, err := c.c.Do(req)
    if err != nil {
        return err
    }

    body, err := ioutil.ReadAll(resp.Body)
    resp.Body.Close()
    if err != nil {
        return err
    }
    if resp.StatusCode != 200 {
        if resp.StatusCode == 403 && !strings.HasPrefix(endpoint, "https") {
            endpoint, err = httpsEndpoint(endpoint, body)
            if err != nil {
                return err
            }
            goto retry
        }
        return fmt.Errorf("got response %s %q", resp.Status, body)
    }
    err = json.Unmarshal(body, &v)
    if err != nil {
        return err
    }

    return nil
}

// PostV1 is a helper function to perform a V1 HTTP request
// and parse our NSQ daemon's expected response format, with deadlines.
func (c *Client) POSTV1(endpoint string) error {
retry:
    req, err := http.NewRequest("POST", endpoint, nil)
    if err != nil {
        return err
    }

    req.Header.Add("Accept", "application/vnd.nsq; version=1.0")

    resp, err := c.c.Do(req)
    if err != nil {
        return err
    }

    body, err := ioutil.ReadAll(resp.Body)
    resp.Body.Close()
    if err != nil {
        return err
    }
    if resp.StatusCode != 200 {
        if resp.StatusCode == 403 && !strings.HasPrefix(endpoint, "https") {
            endpoint, err = httpsEndpoint(endpoint, body)
            if err != nil {
                return err
            }
            goto retry
        }
        return fmt.Errorf("got response %s %q", resp.Status, body)
    }

    return nil
}

func httpsEndpoint(endpoint string, body []byte) (string, error) {
    var forbiddenResp struct {
        HTTPSPort int `json:"https_port"`
    }
    err := json.Unmarshal(body, &forbiddenResp)
    if err != nil {
        return "", err
    }

    u, err := url.Parse(endpoint)
    if err != nil {
        return "", err
    }

    host, _, err := net.SplitHostPort(u.Host)
    if err != nil {
        return "", err
    }

    u.Scheme = "https"
    u.Host = net.JoinHostPort(host, strconv.Itoa(forbiddenResp.HTTPSPort))
    return u.String(), nil
}

api_request.go的更多相关文章

  1. 储物柜soket通信协议和中间件实现技术细节

    一.中间件程序的职责: 1)对柜机提供soket长连接的服务器端,就是soket server.可提供上万的客户端同时连接.用来实时响应控制请求,中间件必须随时知道某个柜机的在线状态,外部请求时才能判 ...

  2. 突破短板,传统桌面程序 使用webapi 扩展迎合web和移动端融合的需求

    传统桌面程序不能完全被web和移动端替代,但是需要改造.这里要说的是巧用webapi把以前用dll和com组件,ocx等方式做接口,做分布式开发的方式,改成restful 风格api的方式实现跨平台, ...

  3. PHP API 框架开发的学习

    基于互联网的应用正变得越来越普及,在这个过程中,有更多的站点将自身的资源开放给开发者来调用.对外提供的API 调用使得站点之间的内容关联性更强,同时这些开放的平台也为用户.开发者和中小网站带来了更大的 ...

  4. jQuery里面ajax请求的封装

    为了避免ajax漫天飞,我们需要对jQuery的代码进行封装,封装代码: function api_request(name, params, cb, scope, async, el) { if ( ...

  5. Python 基于request库的get,post,delete,封装

    # coding=utf-8 import json import requests class TestApi(object): """ /* @param: @ses ...

  6. 听说PHP的生成器yield处理大量数据杠杠的

    官方解释yield yield生成器是php5.5之后出现的,官方文档这样解释:yield提供了一种更容易的方法来实现简单的迭代对象,相比较定义类实现 Iterator 接口的方式,性能开销和复杂性大 ...

  7. 如何使用PHP的生成器yield处理大量数据业务

    官方解释yield yield生成器是php5.5之后出现的,官方文档这样解释:yield提供了一种更容易的方法来实现简单的迭代对象,相比较定义类实现 Iterator 接口的方式,性能开销和复杂性大 ...

  8. 20191011-构建我们公司自己的自动化接口测试框架-testrun最重要的模块

    testrun模块呢就是最终自动化测试入口,调用前面封装的各个模块主要流程是: 1. 获取测试集种待执行的测试用例 2. 处理测试用例获取的数据,包括转换数据格式,处理数据的中的关联等 3. 处理完数 ...

  9. 20191011-构建我们公司自己的自动化接口测试框架-Util的getTestSuite模块

    getTestSuite主要是用于在testData里面获取测试集以及对应的测试数据,包括2个主要的方法,一个是获取测试集,一个是获取测试集里面要执行的测试用例 获取测试集方法: from Util. ...

随机推荐

  1. C# 设置Word文档保护(加密、解密、权限设置)

    对于一些重要的word文档,出于防止资料被他人查看,或者防止文档被修改的目的,我们在选择文档保护时可以选择文档打开添加密码或者设置文档操作权限等,在下面的文章中将介绍如何使用类库Free Spire. ...

  2. ethtool确定网卡对应的物理网口

    在配置有多个网络接口的设备时我们会犯难,eth0.eth1.--到底是那个接口? 我使用的机器是CentOS系统,打开终端,输入ethtool –help 显示帮助信息,下面我就简要介绍一下最常用的两 ...

  3. 你不能错过.net 并发解决方案

    BlockingCollection集合是一个拥有阻塞功能的集合,它就是完成了经典生产者消费者的算法功能.所以BlockingCollection 很适合构造流水线模式的并发方案 BlockingCo ...

  4. DB2常用命令2

    1.启动实例(db2inst1):实例相当于informix中的服务 db2start 2.停止实例(db2inst1): db2stop 3.列出所有实例(db2inst1) db2ilist 4. ...

  5. 人手一份核武器 - Hacking Team 泄露(开源)资料导览手册

    https://zhuanlan.zhihu.com/p/20102713 author:蒸米 0x00 序 事先声明本人并不是全栈安全工程师,仅仅是移动安全小菜一枚,所以对泄漏资料的分析难免会有疏忽 ...

  6. 6 Tools To Jump Start Your Video Content Marketing

    http://www.forbes.com/sites/drewhendricks/2014/10/16/6-tools-to-jump-start-your-video-content-market ...

  7. HTML5学习系列之表单与文件

    article元素 article元素代表文档.页面或应用程序中独立的.完整的.可以独自被外部引用的内容.它可以是一篇博客或报刊中的文章.一篇论坛帖子.一段用户评论或独立的插件,或者其他任何独立的内容 ...

  8. Spark---架构原理

    Spark核心组件 1.Driver 我们编写的Spark程序就在Driver上 Spark集群节点之一,就是你提交的Spark程序的机器 2.Master Master是个进程 Master其实主要 ...

  9. removeElement

    Description: Given an array and a value, remove all instances of that value in place and return the ...

  10. 对JavaScript事件机制的一点理解

    JavaScript通过事件机制实现了异步操作,这种异步操作可以使CPU可以在IO任务的等待中被释放出来处理其他任务,等待IO结束再去处理这个任务.这个是一个基本的事件机制. 那么是不是说事件从监听到 ...