This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 604
Expand file tree
/
Copy pathremoteapi.go
More file actions
128 lines (105 loc) · 2.76 KB
/
remoteapi.go
File metadata and controls
128 lines (105 loc) · 2.76 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package remoteapi
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"koding/api"
"koding/remoteapi/client"
"koding/remoteapi/models"
runtime "github.com/go-openapi/runtime/client"
"github.com/go-openapi/strfmt"
"github.com/koding/kite"
)
// Unmarshal unmarshals resp.Data into v.
//
// If resp.Error is non-nil, non-nil error is returned instead.
func Unmarshal(resp *models.DefaultResponse, v interface{}) error {
if resp.Error != nil {
if err, ok := resp.Error.(map[string]interface{}); ok {
msg, _ := err["message"].(string)
typ, _ := err["name"].(string)
if msg != "" && typ != "" {
return &kite.Error{
Type: typ,
Message: msg,
}
}
}
return fmt.Errorf("%v", resp.Error)
}
if v == nil {
return nil
}
p, err := jsonMarshal(resp.Data)
if err != nil {
return err
}
return json.Unmarshal(p, v)
}
//go:generate swagger generate client -f ../../../../website/swagger.json
// Client is a REST client used to interact with remote.api.
//
// It creates new client object per client, so it requires
// authorisation (obtaining jSession.clientId) happens
// externally.
//
// TODO(rjeczalik): Add admin mode for remote.api for it's possible
// for kloud to use one persistent session to interact with
// remote.api on behalf of the users. See discussion #9667.
type Client struct {
// Endpoint is URL of remote.api endpoint.
//
// If nil, http://127.0.0.1/remote.api is going to be used.
Endpoint *url.URL
// Client is an underlying HTTP client used for
// communication with remote.api server.
//
// If nil, http.DefaultClient is used instead.
Client *http.Client
// Transport is a caching transport that authorizes each
// request with clientID.
Transport *api.Transport
}
func (c *Client) New(user *api.User) *client.Koding {
httpClient := &http.Client{
Transport: c.Transport.NewSingleUser(user),
Jar: http.DefaultClient.Jar,
Timeout: http.DefaultClient.Timeout,
}
if c.Client != nil {
httpClient.Jar = c.Client.Jar
httpClient.Timeout = c.Client.Timeout
}
return client.New(newRuntime(c.endpoint(), httpClient), strfmt.Default)
}
func (c *Client) Timeout() time.Duration {
if c.Client != nil && c.Client.Timeout != 0 {
return c.Client.Timeout
}
return 30 * time.Second
}
func (c *Client) endpoint() *url.URL {
if c.Endpoint != nil {
return c.Endpoint
}
return &url.URL{
Scheme: "http",
Host: "127.0.0.1",
Path: "/",
}
}
func newRuntime(u *url.URL, c *http.Client) *runtime.Runtime {
return runtime.NewWithClient(u.Host, u.Path, []string{u.Scheme}, c)
}
func jsonMarshal(v interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buf.Bytes(), nil
}