|
| 1 | +package restapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "net/http" |
| 6 | + "crypto/tls" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "strings" |
| 11 | + "bytes" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +type api_client struct { |
| 16 | + http_client *http.Client |
| 17 | + uri string |
| 18 | + insecure bool |
| 19 | + username string |
| 20 | + password string |
| 21 | + auth_header string |
| 22 | + redirects int |
| 23 | + timeout int |
| 24 | + id_attribute string |
| 25 | + copy_keys []string |
| 26 | + write_returns_object bool |
| 27 | + create_returns_object bool |
| 28 | + debug bool |
| 29 | +} |
| 30 | + |
| 31 | + |
| 32 | +// Make a new api client for RESTful calls |
| 33 | +func NewAPIClient (i_uri string, i_insecure bool, i_username string, i_password string, i_auth_header string, i_timeout int, i_id_attribute string, i_copy_keys []string, i_wro bool, i_cro bool, i_debug bool) *api_client { |
| 34 | + if i_debug { |
| 35 | + log.Printf("api_client.go: Constructing debug api_client\n") |
| 36 | + } |
| 37 | + |
| 38 | + /* Sane default */ |
| 39 | + if i_id_attribute == "" { |
| 40 | + i_id_attribute = "id" |
| 41 | + } |
| 42 | + |
| 43 | + /* Remove any trailing slashes since we will append |
| 44 | + to this URL with our own root-prefixed location */ |
| 45 | + if strings.HasSuffix(i_uri, "/") { |
| 46 | + i_uri = i_uri[:len(i_uri)-1] |
| 47 | + } |
| 48 | + |
| 49 | + /* Disable TLS verification if requested */ |
| 50 | + tr := &http.Transport{ |
| 51 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: i_insecure}, |
| 52 | + } |
| 53 | + |
| 54 | + client := api_client{ |
| 55 | + http_client: &http.Client{ |
| 56 | + Timeout: time.Second * time.Duration(i_timeout), |
| 57 | + Transport: tr, |
| 58 | + }, |
| 59 | + uri: i_uri, |
| 60 | + insecure: i_insecure, |
| 61 | + username: i_username, |
| 62 | + password: i_password, |
| 63 | + auth_header: i_auth_header, |
| 64 | + id_attribute: i_id_attribute, |
| 65 | + copy_keys: i_copy_keys, |
| 66 | + write_returns_object: i_wro, |
| 67 | + create_returns_object: i_cro, |
| 68 | + redirects: 5, |
| 69 | + debug: i_debug, |
| 70 | + } |
| 71 | + return &client |
| 72 | +} |
| 73 | + |
| 74 | +/* Helper function that handles sending/receiving and handling |
| 75 | + of HTTP data in and out. |
| 76 | + TODO: Handle redirects */ |
| 77 | +func (client *api_client) send_request (method string, path string, data string) (string, error) { |
| 78 | + full_uri := client.uri + path |
| 79 | + var req *http.Request |
| 80 | + var err error |
| 81 | + |
| 82 | + if client.debug { |
| 83 | + log.Printf("api_client.go: method='%s', path='%s', full uri (derived)='%s', data='%s'\n", method, path, full_uri, data) |
| 84 | + } |
| 85 | + |
| 86 | + buffer := bytes.NewBuffer([]byte(data)) |
| 87 | + |
| 88 | + if data == "" { |
| 89 | + req, err = http.NewRequest(method, full_uri, nil) |
| 90 | + } else { |
| 91 | + req, err = http.NewRequest(method, full_uri, buffer) |
| 92 | + |
| 93 | + if err == nil { |
| 94 | + req.Header.Set("Content-Type", "application/json") |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if err != nil { |
| 99 | + log.Fatal(err) |
| 100 | + return "", err |
| 101 | + } |
| 102 | + |
| 103 | + if client.debug { |
| 104 | + log.Printf("api_client.go: Sending HTTP request to %s...\n", req.URL) |
| 105 | + } |
| 106 | + |
| 107 | + /* Allow for tokens or other pre-created secrets */ |
| 108 | + if client.auth_header != "" { |
| 109 | + req.Header.Set("Authorization", client.auth_header) |
| 110 | + } else if client.username != "" && client.password != "" { |
| 111 | + /* ... and fall back to basic auth if configured */ |
| 112 | + req.SetBasicAuth(client.username, client.password) |
| 113 | + } |
| 114 | + |
| 115 | + if client.debug { |
| 116 | + log.Printf("api_client.go: Request headers:\n") |
| 117 | + for name, headers := range req.Header { |
| 118 | + for _, h := range headers { |
| 119 | + log.Printf("api_client.go: %v: %v", name, h) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + log.Printf("api_client.go: BODY:\n") |
| 124 | + body := "<none>" |
| 125 | + if req.Body != nil { |
| 126 | + body = string(data) |
| 127 | + } |
| 128 | + log.Printf("%s\n", body) |
| 129 | + } |
| 130 | + |
| 131 | + for num_redirects := client.redirects; num_redirects >= 0; num_redirects-- { |
| 132 | + resp, err := client.http_client.Do(req) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + //log.Printf("api_client.go: Error detected: %s\n", err) |
| 136 | + return "", err |
| 137 | + } |
| 138 | + |
| 139 | + if client.debug { |
| 140 | + log.Printf("api_client.go: Response code: %d\n", resp.StatusCode) |
| 141 | + log.Printf("api_client.go: Response headers:\n") |
| 142 | + for name, headers := range resp.Header { |
| 143 | + for _, h := range headers { |
| 144 | + log.Printf("api_client.go: %v: %v", name, h) |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + bodyBytes, err2 := ioutil.ReadAll(resp.Body) |
| 150 | + resp.Body.Close() |
| 151 | + |
| 152 | + if err2 != nil { return "", err2 } |
| 153 | + body := string(bodyBytes) |
| 154 | + |
| 155 | + if resp.StatusCode == 301 || resp.StatusCode == 302 { |
| 156 | + //Redirecting... decrement num_redirects and proceed to the next loop |
| 157 | + //uri = URI.parse(rsp['Location']) |
| 158 | + } else if resp.StatusCode == 404 || resp.StatusCode < 200 || resp.StatusCode >= 303 { |
| 159 | + return "", errors.New(fmt.Sprintf("Unexpected response code '%d': %s", resp.StatusCode, body)) |
| 160 | + } else { |
| 161 | + if client.debug { log.Printf("api_client.go: BODY:\n%s\n", body) } |
| 162 | + return body, nil |
| 163 | + } |
| 164 | + |
| 165 | + } //End loop through redirect attempts |
| 166 | + |
| 167 | + return "", errors.New("Error - too many redirects!") |
| 168 | +} |
0 commit comments