Skip to content

Commit c96e7bc

Browse files
authored
Merge pull request #14 from green-api/dev
Fixed #13
2 parents e29e714 + 666dd70 commit c96e7bc

File tree

6 files changed

+25
-14
lines changed

6 files changed

+25
-14
lines changed

pkg/api/api.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,17 @@ func (a GreenAPI) Webhook() GreenAPIWebhook {
2828
func (a GreenAPI) Request(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error) {
2929
url := a.getURL(method, APIMethod, data)
3030

31-
return executeRequest(method, url, data, filePath)
31+
response, err := executeRequest(method, url, data, filePath)
32+
33+
return response.(map[string]interface{}), err
34+
}
35+
36+
func (a GreenAPI) ArrayRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error) {
37+
url := a.getURL(method, APIMethod, data)
38+
39+
response, err := executeRequest(method, url, data, filePath)
40+
41+
return response.([]interface{}), err
3242
}
3343

3444
func (a GreenAPI) getURL(method, APIMethod string, data map[string]interface{}) string {

pkg/api/http.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/gabriel-vasile/mimetype"
1515
)
1616

17-
func executeRequest(method, url string, data map[string]interface{}, filePath string) (map[string]interface{}, error) {
17+
func executeRequest(method, url string, data map[string]interface{}, filePath string) (interface{}, error) {
1818
client := &http.Client{}
1919

2020
req, err := getRequest(method, url, data, filePath)
@@ -126,7 +126,7 @@ func getUploadFileRequest(method, url string, filePath string) (*http.Request, e
126126
return req, nil
127127
}
128128

129-
func getResponse(resp *http.Response) (map[string]interface{}, error) {
129+
func getResponse(resp *http.Response) (interface{}, error) {
130130
body, err := io.ReadAll(resp.Body)
131131
if err != nil {
132132
return nil, err
@@ -141,7 +141,7 @@ func getResponse(resp *http.Response) (map[string]interface{}, error) {
141141
return nil, errors.New(fmt.Sprintf("StatusCode = %d. Body = %s.", resp.StatusCode, body))
142142
}
143143

144-
var data map[string]interface{}
144+
var data interface{}
145145

146146
err = json.Unmarshal(body, &data)
147147
if err != nil {

pkg/categories/methods/base_category.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ package methods
22

33
type GreenAPIInterface interface {
44
Request(method, APIMethod string, data map[string]interface{}, filePath string) (map[string]interface{}, error)
5+
ArrayRequest(method, APIMethod string, data map[string]interface{}, filePath string) ([]interface{}, error)
56
}

pkg/categories/methods/journals.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ type JournalsCategory struct {
55
}
66

77
// GetChatHistory returns the chat message history.
8-
func (c JournalsCategory) GetChatHistory(parameters map[string]interface{}) (map[string]interface{}, error) {
9-
return c.GreenAPI.Request("POST", "getChatHistory", parameters, "")
8+
func (c JournalsCategory) GetChatHistory(parameters map[string]interface{}) ([]interface{}, error) {
9+
return c.GreenAPI.ArrayRequest("POST", "getChatHistory", parameters, "")
1010
}
1111

1212
// GetMessage returns a chat message.
@@ -19,11 +19,11 @@ func (c JournalsCategory) GetMessage(chatId, idMessage string) (map[string]inter
1919

2020
// LastIncomingMessages returns the most recent incoming messages
2121
// of the account.
22-
func (c JournalsCategory) LastIncomingMessages(parameters map[string]interface{}) (map[string]interface{}, error) {
23-
return c.GreenAPI.Request("GET", "lastIncomingMessages", parameters, "")
22+
func (c JournalsCategory) LastIncomingMessages(parameters map[string]interface{}) ([]interface{}, error) {
23+
return c.GreenAPI.ArrayRequest("GET", "lastIncomingMessages", parameters, "")
2424
}
2525

2626
// LastOutgoingMessages returns the last sent messages of the account.
27-
func (c JournalsCategory) LastOutgoingMessages(parameters map[string]interface{}) (map[string]interface{}, error) {
28-
return c.GreenAPI.Request("GET", "lastOutgoingMessages", parameters, "")
27+
func (c JournalsCategory) LastOutgoingMessages(parameters map[string]interface{}) ([]interface{}, error) {
28+
return c.GreenAPI.ArrayRequest("GET", "lastOutgoingMessages", parameters, "")
2929
}

pkg/categories/methods/queues.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ type QueuesCategory struct {
66

77
// ShowMessagesQueue is designed to get the list of messages
88
// that are in the queue to be sent.
9-
func (c QueuesCategory) ShowMessagesQueue() (map[string]interface{}, error) {
10-
return c.GreenAPI.Request("GET", "showMessagesQueue", nil, "")
9+
func (c QueuesCategory) ShowMessagesQueue() ([]interface{}, error) {
10+
return c.GreenAPI.ArrayRequest("GET", "showMessagesQueue", nil, "")
1111
}
1212

1313
// ClearMessagesQueue is designed to clear the queue of messages to be sent.

pkg/categories/methods/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ func (c ServiceCategory) GetAvatar(chatId string) (map[string]interface{}, error
1919
}
2020

2121
// GetContacts is designed to get a list of contacts of the current account.
22-
func (c ServiceCategory) GetContacts() (map[string]interface{}, error) {
23-
return c.GreenAPI.Request("GET", "getContacts", nil, "")
22+
func (c ServiceCategory) GetContacts() ([]interface{}, error) {
23+
return c.GreenAPI.ArrayRequest("GET", "getContacts", nil, "")
2424
}
2525

2626
// GetContactInfo is designed to obtain information about the contact.

0 commit comments

Comments
 (0)