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
52 lines (46 loc) · 1.17 KB
/
Copy pathup.go
File metadata and controls
52 lines (46 loc) · 1.17 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
package gateway
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/railwayapp/cli/entity"
)
func constructReq(ctx context.Context, req *entity.UpRequest) (*http.Request, error) {
url := fmt.Sprintf("%s/project/%s/environment/%s/up?serviceId=%s", GetHost(), req.ProjectID, req.EnvironmentID, req.ServiceID)
httpReq, err := http.NewRequestWithContext(ctx, "POST", url, &req.Data)
if err != nil {
return nil, err
}
httpReq.Header.Set("Content-Type", "multipart/form-data")
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
}
err = g.authorize(httpReq.Header)
if err != nil {
return nil, err
}
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
}