Skip to content

Commit

Permalink
Support GET method on Client
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Nov 30, 2023
1 parent ae31623 commit b6c1fd0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
68 changes: 53 additions & 15 deletions connector/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ import (
// Client provides interface to send requests to the connector service.
type Client interface {
Post(ctx context.Context, url url.URL, activity schema.Activity) error
Delete(ctx context.Context, url url.URL, activity schema.Activity) error
Delete(ctx context.Context, url url.URL) error
Get(ctx context.Context, url url.URL) (json.RawMessage, error)
Put(ctx context.Context, url url.URL, activity schema.Activity) error
}

Expand Down Expand Up @@ -81,19 +82,50 @@ func (client *ConnectorClient) Post(ctx context.Context, target url.URL, activit
if err != nil {
return err
}
return client.sendRequest(req, activity)
return client.sendRequestWithRespErrCheck(req)
}

// Get a resource from given URL using authenticated request.
//
// This method is helpful for obtaining Teams context for your bot.
// Read more: https://learn.microsoft.com/en-us/microsoftteams/platform/bots/how-to/get-teams-context?tabs=json
func (client *ConnectorClient) Get(ctx context.Context, target url.URL) (json.RawMessage, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, target.String(), nil)
if err != nil {
return nil, err
}

res, err := client.sendRequest(req)
if err != nil {
return nil, customerror.HTTPError{
HtErr: err,
}
}
defer res.Body.Close()

if wrappedErr := client.checkRespError(res); wrappedErr != nil {
return nil, wrappedErr
}

var rawOutput json.RawMessage
err = json.NewDecoder(res.Body).Decode(&rawOutput)
if err != nil {
return nil, err
}

return rawOutput, nil
}

// Delete an activity.
//
// Creates a HTTP DELETE request with the provided activity ID and a Bearer token in the header.
// Returns any error as received from the call to connector service.
func (client *ConnectorClient) Delete(ctx context.Context, target url.URL, activity schema.Activity) error {
func (client *ConnectorClient) Delete(ctx context.Context, target url.URL) error {
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, target.String(), nil)
if err != nil {
return err
}
return client.sendRequest(req, activity)
return client.sendRequestWithRespErrCheck(req)
}

// Put an activity.
Expand All @@ -109,29 +141,35 @@ func (client *ConnectorClient) Put(ctx context.Context, target url.URL, activity
if err != nil {
return err
}
return client.sendRequest(req, activity)
return client.sendRequestWithRespErrCheck(req)
}

func (client *ConnectorClient) sendRequest(req *http.Request, activity schema.Activity) error {
func (client *ConnectorClient) sendRequestWithRespErrCheck(req *http.Request) error {
res, err := client.sendRequest(req)
if err != nil {
return customerror.HTTPError{
HtErr: err,
}
}

defer res.Body.Close()
return client.checkRespError(res)
}

func (client *ConnectorClient) sendRequest(req *http.Request) (*http.Response, error) {
token, err := client.getToken(req.Context())
if err != nil {
return err
return nil, err
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+token)

return client.checkRespError(client.ReplyClient.Do(req))
return client.ReplyClient.Do(req)
}

func (client *ConnectorClient) checkRespError(resp *http.Response, err error) error {
func (client *ConnectorClient) checkRespError(resp *http.Response) error {
allowedResp := []int{http.StatusOK, http.StatusCreated, http.StatusAccepted}
if err != nil {
return customerror.HTTPError{
HtErr: err,
}
}
defer resp.Body.Close()
// Check if resp allowed
for _, code := range allowedResp {
if code == resp.StatusCode {
Expand Down
2 changes: 1 addition & 1 deletion core/activity/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (response *DefaultResponse) DeleteActivity(ctx context.Context, activity sc

// Send activity to client
u.Path = path.Join(u.Path, respPath)
err = response.Client.Delete(ctx, *u, activity)
err = response.Client.Delete(ctx, *u)
return errors.Wrap(err, "Failed to delete response.")
}

Expand Down

0 comments on commit b6c1fd0

Please sign in to comment.