Introducing Contexts

Contexts have been added to all API requests, in addition to other linting fixes
This commit is contained in:
densestvoid
2021-01-22 16:47:22 -05:00
parent 646bec0e27
commit f92a8a7a86
33 changed files with 374 additions and 542 deletions

View File

@@ -1,7 +1,9 @@
// Package groupme defines a client capable of executing API commands for the GroupMe chat service
package groupme
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
@@ -12,7 +14,7 @@ import (
// GroupMe documentation: https://dev.groupme.com/docs/v3#messages
////////// Endpoints //////////
/*//////// Endpoints ////////*/
const (
// Used to build other endpoints
messagesEndpointRoot = groupEndpointRoot + "/messages"
@@ -21,9 +23,7 @@ const (
createMessagesEndpoint = messagesEndpointRoot // POST
)
// Index
// MessagesQuery defines the optional URL parameters for IndexMessages
// IndexMessagesQuery defines the optional URL parameters for IndexMessages
type IndexMessagesQuery struct {
// Returns messages created before the given message ID
BeforeID ID
@@ -39,7 +39,7 @@ func (q IndexMessagesQuery) String() string {
return marshal(&q)
}
// MessagesIndexResponse contains the count and set of
// IndexMessagesResponse contains the count and set of
// messages returned by the IndexMessages API request
type IndexMessagesResponse struct {
Count int `json:"count"`
@@ -51,41 +51,31 @@ func (r IndexMessagesResponse) String() string {
}
/*
IndexMessages -
Retrieve messages for a group.
IndexMessages - Retrieves messages for a group.
By default, messages are returned in groups of 20, ordered by
created_at descending. This can be raised or lowered by passing
a limit parameter, up to a maximum of 100 messages.
Messages can be scanned by providing a message ID as either the
before_id, since_id, or after_id parameter. If before_id is
provided, then messages immediately preceding the given message
will be returned, in descending order. This can be used to
continually page back through a group's messages.
The after_id parameter will return messages that immediately
follow a given message, this time in ascending order (which
makes it easy to pick off the last result for continued
pagination).
Finally, the since_id parameter also returns messages created
after the given message, but it retrieves the most recent
messages. For example, if more than twenty messages are created
after the since_id message, using this parameter will omit the
messages that immediately follow the given message. This is a
bit counterintuitive, so take care.
If no messages are found (e.g. when filtering with before_id)
we return code 304.
Note that for historical reasons, likes are returned as an
array of user ids in the favorited_by key.
Parameters: See MessageQuery
*/
func (c *Client) IndexMessages(groupID ID, req *IndexMessagesQuery) (IndexMessagesResponse, error) {
func (c *Client) IndexMessages(ctx context.Context, groupID ID, req *IndexMessagesQuery) (IndexMessagesResponse, error) {
url := fmt.Sprintf(c.endpointBase+indexMessagesEndpoint, groupID)
httpReq, err := http.NewRequest("GET", url, nil)
if err != nil {
@@ -111,7 +101,7 @@ func (c *Client) IndexMessages(groupID ID, req *IndexMessagesQuery) (IndexMessag
URL.RawQuery = query.Encode()
var resp IndexMessagesResponse
err = c.doWithAuthToken(httpReq, &resp)
err = c.doWithAuthToken(ctx, httpReq, &resp)
if err != nil {
return IndexMessagesResponse{}, err
}
@@ -119,11 +109,8 @@ func (c *Client) IndexMessages(groupID ID, req *IndexMessagesQuery) (IndexMessag
return resp, nil
}
// Create
/*
CreateMessage -
Send a message to a group
CreateMessage - Send a message to a group
If you want to attach an image, you must first process it
through our image service.
@@ -137,19 +124,8 @@ The character map is an array of arrays containing rune data
([[{pack_id,offset}],...]).
The placeholder should be a high-point/invisible UTF-8 character.
Parameters:
groupID - required, ID(String)
See Message.
text - required, string. Can be ommitted if at least one
attachment is present
attachments - a polymorphic list of attachments (locations,
images, etc). You may have You may have more than
one of any type of attachment, provided clients can
display it.
*/
func (c *Client) CreateMessage(groupID ID, m *Message) (*Message, error) {
func (c *Client) CreateMessage(ctx context.Context, groupID ID, m *Message) (*Message, error) {
URL := fmt.Sprintf(c.endpointBase+createMessagesEndpoint, groupID)
m.SourceGUID = uuid.New().String()
@@ -172,7 +148,7 @@ func (c *Client) CreateMessage(groupID ID, m *Message) (*Message, error) {
var resp struct {
*Message `json:"message"`
}
err = c.doWithAuthToken(httpReq, &resp)
err = c.doWithAuthToken(ctx, httpReq, &resp)
if err != nil {
return nil, err
}