-
Notifications
You must be signed in to change notification settings - Fork 28
/
version.go
111 lines (94 loc) · 2.73 KB
/
version.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
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
// Copyright 2024 Deepgram SDK contributors. All Rights Reserved.
// Use of this source code is governed by a MIT license that can be found in the LICENSE file.
// SPDX-License-Identifier: MIT
package version
import (
"context"
"fmt"
"net/url"
"regexp"
"strings"
"github.com/gorilla/schema"
klog "k8s.io/klog/v2"
interfaces "github.com/deepgram/deepgram-go-sdk/pkg/client/interfaces"
common "github.com/deepgram/deepgram-go-sdk/pkg/common"
)
// getAPIURL constructs the URL for API requests and handles versioning, path resolution,
// host overrides, and query string parameters.
func getAPIURL(ctx context.Context, apiType, host, version, path string, options interface{}, args ...interface{}) (string, error) {
// set the path
if path == "" {
path = APIPathMap[apiType]
}
// set the protocol
protocol := HTTPProtocol
if apiType == APITypeLive {
protocol = WSProtocol
}
if host == "" {
host = common.DefaultHost
}
// check if the host has a protocol
r := regexp.MustCompile(`^(https?)://(.+)$`)
if apiType == APITypeLive {
r = regexp.MustCompile(`^(wss?)://(.+)$`)
}
match := r.MatchString(host)
klog.V(3).Infof("host decompose... match: %t\n", match)
if match {
matches := r.FindStringSubmatch(host)
for _, match := range matches {
klog.V(5).Infof("match: %s\n", match)
}
protocol = matches[1]
host = matches[2]
if slash := strings.Index(host, "/"); slash > 0 {
host = host[:slash]
}
}
klog.V(3).Infof("protocol: %s\n", protocol)
klog.V(3).Infof("host: %s\n", host)
// make sure the version is set
if version == "" {
version = DefaultAPIVersion
}
// remove the version from the path if it exists
r = regexp.MustCompile(`^(v\d+|%%s)/`)
match = r.MatchString(path)
klog.V(3).Infof("path decompose - match: %t\n", match)
if match {
path = r.ReplaceAllString(path, "")
}
// Construct the query string
// using an options struct
var q url.Values
if options != nil {
q = url.Values{}
encoder := schema.NewEncoder()
if err := encoder.Encode(options, q); err != nil {
return "", err
}
}
// using custom parameters
if parameters, ok := ctx.Value(interfaces.ParametersContext{}).(map[string][]string); ok {
for k, vs := range parameters {
for _, v := range vs {
if q == nil {
q = url.Values{}
}
q.Add(k, v)
}
}
}
// construct the full path and substitute the version and all query parameters
fullpath := fmt.Sprintf("%%s/%s", path)
completeFullpath := fmt.Sprintf(fullpath, append([]interface{}{version}, args...)...)
// construct the URL
var u url.URL
if q != nil {
u = url.URL{Scheme: protocol, Host: host, Path: completeFullpath, RawQuery: q.Encode()}
} else {
u = url.URL{Scheme: protocol, Host: host, Path: completeFullpath}
}
return u.String(), nil
}