Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(client): pointer arguments do not need to be returned #56

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions account_items.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ func (c *Client) GetAccountItems(
return nil, oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, APIPathAccountItems, http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
if err = c.call(ctx, APIPathAccountItems, http.MethodGet, oauth2Token, v, nil, &result); err != nil {
return nil, oauth2Token, err
}

Expand Down
36 changes: 18 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Client) call(ctx context.Context,
oauth2Token *oauth2.Token,
queryParams url.Values, postBody interface{},
res interface{},
) (*oauth2.Token, error) {
) error {
var (
contentType string
body io.Reader
Expand All @@ -74,14 +74,14 @@ func (c *Client) call(ctx context.Context,
contentType = "application/json"
jsonParams, err := json.Marshal(postBody)
if err != nil {
return oauth2Token, err
return err
}
body = bytes.NewBuffer(jsonParams)
}

req, err := c.newRequest(ctx, apiPath, method, contentType, queryParams, body)
if err != nil {
return oauth2Token, err
return err
}
return c.do(ctx, oauth2Token, req, res)
}
Expand All @@ -92,32 +92,32 @@ func (c *Client) postFiles(ctx context.Context,
queryParams url.Values, postBody map[string]string,
fileName string, file []byte,
res interface{},
) (*oauth2.Token, error) {
) error {
var (
contentType string
body = &bytes.Buffer{}
)
mw := multipart.NewWriter(body)
fw, err := mw.CreateFormFile("receipt", fileName)
if err != nil {
return oauth2Token, err
return err
}
if _, err := io.Copy(fw, bytes.NewReader(file)); err != nil {
return oauth2Token, err
return err
}
for k, v := range postBody {
if err := mw.WriteField(k, v); err != nil {
return oauth2Token, err
if err = mw.WriteField(k, v); err != nil {
return err
}
}
contentType = mw.FormDataContentType()
if err := mw.Close(); err != nil {
return oauth2Token, err
if err = mw.Close(); err != nil {
return err
}

req, err := c.newRequest(ctx, apiPath, method, contentType, queryParams, body)
if err != nil {
return oauth2Token, err
return err
}
return c.do(ctx, oauth2Token, req, res)
}
Expand Down Expand Up @@ -155,7 +155,7 @@ func (c *Client) do(
oauth2Token *oauth2.Token,
req *http.Request,
res interface{},
) (*oauth2.Token, error) {
) error {
tokenSource := c.config.Oauth2.TokenSource(ctx, oauth2Token)
httpClient := oauth2.NewClient(ctx, tokenSource)
response, err := httpClient.Do(req)
Expand All @@ -169,7 +169,7 @@ func (c *Client) do(
if e.Response != nil {
resp.StatusCode = e.Response.StatusCode
}
return oauth2Token, resp
return resp
}
errURL := &url.Error{}
if errors.As(err, &errURL) {
Expand All @@ -178,7 +178,7 @@ func (c *Client) do(
err = v
}
}
return oauth2Token, err
return err
}
defer response.Body.Close()
c.logf("[freee] %s: %s", HeaderXFreeeRequestID, response.Header.Get(HeaderXFreeeRequestID))
Expand Down Expand Up @@ -210,18 +210,18 @@ func (c *Client) do(
var e UnauthorizedError
if err := json.NewDecoder(bytes.NewReader(byt)).Decode(&e); err != nil {
c.logf("[freee] HTTP response body: %v", err)
return oauth2Token, res
return res
}
if e.Code == UnauthorizedCodeInvalidAccessToken ||
e.Code == UnauthorizedCodeExpiredAccessToken {
res.IsAuthorizationRequired = true
}
}
return oauth2Token, res
return res
}

if res == nil {
return oauth2Token, nil
return nil
}
return oauth2Token, json.NewDecoder(r).Decode(&res)
return json.NewDecoder(r).Decode(&res)
}
3 changes: 1 addition & 2 deletions companies.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ func (c *Client) GetCompany(
if err != nil {
return nil, oauth2Token, err
}
oauth2Token, err = c.call(ctx, path.Join(APIPathCompanies, fmt.Sprint(companyID)), http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
if err = c.call(ctx, path.Join(APIPathCompanies, fmt.Sprint(companyID)), http.MethodGet, oauth2Token, v, nil, &result); err != nil {
return nil, oauth2Token, err
}
return &result, oauth2Token, nil
Expand Down
11 changes: 4 additions & 7 deletions deals.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (c *Client) GetDeal(
return nil, oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathDeals, fmt.Sprint(dealID)), http.MethodGet, oauth2Token, v, nil, &result)
err = c.call(ctx, path.Join(APIPathDeals, fmt.Sprint(dealID)), http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
return nil, oauth2Token, err
}
Expand All @@ -268,8 +268,7 @@ func (c *Client) CreateDeal(
params DealCreateParams,
) (*Deal, *oauth2.Token, error) {
var result DealResponse
oauth2Token, err := c.call(ctx, APIPathDeals, http.MethodPost, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, APIPathDeals, http.MethodPost, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}
return &result.Deal, oauth2Token, nil
Expand All @@ -280,8 +279,7 @@ func (c *Client) UpdateDeal(
dealID uint64, params DealUpdateParams,
) (*Deal, *oauth2.Token, error) {
var result DealResponse
oauth2Token, err := c.call(ctx, path.Join(APIPathDeals, fmt.Sprint(dealID)), http.MethodPut, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, path.Join(APIPathDeals, fmt.Sprint(dealID)), http.MethodPut, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}
return &result.Deal, oauth2Token, nil
Expand All @@ -296,8 +294,7 @@ func (c *Client) DestroyDeal(
return oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathDeals, fmt.Sprint(dealID)), http.MethodDelete, oauth2Token, v, nil, nil)
if err != nil {
if err = c.call(ctx, path.Join(APIPathDeals, fmt.Sprint(dealID)), http.MethodDelete, oauth2Token, v, nil, nil); err != nil {
return oauth2Token, err
}

Expand Down
12 changes: 4 additions & 8 deletions items.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ func (c *Client) GetItems(
return nil, oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, APIPathItems, http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
if err = c.call(ctx, APIPathItems, http.MethodGet, oauth2Token, v, nil, &result); err != nil {
return nil, oauth2Token, err
}

Expand All @@ -75,8 +74,7 @@ func (c *Client) CreateItem(
params ItemParams,
) (*Item, *oauth2.Token, error) {
var result ItemResponse
oauth2Token, err := c.call(ctx, APIPathItems, http.MethodPost, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, APIPathItems, http.MethodPost, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}
return &result.Item, oauth2Token, nil
Expand All @@ -88,8 +86,7 @@ func (c *Client) UpdateItem(
itemID uint32,
) (*Item, *oauth2.Token, error) {
var result ItemResponse
oauth2Token, err := c.call(ctx, path.Join(APIPathItems, fmt.Sprint(itemID)), http.MethodPut, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, path.Join(APIPathItems, fmt.Sprint(itemID)), http.MethodPut, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}
return &result.Item, oauth2Token, nil
Expand All @@ -104,8 +101,7 @@ func (c *Client) DestroyItem(
return oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathItems, fmt.Sprint(itemID)), http.MethodDelete, oauth2Token, v, nil, nil)
if err != nil {
if err = c.call(ctx, path.Join(APIPathItems, fmt.Sprint(itemID)), http.MethodDelete, oauth2Token, v, nil, nil); err != nil {
return oauth2Token, err
}

Expand Down
9 changes: 3 additions & 6 deletions manual_journals.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,7 @@ func (c *Client) CreateManualJournal(
) (*ManualJournalResponse, *oauth2.Token, error) {
var result ManualJournalResponse

oauth2Token, err := c.call(ctx, APIPathManualJournals, http.MethodPost, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, APIPathManualJournals, http.MethodPost, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}

Expand All @@ -187,8 +186,7 @@ func (c *Client) UpdateManualJournal(
) (*ManualJournalResponse, *oauth2.Token, error) {
var result ManualJournalResponse

oauth2Token, err := c.call(ctx, path.Join(APIPathManualJournals, fmt.Sprint(journalID)), http.MethodPut, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, path.Join(APIPathManualJournals, fmt.Sprint(journalID)), http.MethodPut, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}

Expand All @@ -204,8 +202,7 @@ func (c *Client) DestroyManualJournal(
return oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathManualJournals, fmt.Sprint(journalID)), http.MethodDelete, oauth2Token, v, nil, nil)
if err != nil {
if err = c.call(ctx, path.Join(APIPathManualJournals, fmt.Sprint(journalID)), http.MethodDelete, oauth2Token, v, nil, nil); err != nil {
return oauth2Token, err
}

Expand Down
12 changes: 4 additions & 8 deletions partners.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ func (c *Client) CreatePartner(
) (*Partner, *oauth2.Token, error) {
var result PartnerResponse

oauth2Token, err := c.call(ctx, APIPathPartners, http.MethodPost, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, APIPathPartners, http.MethodPost, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}

Expand Down Expand Up @@ -260,8 +259,7 @@ func (c *Client) UpdatePartner(
) (*Partner, *oauth2.Token, error) {
var result PartnerResponse

oauth2Token, err := c.call(ctx, path.Join(APIPathPartners, fmt.Sprint(partnerID)), http.MethodPut, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, path.Join(APIPathPartners, fmt.Sprint(partnerID)), http.MethodPut, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}

Expand All @@ -288,8 +286,7 @@ func (c *Client) GetPartners(
return nil, oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, APIPathPartners, http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
if err = c.call(ctx, APIPathPartners, http.MethodGet, oauth2Token, v, nil, &result); err != nil {
return nil, oauth2Token, err
}

Expand All @@ -305,8 +302,7 @@ func (c *Client) DestroyPartner(
return oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathPartners, fmt.Sprint(partnerID)), http.MethodDelete, oauth2Token, v, nil, nil)
if err != nil {
if err = c.call(ctx, path.Join(APIPathPartners, fmt.Sprint(partnerID)), http.MethodDelete, oauth2Token, v, nil, nil); err != nil {
return oauth2Token, err
}

Expand Down
6 changes: 2 additions & 4 deletions receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ func (c *Client) CreateReceipt(
"issue_date": params.IssueDate,
}
var result ReceiptResponse
oauth2Token, err := c.postFiles(ctx, APIPathReceipts, http.MethodPost, oauth2Token, nil, postBody, receiptName, params.Receipt, &result)
if err != nil {
if err := c.postFiles(ctx, APIPathReceipts, http.MethodPost, oauth2Token, nil, postBody, receiptName, params.Receipt, &result); err != nil {
return nil, oauth2Token, err
}
return &result, oauth2Token, nil
Expand All @@ -102,8 +101,7 @@ func (c *Client) GetReceipt(
return nil, oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathReceipts, fmt.Sprint(receiptID)), http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
if err = c.call(ctx, path.Join(APIPathReceipts, fmt.Sprint(receiptID)), http.MethodGet, oauth2Token, v, nil, &result); err != nil {
return nil, oauth2Token, err
}
return &result, oauth2Token, nil
Expand Down
12 changes: 4 additions & 8 deletions sections.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ func (c *Client) GetSections(
return nil, oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, APIPathSections, http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
if err = c.call(ctx, APIPathSections, http.MethodGet, oauth2Token, v, nil, &result); err != nil {
return nil, oauth2Token, err
}

Expand All @@ -74,8 +73,7 @@ func (c *Client) CreateSection(
params SectionParams,
) (*Section, *oauth2.Token, error) {
var result SectionResponse
oauth2Token, err := c.call(ctx, APIPathSections, http.MethodPost, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, APIPathSections, http.MethodPost, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}
return &result.Section, oauth2Token, nil
Expand All @@ -86,8 +84,7 @@ func (c *Client) UpdateSection(
sectionID uint32, params SectionParams,
) (*Section, *oauth2.Token, error) {
var result SectionResponse
oauth2Token, err := c.call(ctx, path.Join(APIPathSections, fmt.Sprint(sectionID)), http.MethodPut, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, path.Join(APIPathSections, fmt.Sprint(sectionID)), http.MethodPut, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}
return &result.Section, oauth2Token, nil
Expand All @@ -102,8 +99,7 @@ func (c *Client) DestroySection(
return oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathSections, fmt.Sprint(sectionID)), http.MethodDelete, oauth2Token, v, nil, nil)
if err != nil {
if err = c.call(ctx, path.Join(APIPathSections, fmt.Sprint(sectionID)), http.MethodDelete, oauth2Token, v, nil, nil); err != nil {
return oauth2Token, err
}

Expand Down
12 changes: 4 additions & 8 deletions segment_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ func (c *Client) GetSegmentTags(
return nil, oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathSegments, fmt.Sprint(segmentID), "tags"), http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
if err = c.call(ctx, path.Join(APIPathSegments, fmt.Sprint(segmentID), "tags"), http.MethodGet, oauth2Token, v, nil, &result); err != nil {
return nil, oauth2Token, err
}

Expand All @@ -81,8 +80,7 @@ func (c *Client) CreateSegmentTag(
segmentID uint32, params SegmentTagParams,
) (*SegmentTag, *oauth2.Token, error) {
var result SegmentTagResponse
oauth2Token, err := c.call(ctx, path.Join(APIPathSegments, fmt.Sprint(segmentID), "tags"), http.MethodPost, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, path.Join(APIPathSegments, fmt.Sprint(segmentID), "tags"), http.MethodPost, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}
return &result.SegmentTag, oauth2Token, nil
Expand All @@ -94,8 +92,7 @@ func (c *Client) UpdateSegmentTag(
params SegmentTagParams,
) (*SegmentTag, *oauth2.Token, error) {
var result SegmentTagResponse
oauth2Token, err := c.call(ctx, path.Join(APIPathSegments, fmt.Sprint(segmentID), "tags", fmt.Sprint(id)), http.MethodPut, oauth2Token, nil, params, &result)
if err != nil {
if err := c.call(ctx, path.Join(APIPathSegments, fmt.Sprint(segmentID), "tags", fmt.Sprint(id)), http.MethodPut, oauth2Token, nil, params, &result); err != nil {
return nil, oauth2Token, err
}
return &result.SegmentTag, oauth2Token, nil
Expand All @@ -111,8 +108,7 @@ func (c *Client) DestroySegmentTag(
return oauth2Token, err
}
SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathSegments, fmt.Sprint(segmentID), "tags", fmt.Sprint(id)), http.MethodDelete, oauth2Token, v, nil, nil)
if err != nil {
if err = c.call(ctx, path.Join(APIPathSegments, fmt.Sprint(segmentID), "tags", fmt.Sprint(id)), http.MethodDelete, oauth2Token, v, nil, nil); err != nil {
return oauth2Token, err
}

Expand Down
Loading