@@ -40,7 +40,8 @@ import (
40
40
// Client provides interface to send requests to the connector service.
41
41
type Client interface {
42
42
Post (ctx context.Context , url url.URL , activity schema.Activity ) error
43
- Delete (ctx context.Context , url url.URL , activity schema.Activity ) error
43
+ Delete (ctx context.Context , url url.URL ) error
44
+ Get (ctx context.Context , url url.URL ) (json.RawMessage , error )
44
45
Put (ctx context.Context , url url.URL , activity schema.Activity ) error
45
46
}
46
47
@@ -81,19 +82,48 @@ func (client *ConnectorClient) Post(ctx context.Context, target url.URL, activit
81
82
if err != nil {
82
83
return err
83
84
}
84
- return client .sendRequest (req , activity )
85
+ return client .sendRequestWithRespErrCheck (req )
86
+ }
87
+
88
+ // Get a resource from given URL using authenticated request.
89
+ //
90
+ // This method is helpful for obtaining Teams context for your bot.
91
+ // Read more: https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/get-teams-context?tabs=json
92
+ func (client * ConnectorClient ) Get (ctx context.Context , target url.URL ) (json.RawMessage , error ) {
93
+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , target .String (), nil )
94
+ if err != nil {
95
+ return nil , err
96
+ }
97
+
98
+ res , err := client .sendRequest (req )
99
+ if err != nil {
100
+ return nil , newHTTPError (err )
101
+ }
102
+ defer res .Body .Close ()
103
+
104
+ if wrappedErr := client .checkRespError (res ); wrappedErr != nil {
105
+ return nil , wrappedErr
106
+ }
107
+
108
+ var rawOutput json.RawMessage
109
+ err = json .NewDecoder (res .Body ).Decode (& rawOutput )
110
+ if err != nil {
111
+ return nil , err
112
+ }
113
+
114
+ return rawOutput , nil
85
115
}
86
116
87
117
// Delete an activity.
88
118
//
89
119
// Creates a HTTP DELETE request with the provided activity ID and a Bearer token in the header.
90
120
// Returns any error as received from the call to connector service.
91
- func (client * ConnectorClient ) Delete (ctx context.Context , target url.URL , activity schema. Activity ) error {
121
+ func (client * ConnectorClient ) Delete (ctx context.Context , target url.URL ) error {
92
122
req , err := http .NewRequestWithContext (ctx , http .MethodDelete , target .String (), nil )
93
123
if err != nil {
94
124
return err
95
125
}
96
- return client .sendRequest (req , activity )
126
+ return client .sendRequestWithRespErrCheck (req )
97
127
}
98
128
99
129
// Put an activity.
@@ -109,40 +139,41 @@ func (client *ConnectorClient) Put(ctx context.Context, target url.URL, activity
109
139
if err != nil {
110
140
return err
111
141
}
112
- return client .sendRequest (req , activity )
142
+ return client .sendRequestWithRespErrCheck (req )
113
143
}
114
144
115
- func (client * ConnectorClient ) sendRequest (req * http.Request , activity schema.Activity ) error {
145
+ func (client * ConnectorClient ) sendRequestWithRespErrCheck (req * http.Request ) error {
146
+ res , err := client .sendRequest (req )
147
+ if err != nil {
148
+ return newHTTPError (err )
149
+ }
150
+
151
+ defer res .Body .Close ()
152
+ return client .checkRespError (res )
153
+ }
154
+
155
+ func (client * ConnectorClient ) sendRequest (req * http.Request ) (* http.Response , error ) {
116
156
token , err := client .getToken (req .Context ())
117
157
if err != nil {
118
- return err
158
+ return nil , err
119
159
}
120
160
121
161
req .Header .Set ("Content-Type" , "application/json" )
122
162
req .Header .Set ("Authorization" , "Bearer " + token )
123
163
124
- return client .checkRespError ( client . ReplyClient .Do (req ) )
164
+ return client .ReplyClient .Do (req )
125
165
}
126
166
127
- func (client * ConnectorClient ) checkRespError (resp * http.Response , err error ) error {
167
+ func (client * ConnectorClient ) checkRespError (resp * http.Response ) error {
128
168
allowedResp := []int {http .StatusOK , http .StatusCreated , http .StatusAccepted }
129
- if err != nil {
130
- return customerror.HTTPError {
131
- HtErr : err ,
132
- }
133
- }
134
- defer resp .Body .Close ()
135
169
// Check if resp allowed
136
170
for _ , code := range allowedResp {
137
171
if code == resp .StatusCode {
138
172
return nil
139
173
}
140
174
}
141
175
142
- return customerror.HTTPError {
143
- HtErr : errors .New ("invalid response" ),
144
- StatusCode : resp .StatusCode ,
145
- }
176
+ return newHTTPErrorWithStatusCode (errors .New ("invalid response" ), resp .StatusCode )
146
177
}
147
178
148
179
func (client * ConnectorClient ) getToken (ctx context.Context ) (string , error ) {
@@ -174,10 +205,7 @@ func (client *ConnectorClient) getToken(ctx context.Context) (string, error) {
174
205
175
206
resp , err := client .AuthClient .Do (r )
176
207
if err != nil {
177
- return "" , customerror.HTTPError {
178
- StatusCode : resp .StatusCode ,
179
- HtErr : err ,
180
- }
208
+ return "" , newHTTPErrorWithStatusCode (err , resp .StatusCode )
181
209
}
182
210
183
211
defer resp .Body .Close ()
@@ -196,3 +224,16 @@ func (client *ConnectorClient) getToken(ctx context.Context) (string, error) {
196
224
197
225
return client .AuthCache .Keys .(string ), nil
198
226
}
227
+
228
+ func newHTTPError (err error ) error {
229
+ return customerror.HTTPError {
230
+ HtErr : err ,
231
+ }
232
+ }
233
+
234
+ func newHTTPErrorWithStatusCode (err error , statusCode int ) error {
235
+ return customerror.HTTPError {
236
+ HtErr : err ,
237
+ StatusCode : statusCode ,
238
+ }
239
+ }
0 commit comments