Skip to content

Commit 9065114

Browse files
authored
feat: new http client
1 parent bf2bacc commit 9065114

File tree

18 files changed

+792
-1328
lines changed

18 files changed

+792
-1328
lines changed

.env.local

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ACS_ENDPOINT_URL=
2+
ACS_KEY=

acs.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,20 @@ import (
1111

1212
// Client is the client for the ACS API.
1313
type Client struct {
14-
c *client.Client
1514
SMS *sms.Service
1615
Call *calls.Service
1716
Identity *identities.Service
1817
}
1918

2019
// New creates a new Client.
2120
func New(endpointURL, key string, c *http.Client) *Client {
22-
base := client.New(endpointURL, key, c)
21+
base := client.New().
22+
Client(c).
23+
Base(endpointURL).
24+
QueryStruct(client.DefaultVersion).
25+
SignProvider(client.NewHMacSigner(key))
2326

2427
return &Client{
25-
c: base,
2628
SMS: sms.NewService(base),
2729
Identity: identities.NewService(base),
2830
Call: calls.NewService(base),

calls/calls.go

+18-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package calls
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/go-resty/resty/v2"
78
"github.com/zeiss/go-acs/client"
@@ -130,14 +131,24 @@ const (
130131
TranscriptionTransportTypeWebsocket TranscriptionTransportType = "websocket"
131132
)
132133

133-
// WithAuthToken sets the auth token.
134-
func WithAuthToken(token string) Opt {
135-
return func(r *resty.Request) {
136-
r.SetAuthToken(token)
134+
// CreateCall creates a call.
135+
func (s *Service) CreateCall(ctx context.Context, key string, body *CreateCallRequest) error {
136+
res, err := s.client.New().Post("/calling/callConnections").BodyJSON(body).ReceiveSuccess(ctx, nil)
137+
if err != nil {
138+
return err
137139
}
140+
141+
fmt.Println(res)
142+
143+
return nil
138144
}
139145

140-
// CreateCall creates a call.
141-
func (s *Service) CreateCall(ctx context.Context, key string, req *CreateCallRequest, opts ...client.Opt) error {
142-
return s.client.Post(ctx, key, "/calling/callConnections", "api-version=2024-06-15-preview", req, nil, opts...)
146+
// CallHangUp is the method for recognizing call.
147+
func (s *Service) CallHangUp(ctx context.Context, id string) error {
148+
_, err := s.client.New().Delete(fmt.Sprintf("/calling/callConnections/%s", id)).ReceiveSuccess(ctx, nil)
149+
if err != nil {
150+
return err
151+
}
152+
153+
return nil
143154
}

calls/events.go

-50
This file was deleted.

calls/hangup.go

-13
This file was deleted.

calls/play.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package calls
33
import (
44
"context"
55
"fmt"
6-
7-
"github.com/zeiss/go-acs/client"
86
)
97

108
// CallMediaPlayRequest is the body for playing media.
@@ -83,6 +81,11 @@ const (
8381
)
8482

8583
// CallMediaPlay is the call media play.
86-
func (s *Service) CallMediaPlay(ctx context.Context, id string, key string, req *CallMediaPlayRequest, opts ...client.Opt) error {
87-
return s.client.Post(ctx, key, fmt.Sprintf("/calling/callConnections/%s:play", id), "api-version=2024-06-15-preview", req, nil, opts...)
84+
func (s *Service) CallMediaPlay(ctx context.Context, id string, key string, body *CallMediaPlayRequest) error {
85+
_, err := s.client.New().Post(fmt.Sprintf("/calling/callConnections/%s:play", id)).BodyJSON(body).ReceiveSuccess(ctx, nil)
86+
if err != nil {
87+
return err
88+
}
89+
90+
return nil
8891
}

calls/regonize.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package calls
33
import (
44
"context"
55
"fmt"
6-
7-
"github.com/zeiss/go-acs/client"
86
)
97

108
// CallRecognizeRequest is the body for recognizing call.
@@ -117,6 +115,11 @@ type SpeechOptions struct {
117115
}
118116

119117
// CallMediaRecognize is used to recognize the call.
120-
func (s *Service) CallMediaRecognize(ctx context.Context, id string, key string, req *CallRecognizeRequest, opts ...client.Opt) error {
121-
return s.client.Post(ctx, key, fmt.Sprintf("/calling/callConnections/%s:recognize", id), "api-version=2024-06-15-preview", req, nil, opts...)
118+
func (s *Service) CallMediaRecognize(ctx context.Context, id string, key string, body *CallRecognizeRequest) error {
119+
_, err := s.client.New().Post(fmt.Sprintf("/calling/callConnections/%s:recognize", id)).BodyJSON(body).ReceiveSuccess(ctx, nil)
120+
if err != nil {
121+
return err
122+
}
123+
124+
return nil
122125
}

client/body.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package client
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"strings"
8+
9+
goquery "github.com/google/go-querystring/query"
10+
)
11+
12+
// BodyProvider provides Body content for http.Request attachment.
13+
type BodyProvider interface {
14+
// ContentType returns the Content-Type of the body.
15+
ContentType() string
16+
// Body returns the io.Reader body.
17+
Body() (io.Reader, error)
18+
}
19+
20+
// bodyProvider provides the wrapped body value as a Body for reqests.
21+
type bodyProvider struct {
22+
body io.Reader
23+
}
24+
25+
func (p bodyProvider) ContentType() string {
26+
return ""
27+
}
28+
29+
func (p bodyProvider) Body() (io.Reader, error) {
30+
return p.body, nil
31+
}
32+
33+
// jsonBodyProvider encodes a JSON tagged struct value as a Body for requests.
34+
// See https://golang.org/pkg/encoding/json/#MarshalIndent for details.
35+
type jsonBodyProvider struct {
36+
payload interface{}
37+
}
38+
39+
func (p jsonBodyProvider) ContentType() string {
40+
return jsonContentType
41+
}
42+
43+
func (p jsonBodyProvider) Body() (io.Reader, error) {
44+
buf := &bytes.Buffer{}
45+
err := json.NewEncoder(buf).Encode(p.payload)
46+
if err != nil {
47+
return nil, err
48+
}
49+
return buf, nil
50+
}
51+
52+
// formBodyProvider encodes a url tagged struct value as Body for requests.
53+
// See https://godoc.org/github.com/google/go-querystring/query for details.
54+
type formBodyProvider struct {
55+
payload interface{}
56+
}
57+
58+
func (p formBodyProvider) ContentType() string {
59+
return formContentType
60+
}
61+
62+
func (p formBodyProvider) Body() (io.Reader, error) {
63+
values, err := goquery.Values(p.payload)
64+
if err != nil {
65+
return nil, err
66+
}
67+
return strings.NewReader(values.Encode()), nil
68+
}

0 commit comments

Comments
 (0)