github.com/m3db/m3@v1.5.0/src/cmd/tools/m3ctl/client/http.go (about) 1 // Copyright (c) 2020 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package client 22 23 import ( 24 "io" 25 "io/ioutil" 26 "net/http" 27 "time" 28 29 "go.uber.org/zap" 30 ) 31 32 const timeout = time.Duration(5 * time.Second) 33 34 // DoGet is the low level call to the backend api for gets. 35 func DoGet( 36 url string, 37 headers map[string]string, 38 l *zap.Logger, 39 ) ([]byte, error) { 40 l.Info("request", zap.String("method", "get"), zap.String("url", url)) 41 client := http.Client{ 42 Timeout: timeout, 43 } 44 req, err := http.NewRequest(http.MethodGet, url, nil) 45 if err != nil { 46 return nil, err 47 } 48 49 setHeadersWithDefaults(req, headers) 50 resp, err := client.Do(req) 51 if err != nil { 52 return nil, err 53 } 54 defer func() { 55 ioutil.ReadAll(resp.Body) 56 resp.Body.Close() 57 }() 58 if err := checkForAndHandleError(url, resp, l); err != nil { 59 return nil, err 60 } 61 return ioutil.ReadAll(resp.Body) 62 } 63 64 // DoPost is the low level call to the backend api for posts. 65 func DoPost( 66 url string, 67 headers map[string]string, 68 data io.Reader, 69 l *zap.Logger, 70 ) ([]byte, error) { 71 l.Info("request", zap.String("method", "post"), zap.String("url", url)) 72 client := &http.Client{ 73 Timeout: timeout, 74 } 75 req, err := http.NewRequest(http.MethodPost, url, data) 76 if err != nil { 77 return nil, err 78 } 79 80 setHeadersWithDefaults(req, headers) 81 resp, err := client.Do(req) 82 if err != nil { 83 return nil, err 84 } 85 defer func() { 86 ioutil.ReadAll(resp.Body) 87 resp.Body.Close() 88 }() 89 if err := checkForAndHandleError(url, resp, l); err != nil { 90 return nil, err 91 } 92 return ioutil.ReadAll(resp.Body) 93 } 94 95 // DoDelete is the low level call to the backend api for deletes. 96 func DoDelete( 97 url string, 98 headers map[string]string, 99 l *zap.Logger, 100 ) ([]byte, error) { 101 l.Info("request", zap.String("method", "delete"), zap.String("url", url)) 102 client := &http.Client{ 103 Timeout: timeout, 104 } 105 req, err := http.NewRequest(http.MethodDelete, url, nil) 106 if err != nil { 107 return nil, err 108 } 109 110 setHeadersWithDefaults(req, headers) 111 resp, err := client.Do(req) 112 if err != nil { 113 return nil, err 114 } 115 defer func() { 116 ioutil.ReadAll(resp.Body) 117 resp.Body.Close() 118 }() 119 if err := checkForAndHandleError(url, resp, l); err != nil { 120 return nil, err 121 } 122 return ioutil.ReadAll(resp.Body) 123 } 124 125 func setHeadersWithDefaults(req *http.Request, headers map[string]string) { 126 req.Header.Set("Content-Type", "application/json") 127 for k, v := range headers { 128 req.Header.Set(k, v) 129 } 130 }