From 666dd70ea5e1d593c795b6e702d2bef6fccdca65 Mon Sep 17 00:00:00 2001 From: Amele9 Date: Tue, 19 Dec 2023 20:46:19 +0600 Subject: [PATCH] Fixed array methods --- pkg/api/api.go | 12 +++++++++++- pkg/api/http.go | 6 +++--- pkg/categories/methods/base_category.go | 1 + pkg/categories/methods/journals.go | 12 ++++++------ pkg/categories/methods/queues.go | 4 ++-- pkg/categories/methods/service.go | 4 ++-- 6 files changed, 25 insertions(+), 14 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index cda8bc7..eb66613 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -28,7 +28,17 @@ func (a GreenAPI) Webhook() GreenAPIWebhook { func (a GreenAPI) Request(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error) { url := a.getURL(method, APIMethod, data) - return executeRequest(method, url, data, filePath) + response, err := executeRequest(method, url, data, filePath) + + return response.(map[string]interface{}), err +} + +func (a GreenAPI) ArrayRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error) { + url := a.getURL(method, APIMethod, data) + + response, err := executeRequest(method, url, data, filePath) + + return response.([]interface{}), err } func (a GreenAPI) getURL(method, APIMethod string, data map[string]interface{}) string { diff --git a/pkg/api/http.go b/pkg/api/http.go index 491f09c..4288380 100644 --- a/pkg/api/http.go +++ b/pkg/api/http.go @@ -14,7 +14,7 @@ import ( "github.com/gabriel-vasile/mimetype" ) -func executeRequest(method, url string, data map[string]interface{}, filePath string) (map[string]interface{}, error) { +func executeRequest(method, url string, data map[string]interface{}, filePath string) (interface{}, error) { client := &http.Client{} req, err := getRequest(method, url, data, filePath) @@ -126,7 +126,7 @@ func getUploadFileRequest(method, url string, filePath string) (*http.Request, e return req, nil } -func getResponse(resp *http.Response) (map[string]interface{}, error) { +func getResponse(resp *http.Response) (interface{}, error) { body, err := io.ReadAll(resp.Body) if err != nil { return nil, err @@ -141,7 +141,7 @@ func getResponse(resp *http.Response) (map[string]interface{}, error) { return nil, errors.New(fmt.Sprintf("StatusCode = %d. Body = %s.", resp.StatusCode, body)) } - var data map[string]interface{} + var data interface{} err = json.Unmarshal(body, &data) if err != nil { diff --git a/pkg/categories/methods/base_category.go b/pkg/categories/methods/base_category.go index 2abf4dc..132ae06 100644 --- a/pkg/categories/methods/base_category.go +++ b/pkg/categories/methods/base_category.go @@ -2,4 +2,5 @@ package methods type GreenAPIInterface interface { Request(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error) + ArrayRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error) } diff --git a/pkg/categories/methods/journals.go b/pkg/categories/methods/journals.go index ba23750..9cffa3d 100644 --- a/pkg/categories/methods/journals.go +++ b/pkg/categories/methods/journals.go @@ -5,8 +5,8 @@ type JournalsCategory struct { } // GetChatHistory returns the chat message history. -func (c JournalsCategory) GetChatHistory(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("POST", "getChatHistory", parameters, "") +func (c JournalsCategory) GetChatHistory(parameters map[string]interface{}) ([]interface{}, error) { + return c.GreenAPI.ArrayRequest("POST", "getChatHistory", parameters, "") } // GetMessage returns a chat message. @@ -19,11 +19,11 @@ func (c JournalsCategory) GetMessage(chatId, idMessage string) (map[string]inter // LastIncomingMessages returns the most recent incoming messages // of the account. -func (c JournalsCategory) LastIncomingMessages(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "lastIncomingMessages", parameters, "") +func (c JournalsCategory) LastIncomingMessages(parameters map[string]interface{}) ([]interface{}, error) { + return c.GreenAPI.ArrayRequest("GET", "lastIncomingMessages", parameters, "") } // LastOutgoingMessages returns the last sent messages of the account. -func (c JournalsCategory) LastOutgoingMessages(parameters map[string]interface{}) (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "lastOutgoingMessages", parameters, "") +func (c JournalsCategory) LastOutgoingMessages(parameters map[string]interface{}) ([]interface{}, error) { + return c.GreenAPI.ArrayRequest("GET", "lastOutgoingMessages", parameters, "") } diff --git a/pkg/categories/methods/queues.go b/pkg/categories/methods/queues.go index 5da5bf2..1808c45 100644 --- a/pkg/categories/methods/queues.go +++ b/pkg/categories/methods/queues.go @@ -6,8 +6,8 @@ type QueuesCategory struct { // ShowMessagesQueue is designed to get the list of messages // that are in the queue to be sent. -func (c QueuesCategory) ShowMessagesQueue() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "showMessagesQueue", nil, "") +func (c QueuesCategory) ShowMessagesQueue() ([]interface{}, error) { + return c.GreenAPI.ArrayRequest("GET", "showMessagesQueue", nil, "") } // ClearMessagesQueue is designed to clear the queue of messages to be sent. diff --git a/pkg/categories/methods/service.go b/pkg/categories/methods/service.go index 6b70fa7..853580c 100644 --- a/pkg/categories/methods/service.go +++ b/pkg/categories/methods/service.go @@ -19,8 +19,8 @@ func (c ServiceCategory) GetAvatar(chatId string) (map[string]interface{}, error } // GetContacts is designed to get a list of contacts of the current account. -func (c ServiceCategory) GetContacts() (map[string]interface{}, error) { - return c.GreenAPI.Request("GET", "getContacts", nil, "") +func (c ServiceCategory) GetContacts() ([]interface{}, error) { + return c.GreenAPI.ArrayRequest("GET", "getContacts", nil, "") } // GetContactInfo is designed to obtain information about the contact.