Skip to content

Commit

Permalink
Merge pull request #14 from green-api/dev
Browse files Browse the repository at this point in the history
Fixed #13
  • Loading branch information
Amele9 authored Dec 19, 2023
2 parents e29e714 + 666dd70 commit c96e7bc
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 14 deletions.
12 changes: 11 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions pkg/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/categories/methods/base_category.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
12 changes: 6 additions & 6 deletions pkg/categories/methods/journals.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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, "")
}
4 changes: 2 additions & 2 deletions pkg/categories/methods/queues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions pkg/categories/methods/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c96e7bc

Please sign in to comment.