Skip to content

Commit

Permalink
Merge pull request #60 from LayerXcom/add-walletables
Browse files Browse the repository at this point in the history
add walletables
  • Loading branch information
yyoshiki41 authored Feb 2, 2022
2 parents cfb4096 + 22c5e85 commit a46f566
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ func main() {

### 口座

- [ ] GET /api/1/walletables 口座一覧の取得
- [x] GET /api/1/walletables 口座一覧の取得
- [ ] POST /api/1/walletables 口座の作成
- [ ] GET /api/1/walletables/{type}/{id} 口座情報の取得
- [x] GET /api/1/walletables/{type}/{id} 口座情報の取得
- [ ] PUT /api/1/walletables/{type}/{id} 口座の更新
- [ ] DELETE /api/1/walletables/{type}/{id} 口座の削除
8 changes: 8 additions & 0 deletions transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func (c *Client) GetWalletTransactions(
companyID uint32, opts GetWalletTxnOpts) (*WalletTxnsResponse, *oauth2.Token, error) {
var result WalletTxnsResponse

if (opts.WalletableType != "" && opts.WalletableID == 0) || (opts.WalletableID != 0 && opts.WalletableType == "") {
return nil, oauth2Token, fmt.Errorf("either walletable_type or walletable_id is specified, then other value must be set")
}

v, err := query.Values(opts)
if err != nil {
return nil, oauth2Token, err
Expand All @@ -98,6 +102,10 @@ func (c *Client) GetWalletTransaction(
) (*WalletTxn, *oauth2.Token, error) {
var result WalletTxnResponse

if (opts.WalletableType != "" && opts.WalletableID == 0) || (opts.WalletableID != 0 && opts.WalletableType == "") {
return nil, oauth2Token, fmt.Errorf("either walletable_type or walletable_id is specified, then other value must be set")
}

v, err := query.Values(opts)
if err != nil {
return nil, oauth2Token, err
Expand Down
91 changes: 91 additions & 0 deletions walletabales.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package freee

import (
"context"
"fmt"
"github.com/google/go-querystring/query"
"golang.org/x/oauth2"
"net/http"
"path"
)

const (
APIPathWalletables = "walletables"
)

type WalletablesResponse struct {
Walletables []Walletable `json:"walletables"`
Meta Meta `json:"meta"`
}

type WalletableResponse struct {
Walletable Walletable `json:"walletable"`
Meta Meta `json:"meta"`
}

type Meta struct {
UpToDate bool `json:"up_to_date"`
}

type GetWalletablesOpts struct {
// 残高情報を含める
WithBalance bool `url:"with_balance,omitempty"`
// 口座区分 (銀行口座: bank_account, クレジットカード: credit_card, その他の決済口座: wallet)
Type string `url:"type,omitempty"`
}

type Walletable struct {
// 口座ID
ID uint64 `json:"id"`
// 口座名 (255文字以内)
Name string `json:"name"`
// サービスID
BankID uint64 `json:"bank_id"`
// 口座区分 (銀行口座: bank_account, クレジットカード: credit_card, 現金: wallet)
Type string `json:"type"`
// 同期残高
LastBalance int64 `json:"last_balance,omitempty"`
// 登録残高
WalletableBalance int64 `json:"walletable_balance,omitempty"`
}

func (c *Client) GetWalletables(
ctx context.Context, oauth2Token *oauth2.Token,
companyID uint32, opts GetWalletablesOpts,
) (*WalletablesResponse, *oauth2.Token, error) {
var result WalletablesResponse

v, err := query.Values(opts)
if err != nil {
return nil, oauth2Token, err
}

SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathWalletables), http.MethodGet,oauth2Token, v, nil, &result)
if err != nil {
return nil, oauth2Token, err
}

return &result, oauth2Token, nil
}


func (c *Client) GetWalletable(
ctx context.Context, oauth2Token *oauth2.Token,
companyID uint32, walletableID uint64, opts GetWalletTxnOpts,
) (*Walletable, *oauth2.Token, error) {
var result WalletableResponse

v, err := query.Values(opts)
if err != nil {
return nil, oauth2Token, err
}

SetCompanyID(&v, companyID)
oauth2Token, err = c.call(ctx, path.Join(APIPathWalletables, fmt.Sprint(walletableID)), http.MethodGet, oauth2Token, v, nil, &result)
if err != nil {
return nil, oauth2Token, err
}

return &result.Walletable, oauth2Token, nil
}

0 comments on commit a46f566

Please sign in to comment.