|
| 1 | +package rest |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "net/http/cookiejar" |
| 10 | +) |
| 11 | + |
| 12 | +type PgRestHandle struct { |
| 13 | + Ip_or_host string |
| 14 | + Port int |
| 15 | + User string |
| 16 | + Password string |
| 17 | + |
| 18 | + client_cookies *cookiejar.Jar |
| 19 | + httpClient *http.Client |
| 20 | +} |
| 21 | + |
| 22 | +func CreatePGRestClient(rest_ip string, rest_Port int, Username string, Password string) *PgRestHandle { |
| 23 | + |
| 24 | + cookies, _ := cookiejar.New(nil) |
| 25 | + |
| 26 | + return &PgRestHandle{ |
| 27 | + Ip_or_host: rest_ip, |
| 28 | + Port: rest_Port, |
| 29 | + User: Username, |
| 30 | + Password: Password, |
| 31 | + |
| 32 | + client_cookies: cookies, |
| 33 | + httpClient: &http.Client{ |
| 34 | + Transport: &http.Transport{ |
| 35 | + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
| 36 | + }, |
| 37 | + Jar: cookies, |
| 38 | + }, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func RestGet(handle *PgRestHandle, path string) (error, int, []byte) { |
| 43 | + |
| 44 | + req, _ := http.NewRequest("GET", path, nil) |
| 45 | + |
| 46 | + req.Header.Set("Accept", "application/json") |
| 47 | + |
| 48 | + res, err := handle.httpClient.Do(req) |
| 49 | + if err != nil { |
| 50 | + return fmt.Errorf("Failed to login on PG plat: %v", err), 0, nil |
| 51 | + } |
| 52 | + |
| 53 | + // Read body data |
| 54 | + body_data, err := ioutil.ReadAll(res.Body) |
| 55 | + defer res.Body.Close() |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + return fmt.Errorf("Error reading body data %v", err), 0, nil |
| 59 | + } |
| 60 | + |
| 61 | + return nil, res.StatusCode, body_data |
| 62 | +} |
| 63 | + |
| 64 | +func RestPost(handle *PgRestHandle, path string, data string) (error, int, []byte) { |
| 65 | + |
| 66 | + req, _ := http.NewRequest("POST", path, bytes.NewBufferString(data)) |
| 67 | + |
| 68 | + req.Header.Set("Content-Type", "application/json") |
| 69 | + req.Header.Set("Accept", "application/json") |
| 70 | + |
| 71 | + res, err := handle.httpClient.Do(req) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("Faild to POST: %v", err), 0, nil |
| 74 | + } |
| 75 | + |
| 76 | + // Read body data |
| 77 | + body_data, err := ioutil.ReadAll(res.Body) |
| 78 | + defer res.Body.Close() |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("Error reading body data %v", err), 0, nil |
| 82 | + } |
| 83 | + |
| 84 | + return nil, res.StatusCode, body_data |
| 85 | +} |
| 86 | + |
| 87 | +func RestPut(handle *PgRestHandle, path string, data string) (error, int, []byte) { |
| 88 | + |
| 89 | + req, _ := http.NewRequest("PUT", path, bytes.NewBufferString(data)) |
| 90 | + |
| 91 | + req.Header.Set("Content-Type", "application/json") |
| 92 | + req.Header.Set("Accept", "application/json") |
| 93 | + |
| 94 | + res, err := handle.httpClient.Do(req) |
| 95 | + if err != nil { |
| 96 | + return fmt.Errorf("Faild to PUT: %v", err), 0, nil |
| 97 | + } |
| 98 | + |
| 99 | + // Read body data |
| 100 | + body_data, err := ioutil.ReadAll(res.Body) |
| 101 | + defer res.Body.Close() |
| 102 | + |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("Error reading body data %v", err), 0, nil |
| 105 | + } |
| 106 | + |
| 107 | + return nil, res.StatusCode, body_data |
| 108 | +} |
| 109 | + |
| 110 | +func RestDelete(handle *PgRestHandle, path string) (error, int, []byte) { |
| 111 | + |
| 112 | + req, _ := http.NewRequest("DELETE", path, nil) |
| 113 | + |
| 114 | + req.Header.Set("Accept", "application/json") |
| 115 | + |
| 116 | + res, err := handle.httpClient.Do(req) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("Failed to DELETE: %v", err), 0, nil |
| 119 | + } |
| 120 | + |
| 121 | + // Read body data |
| 122 | + body_data, err := ioutil.ReadAll(res.Body) |
| 123 | + defer res.Body.Close() |
| 124 | + |
| 125 | + if err != nil { |
| 126 | + return fmt.Errorf("Error reading body data %v", err), 0, nil |
| 127 | + } |
| 128 | + |
| 129 | + return nil, res.StatusCode, body_data |
| 130 | +} |
0 commit comments