当前位置: 首页>>代码示例>>Golang>>正文


Golang Context.GlobalBool方法代码示例

本文整理汇总了Golang中github.com/AudriusButkevicius/cli.Context.GlobalBool方法的典型用法代码示例。如果您正苦于以下问题:Golang Context.GlobalBool方法的具体用法?Golang Context.GlobalBool怎么用?Golang Context.GlobalBool使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/AudriusButkevicius/cli.Context的用法示例。


在下文中一共展示了Context.GlobalBool方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: getClient

func getClient(c *cli.Context) *APIClient {
	if instance != nil {
		return instance
	}
	endpoint := c.GlobalString("endpoint")
	if !strings.HasPrefix(endpoint, "http") {
		endpoint = "http://" + endpoint
	}
	httpClient := http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				InsecureSkipVerify: c.GlobalBool("insecure"),
			},
		},
	}
	client := APIClient{
		httpClient: httpClient,
		endpoint:   endpoint,
		apikey:     c.GlobalString("apikey"),
		username:   c.GlobalString("username"),
		password:   c.GlobalString("password"),
	}

	if client.apikey == "" {
		request, err := http.NewRequest("GET", client.endpoint, nil)
		die(err)
		response := client.handleRequest(request)
		client.id = response.Header.Get("X-Syncthing-ID")
		if client.id == "" {
			die("Failed to get device ID")
		}
		for _, item := range response.Cookies() {
			if item.Name == "CSRF-Token-"+client.id[:5] {
				client.csrf = item.Value
				goto csrffound
			}
		}
		die("Failed to get CSRF token")
	csrffound:
	}
	instance = &client
	return &client
}

func (client *APIClient) handleRequest(request *http.Request) *http.Response {
	if client.apikey != "" {
		request.Header.Set("X-API-Key", client.apikey)
	}
	if client.username != "" || client.password != "" {
		request.SetBasicAuth(client.username, client.password)
	}
	if client.csrf != "" {
		request.Header.Set("X-CSRF-Token-"+client.id[:5], client.csrf)
	}

	response, err := client.httpClient.Do(request)
	die(err)

	if response.StatusCode == 404 {
		die("Invalid endpoint or API call")
	} else if response.StatusCode == 401 {
		die("Invalid username or password")
	} else if response.StatusCode == 403 {
		if client.apikey == "" {
			die("Invalid CSRF token")
		}
		die("Invalid API key")
	} else if response.StatusCode != 200 {
		body := strings.TrimSpace(string(responseToBArray(response)))
		if body != "" {
			die(body)
		}
		die("Unknown HTTP status returned: " + response.Status)
	}
	return response
}

func httpGet(c *cli.Context, url string) *http.Response {
	client := getClient(c)
	request, err := http.NewRequest("GET", client.endpoint+"/rest/"+url, nil)
	die(err)
	return client.handleRequest(request)
}

func httpPost(c *cli.Context, url string, body string) *http.Response {
	client := getClient(c)
	request, err := http.NewRequest("POST", client.endpoint+"/rest/"+url, bytes.NewBufferString(body))
	die(err)
	return client.handleRequest(request)
}
开发者ID:nrm21,项目名称:syncthing,代码行数:90,代码来源:client.go


注:本文中的github.com/AudriusButkevicius/cli.Context.GlobalBool方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。