Skip to content

Commit

Permalink
Add APIResponse interface.
Browse files Browse the repository at this point in the history
The APIResponse interface allows to easily group all API response
objects.
  • Loading branch information
NicoNex committed Dec 10, 2021
1 parent 0cfd7d4 commit 1c4a739
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions types.go
Expand Up @@ -48,6 +48,12 @@ type WebhookInfo struct {
AllowedUpdates []*UpdateType `json:"allowed_updates,omitempty"`
}

// APIResponse is implemented by all the APIResponse* types.
type APIResponse interface {
// Base returns the object of type APIResponseBase contained in each implemented type.
Base() APIResponseBase
}

// APIResponseBase is a base type that represents the incoming response from Telegram servers.
// Used by APIResponse* to slim down the implementation.
type APIResponseBase struct {
Expand All @@ -56,139 +62,239 @@ type APIResponseBase struct {
Description string `json:"description,omitempty"`
}

// Returns the APIResponseBase itself.
func (a APIResponseBase) Base() APIResponseBase {
return a
}

// APIResponseUpdate represents the incoming response from Telegram servers.
// Used by all methods that return an array of Update objects on success.
type APIResponseUpdate struct {
Result []*Update `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseUpdate) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseUser represents the incoming response from Telegram servers.
// Used by all methods that return a User object on success.
type APIResponseUser struct {
Result *User `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseUser) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseMessage represents the incoming response from Telegram servers.
// Used by all methods that return a Message object on success.
type APIResponseMessage struct {
Result *Message `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseMessage) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseMessageArray represents the incoming response from Telegram servers.
// Used by all methods that return an array of Message objects on success.
type APIResponseMessageArray struct {
Result []*Message `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseMessageArray) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseMessageID represents the incoming response from Telegram servers.
// Used by all methods that return a MessageID object on success.
type APIResponseMessageID struct {
Result *MessageID `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseMessageID) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseCommands represents the incoming response from Telegram servers.
// Used by all methods that return an array of BotCommand objects on success.
type APIResponseCommands struct {
Result []*BotCommand `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseCommands) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseBool represents the incoming response from Telegram servers.
// Used by all methods that return True on success.
type APIResponseBool struct {
Result bool `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseBool) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseString represents the incoming response from Telegram servers.
// Used by all methods that return a string on success.
type APIResponseString struct {
Result string `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseString) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseChat represents the incoming response from Telegram servers.
// Used by all methods that return a Chat object on success.
type APIResponseChat struct {
Result *Chat `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseChat) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseInviteLink represents the incoming response from Telegram servers.
// Used by all methods that return a ChatInviteLink object on success.
type APIResponseInviteLink struct {
Result *ChatInviteLink `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseInviteLink) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseStickerSet represents the incoming response from Telegram servers.
// Used by all methods that return a StickerSet object on success.
type APIResponseStickerSet struct {
Result *StickerSet `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseStickerSet) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseUserProfile represents the incoming response from Telegram servers.
// Used by all methods that return a UserProfilePhotos object on success.
type APIResponseUserProfile struct {
Result *UserProfilePhotos `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseUserProfile) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseFile represents the incoming response from Telegram servers.
// Used by all methods that return a File object on success.
type APIResponseFile struct {
Result *File `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseFile) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseAdministrators represents the incoming response from Telegram servers.
// Used by all methods that return an array of ChatMember objects on success.
type APIResponseAdministrators struct {
Result []*ChatMember `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseAdministrators) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseChatMember represents the incoming response from Telegram servers.
// Used by all methods that return a ChatMember object on success.
type APIResponseChatMember struct {
Result *ChatMember `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseChatMember) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseInteger represents the incoming response from Telegram servers.
// Used by all methods that return an integer on success.
type APIResponseInteger struct {
Result int `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseInteger) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponsePoll represents the incoming response from Telegram servers.
// Used by all methods that return a Poll object on success.
type APIResponsePoll struct {
Result *Poll `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponsePoll) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseGameHighScore represents the incoming response from Telegram servers.
// Used by all methods that return an array of GameHighScore objects on success.
type APIResponseGameHighScore struct {
Result []*GameHighScore `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseGameHighScore) Base() APIResponseBase {
return a.APIResponseBase
}

// APIResponseWebhook represents the incoming response from Telegram servers.
// Used by all methods that return a WebhookInfo object on success.
type APIResponseWebhook struct {
Result *WebhookInfo `json:"result,omitempty"`
APIResponseBase
}

// Returns the contained object of type APIResponseBase.
func (a APIResponseWebhook) Base() APIResponseBase {
return a.APIResponseBase
}

// User represents a Telegram user or bot.
type User struct {
ID int64 `json:"id"`
Expand Down

0 comments on commit 1c4a739

Please sign in to comment.