org.go 1.1 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7
package application

import (
	"fmt"
	"net/http"
	"net/url"
	"time"
Y
Your Name 已提交
8 9

	"github.com/eolinker/goku-api-gateway/utils"
E
eoLinker API Management 已提交
10 11
)

Y
Your Name 已提交
12
//Org org
E
eoLinker API Management 已提交
13 14 15 16
type Org struct {
	server string
}

Y
Your Name 已提交
17
//Send 请求发送,忽略重试
Y
Your Name 已提交
18
func (app *Org) Send(proto string, method string, path string, querys url.Values, header http.Header, body []byte, timeout time.Duration, retry int) (*http.Response, string, []string, error) {
E
eoLinker API Management 已提交
19

Y
Your Name 已提交
20 21
	var response *http.Response
	var err error
E
eoLinker API Management 已提交
22 23

	FinalTargetServer := ""
Y
Your Name 已提交
24
	RetryTargetServers := make([]string, 0, retry+1)
E
eoLinker API Management 已提交
25

Y
Your Name 已提交
26
	path = utils.TrimPrefixAll(path, "/")
E
eoLinker API Management 已提交
27

Y
Your Name 已提交
28
	for doTrice := retry + 1; doTrice > 0; doTrice-- {
E
eoLinker API Management 已提交
29

Y
Your Name 已提交
30
		u := fmt.Sprintf("%s://%s/%s", proto, app.server, path)
E
eoLinker API Management 已提交
31
		FinalTargetServer = app.server
Y
Your Name 已提交
32 33 34 35 36 37
		RetryTargetServers = append(RetryTargetServers, FinalTargetServer)
		response, err = request(method, u, querys, header, body, timeout)
		if err != nil {
			continue
		} else {
			return response, FinalTargetServer, RetryTargetServers, err
E
eoLinker API Management 已提交
38 39 40
		}
	}

Y
Your Name 已提交
41
	return response, FinalTargetServer, RetryTargetServers, err
E
eoLinker API Management 已提交
42 43
}

Y
Your Name 已提交
44
//NewOrg 创建新的IHttpApplication
Y
Your Name 已提交
45
func NewOrg(server string) IHttpApplication {
E
eoLinker API Management 已提交
46
	return &Org{
Y
Your Name 已提交
47
		server: server,
E
eoLinker API Management 已提交
48
	}
Y
Your Name 已提交
49
}