forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathup.go
More file actions
61 lines (56 loc) · 1.33 KB
/
Copy pathup.go
File metadata and controls
61 lines (56 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package gateway
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"github.com/railwayapp/cli/entity"
)
func constructReq(ctx context.Context, req *entity.UpRequest) (*http.Request, error) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("file", req.ProjectID)
if err != nil {
return nil, err
}
part.Write(req.Data.Bytes())
err = writer.Close()
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/project/%s/up", GetHost(), req.ProjectID)
httpReq, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", writer.FormDataContentType())
return httpReq, nil
}
func (g *Gateway) Up(ctx context.Context, req *entity.UpRequest) (*entity.UpResponse, error) {
httpReq, err := constructReq(ctx, req)
if err != nil {
return nil, err
}
g.authorize(ctx, httpReq.Header)
client := &http.Client{}
resp, err := client.Do(httpReq)
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode >= 400 {
return nil, errors.New(string(bodyBytes))
}
var res entity.UpResponse
if err := json.Unmarshal(bodyBytes, &res); err != nil {
return nil, err
}
return &res, nil
}