From e1848b45b493f0b4a0a051a1edbf6946957037c9 Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Wed, 5 Dec 2018 17:14:51 +0300 Subject: [PATCH] improve PostRequest method --- v1/client.go | 12 +++++++----- v1/client_test.go | 39 +++++++++++++++++++++++++++++++++++++-- v1/request.go | 11 +++++++---- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/v1/client.go b/v1/client.go index 4852e22..bff713e 100644 --- a/v1/client.go +++ b/v1/client.go @@ -1,9 +1,11 @@ package v1 import ( + "bytes" "encoding/json" "errors" "fmt" + "io" "net/http" "strconv" "time" @@ -98,7 +100,7 @@ func (c *MgClient) ActivateTransportChannel(request Channel) (ActivateResponse, var resp ActivateResponse outgoing, _ := json.Marshal(&request) - data, status, err := c.PostRequest("/channels", []byte(outgoing)) + data, status, err := c.PostRequest("/channels", bytes.NewBuffer(outgoing)) if err != nil { return resp, status, err } @@ -243,7 +245,7 @@ func (c *MgClient) Messages(request SendData) (MessagesResponse, int, error) { var resp MessagesResponse outgoing, _ := json.Marshal(&request) - data, status, err := c.PostRequest("/messages", []byte(outgoing)) + data, status, err := c.PostRequest("/messages", bytes.NewBuffer(outgoing)) if err != nil { return resp, status, err } @@ -326,7 +328,7 @@ func (c *MgClient) MarkMessageRead(request MarkMessageReadRequest) (MarkMessageR var resp MarkMessageReadResponse outgoing, _ := json.Marshal(&request) - data, status, err := c.PostRequest("/messages/read", []byte(outgoing)) + data, status, err := c.PostRequest("/messages/read", bytes.NewBuffer(outgoing)) if err != nil { return resp, status, err } @@ -420,7 +422,7 @@ func (c *MgClient) GetFile(request string) (FullFileResponse, int, error) { } // UploadFile upload file -func (c *MgClient) UploadFile(request []byte) (UploadFileResponse, int, error) { +func (c *MgClient) UploadFile(request io.Reader) (UploadFileResponse, int, error) { var resp UploadFileResponse data, status, err := c.PostRequest("/files/upload", request) @@ -444,7 +446,7 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe var resp UploadFileResponse outgoing, _ := json.Marshal(&request) - data, status, err := c.PostRequest("/files/upload_by_url", []byte(outgoing)) + data, status, err := c.PostRequest("/files/upload_by_url", bytes.NewBuffer(outgoing)) if err != nil { return resp, status, err } diff --git a/v1/client_test.go b/v1/client_test.go index 6322761..dde96df 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -181,7 +181,7 @@ func TestMgClient_TextMessages(t *testing.T) { snd := SendData{ Message: Message{ ExternalID: ext, - Type: "text", + Type: MsgTypeText, Text: "hello!", }, User: User{ @@ -220,7 +220,7 @@ func TestMgClient_ImageMessages(t *testing.T) { snd := SendData{ Message: Message{ - ExternalID: ext, + ExternalID: ext + "file", Type: MsgTypeImage, Items: []Item{{ID: uploadFileResponse.ID}}, }, @@ -304,6 +304,21 @@ func TestMgClient_MarkMessageReadAndDelete(t *testing.T) { } t.Logf("Message %v deleted", data.MessageID) + + sndD = DeleteData{ + Message{ + ExternalID: ext + "file", + }, + channelID, + } + + data, status, err = c.DeleteMessage(sndD) + + if status != http.StatusOK { + t.Errorf("%v", err) + } + + t.Logf("Message %v deleted", data.MessageID) } func TestMgClient_DeactivateTransportChannel(t *testing.T) { @@ -320,3 +335,23 @@ func TestMgClient_DeactivateTransportChannel(t *testing.T) { t.Logf("Deactivate selected channel: %v", deleteData.ChannelID) } + +func TestMgClient_UploadFile(t *testing.T) { + c := client() + t.Logf("%v", ext) + + resp, err := http.Get("https://via.placeholder.com/300") + if err != nil { + t.Errorf("%v", err) + } + + defer resp.Body.Close() + + data, status, err := c.UploadFile(resp.Body) + + if status != http.StatusOK { + t.Errorf("%v", err) + } + + t.Logf("Message %+v is sent", data) +} diff --git a/v1/request.go b/v1/request.go index b7d65fc..5727d74 100644 --- a/v1/request.go +++ b/v1/request.go @@ -3,6 +3,7 @@ package v1 import ( "bytes" "fmt" + "io" "io/ioutil" "log" "net/http" @@ -21,11 +22,11 @@ func (c *MgClient) GetRequest(url string, parameters []byte) ([]byte, int, error } // PostRequest implements POST Request -func (c *MgClient) PostRequest(url string, parameters []byte) ([]byte, int, error) { +func (c *MgClient) PostRequest(url string, parameters io.Reader) ([]byte, int, error) { return makeRequest( "POST", fmt.Sprintf("%s%s%s", c.URL, prefix, url), - bytes.NewBuffer(parameters), + parameters, c, ) } @@ -50,7 +51,7 @@ func (c *MgClient) DeleteRequest(url string, parameters []byte) ([]byte, int, er ) } -func makeRequest(reqType, url string, buf *bytes.Buffer, c *MgClient) ([]byte, int, error) { +func makeRequest(reqType, url string, buf io.Reader, c *MgClient) ([]byte, int, error) { var res []byte req, err := http.NewRequest(reqType, url, buf) if err != nil { @@ -61,7 +62,9 @@ func makeRequest(reqType, url string, buf *bytes.Buffer, c *MgClient) ([]byte, i req.Header.Set("X-Transport-Token", c.Token) if c.Debug { - log.Printf("MG TRANSPORT API Request: %s %s %s %s", reqType, url, c.Token, buf.String()) + b := new(bytes.Buffer) + b.ReadFrom(buf) + log.Printf("MG TRANSPORT API Request: %s %s %s %s", reqType, url, c.Token, b.String()) } resp, err := c.httpClient.Do(req)