-
Notifications
You must be signed in to change notification settings - Fork 3
/
request_builder.go
51 lines (43 loc) · 1.02 KB
/
request_builder.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
package vertexai
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)
const (
predictURLTemplate = "https://us-central1-aiplatform.googleapis.com/v1/projects/%v/locations/us-central1/publishers/google/models/%v" // projectID, endpointID
)
type requestBuilder interface {
build(
ctx context.Context,
method string,
projectID string,
endpointID string,
urlSuffix string,
request any,
) (*http.Request, error)
}
type httpRequestBuilder struct{}
func newRequestBuilder() requestBuilder {
return &httpRequestBuilder{}
}
func (b *httpRequestBuilder) build(
ctx context.Context,
method string,
projectID string,
endpointID string,
urlSuffix string,
request any,
) (*http.Request, error) {
url := fmt.Sprintf(predictURLTemplate, projectID, endpointID) + urlSuffix
if request == nil {
return http.NewRequestWithContext(ctx, method, url, nil)
}
reqBytes, err := json.Marshal(request)
if err != nil {
return nil, err
}
return http.NewRequestWithContext(ctx, method, url, bytes.NewBuffer(reqBytes))
}