github.com/mika/distribution@v2.2.2-0.20160108133430-a75790e3d8e0+incompatible/registry/client/auth/api_version.go (about) 1 package auth 2 3 import ( 4 "net/http" 5 "strings" 6 ) 7 8 // APIVersion represents a version of an API including its 9 // type and version number. 10 type APIVersion struct { 11 // Type refers to the name of a specific API specification 12 // such as "registry" 13 Type string 14 15 // Version is the version of the API specification implemented, 16 // This may omit the revision number and only include 17 // the major and minor version, such as "2.0" 18 Version string 19 } 20 21 // String returns the string formatted API Version 22 func (v APIVersion) String() string { 23 return v.Type + "/" + v.Version 24 } 25 26 // APIVersions gets the API versions out of an HTTP response using the provided 27 // version header as the key for the HTTP header. 28 func APIVersions(resp *http.Response, versionHeader string) []APIVersion { 29 versions := []APIVersion{} 30 if versionHeader != "" { 31 for _, supportedVersions := range resp.Header[http.CanonicalHeaderKey(versionHeader)] { 32 for _, version := range strings.Fields(supportedVersions) { 33 versions = append(versions, ParseAPIVersion(version)) 34 } 35 } 36 } 37 return versions 38 } 39 40 // ParseAPIVersion parses an API version string into an APIVersion 41 // Format (Expected, not enforced): 42 // API version string = <API type> '/' <API version> 43 // API type = [a-z][a-z0-9]* 44 // API version = [0-9]+(\.[0-9]+)? 45 // TODO(dmcgowan): Enforce format, add error condition, remove unknown type 46 func ParseAPIVersion(versionStr string) APIVersion { 47 idx := strings.IndexRune(versionStr, '/') 48 if idx == -1 { 49 return APIVersion{ 50 Type: "unknown", 51 Version: versionStr, 52 } 53 } 54 return APIVersion{ 55 Type: strings.ToLower(versionStr[:idx]), 56 Version: versionStr[idx+1:], 57 } 58 }