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


Golang ProviderClient.Request方法代码示例

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


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

示例1: ChooseVersion

// ChooseVersion queries the base endpoint of an API to choose the most recent non-experimental alternative from a service's
// published versions.
// It returns the highest-Priority Version among the alternatives that are provided, as well as its corresponding endpoint.
func ChooseVersion(client *gophercloud.ProviderClient, recognized []*Version) (*Version, string, error) {
	type linkResp struct {
		Href string `json:"href"`
		Rel  string `json:"rel"`
	}

	type valueResp struct {
		ID     string     `json:"id"`
		Status string     `json:"status"`
		Links  []linkResp `json:"links"`
	}

	type versionsResp struct {
		Values []valueResp `json:"values"`
	}

	type response struct {
		Versions versionsResp `json:"versions"`
	}

	normalize := func(endpoint string) string {
		if !strings.HasSuffix(endpoint, "/") {
			return endpoint + "/"
		}
		return endpoint
	}
	identityEndpoint := normalize(client.IdentityEndpoint)

	// If a full endpoint is specified, check version suffixes for a match first.
	for _, v := range recognized {
		if strings.HasSuffix(identityEndpoint, v.Suffix) {
			return v, identityEndpoint, nil
		}
	}

	var resp response
	_, err := client.Request("GET", client.IdentityBase, gophercloud.RequestOpts{
		JSONResponse: &resp,
		OkCodes:      []int{200, 300},
	})

	if err != nil {
		return nil, "", err
	}

	byID := make(map[string]*Version)
	for _, version := range recognized {
		byID[version.ID] = version
	}

	var highest *Version
	var endpoint string

	for _, value := range resp.Versions.Values {
		href := ""
		for _, link := range value.Links {
			if link.Rel == "self" {
				href = normalize(link.Href)
			}
		}

		if matching, ok := byID[value.ID]; ok {
			// Prefer a version that exactly matches the provided endpoint.
			if href == identityEndpoint {
				if href == "" {
					return nil, "", fmt.Errorf("Endpoint missing in version %s response from %s", value.ID, client.IdentityBase)
				}
				return matching, href, nil
			}

			// Otherwise, find the highest-priority version with a whitelisted status.
			if goodStatus[strings.ToLower(value.Status)] {
				if highest == nil || matching.Priority > highest.Priority {
					highest = matching
					endpoint = href
				}
			}
		}
	}

	if highest == nil {
		return nil, "", fmt.Errorf("No supported version available from endpoint %s", client.IdentityBase)
	}
	if endpoint == "" {
		return nil, "", fmt.Errorf("Endpoint missing in version %s response from %s", highest.ID, client.IdentityBase)
	}

	return highest, endpoint, nil
}
开发者ID:hdansou,项目名称:rack,代码行数:92,代码来源:choose_version.go


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