Skip to content

Commit

Permalink
Merge pull request #10 from Clarilab/fileUpload
Browse files Browse the repository at this point in the history
use file-upload strategy of zoho to upload larger content
  • Loading branch information
TobiEiss authored Nov 24, 2023
2 parents d5e3152 + ba1e048 commit 7417af1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.20

require (
github.com/go-resty/resty/v2 v2.10.0
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d
github.com/json-iterator/go v1.1.12
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-resty/resty/v2 v2.10.0 h1:Qla4W/+TMmv0fOeeRqzEpXPLfTUnR5HZ1+lGs+CkiCo=
github.com/go-resty/resty/v2 v2.10.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d h1:KbPOUXFUDJxwZ04vbmDOc3yuruGvVO+LOa7cVER3yWw=
github.com/gocarina/gocsv v0.0.0-20230616125104-99d496ca653d/go.mod h1:5YoVOkjYAQumqlV356Hj3xeYh4BdZuLE0/nRkf2NKkI=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand Down
44 changes: 35 additions & 9 deletions zoho.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package zoholab

import (
"bytes"
"fmt"
"io"
"mime/multipart"
"strings"

"github.com/Clarilab/zoholab/domain"
Expand Down Expand Up @@ -53,7 +56,7 @@ func (s *ZohoService) AddRow(tableUri string, columnValues map[string]string) (*

var addedRows ZohoAddRowResponse

err := s.sendAPIRequest(columnValues, tableUri, addRowAction, &addedRows)
err := s.sendAPIRequest(columnValues, nil, tableUri, addRowAction, nil, &addedRows)
if err != nil {
return nil, errors.Wrap(err, errMessage)
}
Expand All @@ -65,12 +68,27 @@ func (s *ZohoService) AddRow(tableUri string, columnValues map[string]string) (*
func (s *ZohoService) ImportCSV(tableUri, csvData string, config map[string]string) (*ZohoAddRowResponse, error) {
const errMessage = "could not import csv data in zoho"

config["ZOHO_IMPORT_DATA"] = csvData
config["ZOHO_IMPORT_FILETYPE"] = csvFileType
config["ZOHO_AUTO_IDENTIFY"] = autoIdentify

bodyBuf := bytes.NewBufferString("")
bodyWriter := multipart.NewWriter(bodyBuf)
_, err := bodyWriter.CreateFormFile("ZOHO_FILE", "file.csv")
if err != nil {
return nil, errors.Wrap(err, errMessage)
}

fileReader := strings.NewReader(csvData)

boundary := bodyWriter.Boundary()
closeBuf := bytes.NewBufferString(fmt.Sprintf("\r\n--%s--\r\n", boundary))
requestReader := io.MultiReader(bodyBuf, fileReader, closeBuf)

headers := map[string]string{
"Content-Type": "multipart/form-data; boundary=" + boundary,
}

var resp ZohoAddRowResponse
err := s.sendAPIRequest(config, tableUri, importAction, &resp)
err = s.sendAPIRequest(config, headers, tableUri, importAction, requestReader, &resp)
if err != nil {
return nil, errors.Wrap(err, errMessage)
}
Expand All @@ -79,12 +97,11 @@ func (s *ZohoService) ImportCSV(tableUri, csvData string, config map[string]stri
}

// sendAPIRequest sends a request to the zoho api.
func (s *ZohoService) sendAPIRequest(config map[string]string, path, action string, result any) error {
func (s *ZohoService) sendAPIRequest(config map[string]string, additionalHeaders map[string]string, path, action string, body any, result any) error {
const errMsg = "could not send api request"

resp, err := s.restyClient.
request := s.restyClient.
R().
SetHeader("User-Agent", "ZohoAnalytics GoLibrary").
SetQueryParams(map[string]string{
"ZOHO_ACTION": action,
"ZOHO_OUTPUT_FORMAT": outputFormat,
Expand All @@ -93,8 +110,17 @@ func (s *ZohoService) sendAPIRequest(config map[string]string, path, action stri
"ZOHO_VALID_JSON": validJson,
}).
SetQueryParams(config).
SetResult(result).
Post(path)
SetResult(result)

if body != nil {
request.SetBody(body)
}

if additionalHeaders != nil {
request.SetHeaders(additionalHeaders)
}

resp, err := request.Post(path)
if err != nil {
return errors.Wrap(err, errMsg)
}
Expand Down

0 comments on commit 7417af1

Please sign in to comment.