From 1c4a73931f610856239479bf984bb1c303cfc157 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Santamaria?= Date: Fri, 10 Dec 2021 19:47:10 +0100 Subject: [PATCH] Add APIResponse interface. The APIResponse interface allows to easily group all API response objects. --- types.go | 106 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/types.go b/types.go index e58c455..d5ed312 100644 --- a/types.go +++ b/types.go @@ -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 { @@ -56,6 +62,11 @@ 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 { @@ -63,6 +74,11 @@ type APIResponseUpdate struct { 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 { @@ -70,6 +86,11 @@ type APIResponseUser struct { 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 { @@ -77,6 +98,11 @@ type APIResponseMessage struct { 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 { @@ -84,6 +110,11 @@ type APIResponseMessageArray struct { 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 { @@ -91,6 +122,11 @@ type APIResponseMessageID struct { 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 { @@ -98,6 +134,11 @@ type APIResponseCommands struct { 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 { @@ -105,6 +146,11 @@ type APIResponseBool struct { 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 { @@ -112,6 +158,11 @@ type APIResponseString struct { 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 { @@ -119,6 +170,11 @@ type APIResponseChat struct { 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 { @@ -126,6 +182,11 @@ type APIResponseInviteLink struct { 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 { @@ -133,6 +194,11 @@ type APIResponseStickerSet struct { 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 { @@ -140,6 +206,11 @@ type APIResponseUserProfile struct { 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 { @@ -147,6 +218,11 @@ type APIResponseFile struct { 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 { @@ -154,6 +230,11 @@ type APIResponseAdministrators struct { 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 { @@ -161,6 +242,11 @@ type APIResponseChatMember struct { 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 { @@ -168,6 +254,11 @@ type APIResponseInteger struct { 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 { @@ -175,6 +266,11 @@ type APIResponsePoll struct { 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 { @@ -182,6 +278,11 @@ type APIResponseGameHighScore struct { 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 { @@ -189,6 +290,11 @@ type APIResponseWebhook struct { 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"`