2021-01-22 21:47:22 +00:00
|
|
|
// Package groupme defines a client capable of executing API commands for the GroupMe chat service
|
2020-07-30 02:20:57 +00:00
|
|
|
package groupme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GroupMe documentation: https://dev.groupme.com/docs/responses
|
|
|
|
|
|
|
|
// ID is an unordered alphanumeric string
|
|
|
|
type ID string
|
|
|
|
|
|
|
|
// Treated as a constant
|
|
|
|
var alphaNumericRegex = regexp.MustCompile(`^[a-zA-Z0-9]+$`)
|
|
|
|
|
|
|
|
// Valid checks if the ID string is alpha numeric
|
|
|
|
func (id ID) Valid() bool {
|
|
|
|
return alphaNumericRegex.MatchString(string(id))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (id ID) String() string {
|
|
|
|
return string(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timestamp is the number of seconds since the UNIX epoch
|
|
|
|
type Timestamp uint64
|
|
|
|
|
|
|
|
// FromTime returns the time.Time as a Timestamp
|
|
|
|
func FromTime(t time.Time) Timestamp {
|
|
|
|
return Timestamp(t.Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToTime returns the Timestamp as a UTC Time
|
|
|
|
func (t Timestamp) ToTime() time.Time {
|
|
|
|
return time.Unix(int64(t), 0).UTC()
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the Timestamp in the default time.Time string format
|
|
|
|
func (t Timestamp) String() string {
|
|
|
|
return t.ToTime().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PhoneNumber is the country code plus the number of the user
|
|
|
|
type PhoneNumber string
|
|
|
|
|
|
|
|
// Treated as a constant
|
2021-01-22 21:47:22 +00:00
|
|
|
var phoneNumberRegex = regexp.MustCompile(`^\+\d+ \d{10}$`)
|
2020-07-30 02:20:57 +00:00
|
|
|
|
|
|
|
// Valid checks if the ID string is alpha numeric
|
|
|
|
func (pn PhoneNumber) Valid() bool {
|
|
|
|
return phoneNumberRegex.MatchString(string(pn))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pn PhoneNumber) String() string {
|
|
|
|
return string(pn)
|
|
|
|
}
|
|
|
|
|
2021-01-22 21:47:22 +00:00
|
|
|
// HTTPStatusCode are returned by HTTP requests in
|
2020-07-30 02:20:57 +00:00
|
|
|
// the header and the json "meta" field
|
|
|
|
type HTTPStatusCode int
|
|
|
|
|
|
|
|
// Text used as constant name
|
|
|
|
const (
|
2021-01-22 21:47:22 +00:00
|
|
|
HTTPOk HTTPStatusCode = 200
|
|
|
|
HTTPCreated HTTPStatusCode = 201
|
|
|
|
HTTPNoContent HTTPStatusCode = 204
|
|
|
|
HTTPNotModified HTTPStatusCode = 304
|
|
|
|
HTTPBadRequest HTTPStatusCode = 400
|
|
|
|
HTTPUnauthorized HTTPStatusCode = 401
|
|
|
|
HTTPForbidden HTTPStatusCode = 403
|
|
|
|
HTTPNotFound HTTPStatusCode = 404
|
|
|
|
HTTPEnhanceYourCalm HTTPStatusCode = 420
|
|
|
|
HTTPInternalServerError HTTPStatusCode = 500
|
|
|
|
HTTPBadGateway HTTPStatusCode = 502
|
|
|
|
HTTPServiceUnavailable HTTPStatusCode = 503
|
2020-07-30 02:20:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// String returns the description of the status code according to GroupMe
|
|
|
|
func (c HTTPStatusCode) String() string {
|
|
|
|
return map[HTTPStatusCode]string{
|
2021-01-22 21:47:22 +00:00
|
|
|
HTTPOk: "success",
|
|
|
|
HTTPCreated: "resource was created successfully",
|
|
|
|
HTTPNoContent: "resource was deleted successfully",
|
|
|
|
HTTPNotModified: "no new data to return",
|
|
|
|
HTTPBadRequest: "invalid format or data specified in the request",
|
|
|
|
HTTPUnauthorized: "authentication credentials missing or incorrect",
|
|
|
|
HTTPForbidden: "request refused due to update limits",
|
|
|
|
HTTPNotFound: "URI is invalid or resource does not exist",
|
|
|
|
HTTPEnhanceYourCalm: "application is being rate limited",
|
|
|
|
HTTPInternalServerError: "something unexpected occurred",
|
|
|
|
HTTPBadGateway: "GroupMe is down or being upgraded",
|
|
|
|
HTTPServiceUnavailable: "servers are overloaded, try again later",
|
2020-07-30 02:20:57 +00:00
|
|
|
}[c]
|
|
|
|
}
|