-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdiscovery.go
44 lines (36 loc) · 1.13 KB
/
discovery.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
package gotrix
import (
"net/url"
"github.com/chanbakjsd/gotrix/api/httputil"
)
// Discover is a helper function that calls DiscoverWithClient with the default HTTP client.
func Discover(serverName string) (*Client, error) {
return DiscoverWithClient(httputil.NewClient(), serverName)
}
// DiscoverWithClient attempts to discover the homeserver using the provided server name.
//
// This allows the host address to be extracted from the user ID and used to discover the
// homeserver host in a way that is spec-compliant.
func DiscoverWithClient(httpClient httputil.Client, serverName string) (*Client, error) {
apiClient, err := NewWithClient(httpClient, serverName)
if err != nil {
return nil, err
}
info, err := apiClient.DiscoveryInfo()
if err != nil {
return nil, err
}
apiClient, err = NewWithClient(httpClient, info.HomeServer.BaseURL)
if err != nil {
return nil, err
}
identityServerURL, err := url.Parse(info.IdentityServer.BaseURL)
if err != nil {
return nil, err
}
if identityServerURL.Scheme == "" {
identityServerURL.Scheme = "https"
}
apiClient.IdentityServer = identityServerURL.Host
return apiClient, nil
}