| 1 | package js |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "io/ioutil" |
| 8 | "net/http" |
| 9 | "net/url" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/robertkrimen/otto" |
| 13 | ) |
| 14 | |
| 15 | type httpPackage struct { |
| 16 | } |
| 17 | |
| 18 | type httpResponse struct { |
| 19 | Error error |
| 20 | Response *http.Response |
| 21 | Raw []byte |
| 22 | Body string |
| 23 | JSON interface{} |
| 24 | } |
| 25 | |
| 26 | func (c httpPackage) Encode(s string) string { |
| 27 | return url.QueryEscape(s) |
| 28 | } |
| 29 | |
| 30 | func (c httpPackage) Request(method string, uri string, |
| 31 | headers map[string]string, |
| 32 | form map[string]string, |
| 33 | json string) httpResponse { |
| 34 | var reader io.Reader |
| 35 | |
| 36 | if form != nil { |
| 37 | data := url.Values{} |
| 38 | for k, v := range form { |
| 39 | data.Set(k, v) |
| 40 | } |
| 41 | reader = bytes.NewBufferString(data.Encode()) |
| 42 | } else if json != "" { |
| 43 | reader = strings.NewReader(json) |
| 44 | } |
| 45 | |
| 46 | req, err := http.NewRequest(method, uri, reader) |
| 47 | if err != nil { |
| 48 | return httpResponse{Error: err} |
| 49 | } |
| 50 | |
| 51 | if form != nil { |
| 52 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 53 | } else if json != "" { |
| 54 | req.Header.Set("Content-Type", "application/json") |
| 55 | } |
| 56 | |
| 57 | for name, value := range headers { |
| 58 | req.Header.Add(name, value) |
| 59 | } |
| 60 | |
| 61 | resp, err := http.DefaultClient.Do(req) |
| 62 | if err != nil { |
| 63 | return httpResponse{Error: err} |
| 64 | } |
| 65 | defer resp.Body.Close() |
| 66 | |
| 67 | raw, err := ioutil.ReadAll(resp.Body) |
| 68 | if err != nil { |
| 69 | return httpResponse{Error: err} |
| 70 | } |
| 71 | |
| 72 | res := httpResponse{ |
| 73 | Response: resp, |
| 74 | Raw: raw, |
| 75 | Body: string(raw), |
| 76 | } |
| 77 | |
| 78 | if resp.StatusCode != http.StatusOK { |
| 79 | res.Error = fmt.Errorf("%s", resp.Status) |
| 80 | } |
| 81 | |
| 82 | return res |
| 83 | } |
| 84 | |
| 85 | func (c httpPackage) Get(url string, headers map[string]string) httpResponse { |
| 86 | return c.Request("GET", url, headers, nil, "") |
| 87 | } |
| 88 | |
| 89 | func (c httpPackage) PostForm(url string, headers map[string]string, form map[string]string) httpResponse { |
| 90 | return c.Request("POST", url, headers, form, "") |
| 91 | } |
| 92 | |
| 93 | func (c httpPackage) PostJSON(url string, headers map[string]string, json string) httpResponse { |
| 94 | return c.Request("POST", url, headers, nil, json) |
| 95 | } |
| 96 | |
| 97 | func httpRequest(call otto.FunctionCall) otto.Value { |
| 98 | argv := call.ArgumentList |
| 99 | argc := len(argv) |
| 100 | if argc < 2 { |
| 101 | return ReportError("httpRequest: expected 2 or more, %d given instead.", argc) |
| 102 | } |
| 103 | |
| 104 | method := argv[0].String() |
| 105 | url := argv[1].String() |
| 106 | |
| 107 | client := &http.Client{} |
| 108 | req, err := http.NewRequest(method, url, nil) |
| 109 | if argc >= 3 { |
| 110 | data := argv[2].String() |
| 111 | req, err = http.NewRequest(method, url, bytes.NewBuffer([]byte(data))) |
| 112 | if err != nil { |
| 113 | return ReportError("Could create request to url %s: %s", url, err) |
| 114 | } |
| 115 | |
| 116 | if argc > 3 { |
| 117 | headers := argv[3].Object() |
| 118 | for _, key := range headers.Keys() { |
| 119 | v, err := headers.Get(key) |
| 120 | if err != nil { |
| 121 | return ReportError("Could add header %s to request: %s", key, err) |
| 122 | } |
| 123 | req.Header.Add(key, v.String()) |
| 124 | } |
| 125 | } |
| 126 | } else if err != nil { |
| 127 | return ReportError("Could create request to url %s: %s", url, err) |
| 128 | } |
| 129 | |
| 130 | resp, err := client.Do(req) |
| 131 | if err != nil { |
| 132 | return ReportError("Could not request url %s: %s", url, err) |
| 133 | } |
| 134 | defer resp.Body.Close() |
| 135 | |
| 136 | body, err := ioutil.ReadAll(resp.Body) |
| 137 | if err != nil { |
| 138 | return ReportError("Could not read response: %s", err) |
| 139 | } |
| 140 | |
| 141 | object, err := otto.New().Object("({})") |
| 142 | if err != nil { |
| 143 | return ReportError("Could not create response object: %s", err) |
| 144 | } |
| 145 | |
| 146 | err = object.Set("body", string(body)) |
| 147 | if err != nil { |
| 148 | return ReportError("Could not populate response object: %s", err) |
| 149 | } |
| 150 | |
| 151 | v, err := otto.ToValue(object) |
| 152 | if err != nil { |
| 153 | return ReportError("Could not convert to object: %s", err) |
| 154 | } |
| 155 | return v |
| 156 | } |