1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/sdk/go/arvados"
22 type contextKey string
24 const ContextKeyCredentials contextKey = "credentials"
26 type TokenProvider func(context.Context) ([]string, error)
30 httpClient http.Client
32 tokenProvider TokenProvider
35 func NewConn(clusterID string, url *url.URL, insecure bool, tp TokenProvider) *Conn {
36 transport := http.DefaultTransport
38 // It's not safe to copy *http.DefaultTransport
39 // because it has a mutex (which might be locked)
40 // protecting a private map (which might not be nil).
41 // So we build our own, using the Go 1.12 default
42 // values, ignoring any changes the application has
43 // made to http.DefaultTransport.
44 transport = &http.Transport{
45 DialContext: (&net.Dialer{
46 Timeout: 30 * time.Second,
47 KeepAlive: 30 * time.Second,
51 IdleConnTimeout: 90 * time.Second,
52 TLSHandshakeTimeout: 10 * time.Second,
53 ExpectContinueTimeout: 1 * time.Second,
54 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
59 httpClient: http.Client{Transport: transport},
65 func (conn *Conn) requestAndDecode(ctx context.Context, dst interface{}, ep arvados.APIEndpoint, body io.Reader, opts interface{}) error {
66 aClient := arvados.Client{
67 Client: &conn.httpClient,
68 Scheme: conn.baseURL.Scheme,
69 APIHost: conn.baseURL.Host,
71 tokens, err := conn.tokenProvider(ctx)
74 } else if len(tokens) > 0 {
75 ctx = context.WithValue(ctx, "Authorization", "Bearer "+tokens[0])
77 // Use a non-empty auth string to ensure we override
78 // any default token set on aClient -- and to avoid
79 // having the remote prompt us to send a token by
81 ctx = context.WithValue(ctx, "Authorization", "Bearer -")
84 // Encode opts to JSON and decode from there to a
85 // map[string]interface{}, so we can munge the query params
86 // using the JSON key names specified by opts' struct tags.
87 j, err := json.Marshal(opts)
89 return fmt.Errorf("%T: requestAndDecode: Marshal opts: %s", conn, err)
91 var params map[string]interface{}
92 err = json.Unmarshal(j, ¶ms)
94 return fmt.Errorf("%T: requestAndDecode: Unmarshal opts: %s", conn, err)
96 if attrs, ok := params["attrs"]; ok && ep.AttrsKey != "" {
97 params[ep.AttrsKey] = attrs
98 delete(params, "attrs")
100 if limit, ok := params["limit"].(float64); ok && limit < 0 {
101 // Negative limit means "not specified" here, but some
102 // servers/versions do not accept that, so we need to
103 // remove it entirely.
104 delete(params, "limit")
107 if strings.Contains(ep.Path, "/:uuid") {
108 uuid, _ := params["uuid"].(string)
109 path = strings.Replace(path, "/:uuid", "/"+uuid, 1)
110 delete(params, "uuid")
112 return aClient.RequestAndDecodeContext(ctx, dst, ep.Method, path, body, params)
115 func (conn *Conn) CollectionCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Collection, error) {
116 ep := arvados.EndpointCollectionCreate
117 var resp arvados.Collection
118 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
122 func (conn *Conn) CollectionUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Collection, error) {
123 ep := arvados.EndpointCollectionUpdate
124 var resp arvados.Collection
125 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
129 func (conn *Conn) CollectionGet(ctx context.Context, options arvados.GetOptions) (arvados.Collection, error) {
130 ep := arvados.EndpointCollectionGet
131 var resp arvados.Collection
132 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
136 func (conn *Conn) CollectionList(ctx context.Context, options arvados.ListOptions) (arvados.CollectionList, error) {
137 ep := arvados.EndpointCollectionList
138 var resp arvados.CollectionList
139 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
143 func (conn *Conn) CollectionDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
144 ep := arvados.EndpointCollectionDelete
145 var resp arvados.Collection
146 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
150 func (conn *Conn) ContainerCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Container, error) {
151 ep := arvados.EndpointContainerCreate
152 var resp arvados.Container
153 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
157 func (conn *Conn) ContainerUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
158 ep := arvados.EndpointContainerUpdate
159 var resp arvados.Container
160 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
164 func (conn *Conn) ContainerGet(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
165 ep := arvados.EndpointContainerGet
166 var resp arvados.Container
167 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
171 func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerList, error) {
172 ep := arvados.EndpointContainerList
173 var resp arvados.ContainerList
174 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
178 func (conn *Conn) ContainerDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Container, error) {
179 ep := arvados.EndpointContainerDelete
180 var resp arvados.Container
181 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
185 func (conn *Conn) ContainerLock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
186 ep := arvados.EndpointContainerLock
187 var resp arvados.Container
188 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
192 func (conn *Conn) ContainerUnlock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
193 ep := arvados.EndpointContainerUnlock
194 var resp arvados.Container
195 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
199 func (conn *Conn) SpecimenCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Specimen, error) {
200 ep := arvados.EndpointSpecimenCreate
201 var resp arvados.Specimen
202 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
206 func (conn *Conn) SpecimenUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Specimen, error) {
207 ep := arvados.EndpointSpecimenUpdate
208 var resp arvados.Specimen
209 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
213 func (conn *Conn) SpecimenGet(ctx context.Context, options arvados.GetOptions) (arvados.Specimen, error) {
214 ep := arvados.EndpointSpecimenGet
215 var resp arvados.Specimen
216 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
220 func (conn *Conn) SpecimenList(ctx context.Context, options arvados.ListOptions) (arvados.SpecimenList, error) {
221 ep := arvados.EndpointSpecimenList
222 var resp arvados.SpecimenList
223 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
227 func (conn *Conn) SpecimenDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Specimen, error) {
228 ep := arvados.EndpointSpecimenDelete
229 var resp arvados.Specimen
230 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
234 func (conn *Conn) APIClientAuthorizationCurrent(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
235 ep := arvados.EndpointAPIClientAuthorizationCurrent
236 var resp arvados.APIClientAuthorization
237 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)