service.go raw

   1  package api
   2  
   3  import (
   4  	"net/http"
   5  	"regexp"
   6  )
   7  
   8  type service struct {
   9  	core *Core
  10  }
  11  
  12  // getLink get a rel into the Link header.
  13  func getLink(header http.Header, rel string) string {
  14  	links := getLinks(header, rel)
  15  	if len(links) < 1 {
  16  		return ""
  17  	}
  18  
  19  	return links[0]
  20  }
  21  
  22  func getLinks(header http.Header, rel string) []string {
  23  	linkExpr := regexp.MustCompile(`<(.+?)>(?:;[^;]+)*?;\s*rel="(.+?)"`)
  24  
  25  	var links []string
  26  
  27  	for _, link := range header["Link"] {
  28  		for _, m := range linkExpr.FindAllStringSubmatch(link, -1) {
  29  			if len(m) != 3 {
  30  				continue
  31  			}
  32  
  33  			if m[2] == rel {
  34  				links = append(links, m[1])
  35  			}
  36  		}
  37  	}
  38  
  39  	return links
  40  }
  41  
  42  // getLocation get the value of the header Location.
  43  func getLocation(resp *http.Response) string {
  44  	if resp == nil {
  45  		return ""
  46  	}
  47  
  48  	return resp.Header.Get("Location")
  49  }
  50  
  51  // getRetryAfter get the value of the header Retry-After.
  52  func getRetryAfter(resp *http.Response) string {
  53  	if resp == nil {
  54  		return ""
  55  	}
  56  
  57  	return resp.Header.Get("Retry-After")
  58  }
  59