/
dial.go
45 lines (38 loc) · 1.1 KB
/
dial.go
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
// Copyright 2017 GRAIL, Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package http
import (
"net/http"
"net/url"
"sync"
"github.com/grailbio/reflow"
"github.com/grailbio/reflow/errors"
"github.com/grailbio/reflow/repository"
"github.com/grailbio/reflow/repository/client"
"github.com/grailbio/reflow/rest"
)
func init() {
repository.RegisterScheme("https", Dial)
}
// HTTPClient is the client that is used to instantiate the (REST)
// API client for remote repositories.
var HTTPClient *http.Client
var (
mu sync.Mutex
// clients caches repository clients.
clients = map[string]*client.Client{}
)
// Dial implements repository dialling for https urls.
func Dial(u *url.URL) (reflow.Repository, error) {
if u.Scheme != "https" {
return nil, errors.E("dial", u.String(), errors.NotSupported, errors.Errorf("unknown scheme %q", u.Scheme))
}
mu.Lock()
defer mu.Unlock()
key := u.String()
if clients[key] == nil {
clients[key] = &client.Client{Client: rest.NewClient(HTTPClient, u, nil)}
}
return clients[key], nil
}