mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-19 17:50:00 +00:00
41 lines
780 B
Go
41 lines
780 B
Go
|
package httpclient
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/hashicorp/terraform/version"
|
||
|
)
|
||
|
|
||
|
const userAgentFormat = "Terraform/%s"
|
||
|
const uaEnvVar = "TF_APPEND_USER_AGENT"
|
||
|
|
||
|
func UserAgentString() string {
|
||
|
ua := fmt.Sprintf(userAgentFormat, version.Version)
|
||
|
|
||
|
if add := os.Getenv(uaEnvVar); add != "" {
|
||
|
add = strings.TrimSpace(add)
|
||
|
if len(add) > 0 {
|
||
|
ua += " " + add
|
||
|
log.Printf("[DEBUG] Using modified User-Agent: %s", ua)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ua
|
||
|
}
|
||
|
|
||
|
type userAgentRoundTripper struct {
|
||
|
inner http.RoundTripper
|
||
|
userAgent string
|
||
|
}
|
||
|
|
||
|
func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
||
|
if _, ok := req.Header["User-Agent"]; !ok {
|
||
|
req.Header.Set("User-Agent", rt.userAgent)
|
||
|
}
|
||
|
return rt.inner.RoundTrip(req)
|
||
|
}
|