func updateTtySize(url string) error { cols, err := term.Cols() if err != nil { return errgo.Mask(err, errgo.Any) } lines, err := term.Lines() if err != nil { return errgo.Mask(err, errgo.Any) } params := UpdateTtyParams{ fmt.Sprintf("%d", cols), fmt.Sprintf("%d", lines), } paramsJson, _ := json.Marshal(¶ms) req, err := http.NewRequest("PUT", url, bytes.NewReader(paramsJson)) if err != nil { return errgo.Mask(err, errgo.Any) } req.SetBasicAuth("", scalingo.CurrentUser.AuthToken) res, err := httpclient.Do(req) if err != nil { return errgo.Mask(err, errgo.Any) } defer res.Body.Close() if res.StatusCode != 200 { return errgo.Newf("Invalid error code from run server: %s", res.Status) } return nil }
// Execute an API request and return its response/error func (req *APIRequest) Do() (*http.Response, error) { err := req.FillDefaultValues() if err != nil { return nil, errgo.Mask(err, errgo.Any) } endpoint := req.URL + req.Endpoint // Execute the HTTP request according to the HTTP method switch req.Method { case "PATCH": fallthrough case "POST": fallthrough case "WITH_BODY": buffer, err := json.Marshal(req.Params) if err != nil { return nil, errgo.Mask(err, errgo.Any) } reader := bytes.NewReader(buffer) req.HTTPRequest, err = http.NewRequest(req.Method, endpoint, reader) if err != nil { return nil, errgo.Mask(err, errgo.Any) } case "GET", "DELETE": values, err := req.BuildQueryFromParams() if err != nil { return nil, errgo.Mask(err, errgo.Any) } endpoint = fmt.Sprintf("%s?%s", endpoint, values.Encode()) req.HTTPRequest, err = http.NewRequest(req.Method, endpoint, nil) if err != nil { return nil, errgo.Mask(err, errgo.Any) } } debug.Printf("[API] %v %v\n", req.HTTPRequest.Method, req.HTTPRequest.URL) debug.Printf(io.Indent(fmt.Sprintf("Headers: %v", req.HTTPRequest.Header), 6)) debug.Printf(io.Indent("Params : %v", 6), req.Params) req.HTTPRequest.SetBasicAuth("", req.Token) res, err := httpclient.Do(req.HTTPRequest) if err != nil { fmt.Printf("Fail to query %s: %v\n", req.HTTPRequest.Host, err) os.Exit(1) } if req.Expected.Contains(res.StatusCode) { return res, nil } return nil, NewRequestFailedError(res, req) }
func (ctx *runContext) uploadFile(endpoint string, file string) error { body := new(bytes.Buffer) name := filepath.Base(file) multipartFile := multipart.NewWriter(body) writer, err := multipartFile.CreateFormFile("file", name) if err != nil { return errgo.Mask(err, errgo.Any) } fd, err := os.OpenFile(file, os.O_RDONLY, 0600) if err != nil { return errgo.Mask(err, errgo.Any) } _, err = stdio.Copy(writer, fd) if err != nil { return errgo.Mask(err, errgo.Any) } err = fd.Close() if err != nil { return errgo.Mask(err, errgo.Any) } err = multipartFile.Close() if err != nil { return errgo.Mask(err, errgo.Any) } req, err := http.NewRequest("POST", endpoint, body) if err != nil { return errgo.Mask(err, errgo.Any) } req.SetBasicAuth("", config.AuthenticatedUser.AuthenticationToken) req.Header.Set("Content-Type", multipartFile.FormDataContentType()) fmt.Fprintln(ctx.waitingTextOutputWriter, "Upload", file, "to container.") debug.Println("Endpoint:", req.URL) res, err := httpclient.Do(req) if err != nil { return errgo.Mask(err, errgo.Any) } defer res.Body.Close() if res.StatusCode != 200 { b, _ := ioutil.ReadAll(res.Body) return errgo.Newf("Invalid return code %v (%s)", res.Status, strings.TrimSpace(string(b))) } return nil }