summaryrefslogtreecommitdiff
path: root/src/cardmarket/account.go
blob: 3f9fbd2ad23274ec02228d33bf02c1fc37549032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cardmarket

import (
	"context"
	"encoding/json"
	"io"
	"log/slog"
)

type Account struct{}

func (c *ApiClient) GetAccountInformation(ctx context.Context) (Account, error) {
	slog.DebugContext(ctx, "requesting account info...")
	resp, err := c.httpClient.GetWithContext(ctx, c.baseURL+"/account")
	if err != nil {
		slog.DebugContext(ctx, "failed to get account info from API")
		return Account{}, err
	}
	slog.DebugContext(ctx, "received account info")

	slog.DebugContext(ctx, "reading body...")
	respBodyBuf, err := io.ReadAll(resp.Body)
	if err != nil {
		slog.DebugContext(ctx, "failed to read body")
		return Account{}, err
	}
	defer resp.Body.Close()
	slog.DebugContext(ctx, "read body")

	slog.DebugContext(ctx, "parsing body...", "respBodyBuf", string(respBodyBuf))
	type responseBody struct {
		Account struct {
			UserID            uint32 `json:"idUser"`
			Username          string `json:"username"`
			Type              uint8  `json:"isCommercial"`
			Reputation        uint8  `json:"reputation"`
			SalesCount        uint32 `json:"sellCount"`
			OnVacation        bool   `json:"onVacation"`
			Country           string `json:"country"`
			MaySell           bool   `json:"maySell"`
			SellerStatus      uint8  `json:"sellerActivation"`
			ShipsFast         int8   `json:"shipsFast"`
			SoldItemCount     uint32 `json:"soldItems"`
			AvgShippingTime   uint8  `json:"avgShippingTime"`
			DisplayLanguageID uint8  `json:"idDisplayLanguage"`
			Name              struct {
				First string `json:"firstName"`
				Last  string `json:"lastName"`
			} `json:"name"`
			HomeAddress struct {
				ReceiverName string `json:"name"`
				Extra        string `json:"extra"`
				Street       string `json:"street"`
				PostCode     string `json:"zip"`
				City         string `json:"city"`
				Country      string `json:"country"`
			} `json:"homeAddress"`
			Email            string `json:"email"`
			PhoneNumber      string `json:"phoneNumber"`
			VAT              string `json:"vat"`
			LegalInformation string `json:"legalInformation"`
			RegistrationDate string `json:"registerDate"`
			IsActivated      bool   `json:"isActivated"`
			BankAccount      struct {
				AccountOwner string `json:"accountOwner"`
				IBAN         string `json:"iban"`
				BIC          string `json:"bic"`
				BankName     string `json:"bankName"`
			} `json:"bankAccount"`
			ArticlesInShoppingCart uint16 `json:"articlesInShoppingCart"`
			UnreadMessages         uint16 `json:"unreadMessages"`
			Wallet                 struct {
				TotalBalance           float32 `json:"totalBalance"`
				MoneyBalance           float32 `json:"moneyBalance"`
				BonusBalance           float32 `json:"bonusBalance"`
				UnpaidAmount           float32 `json:"unpaidAmount"`
				ProviderRechargeAmount float32 `json:"providerRechargeAmount"`
				CurrencyID             uint8   `json:"idCurrency"`
				CurrencyCode           string  `json:"currencyCode"`
			} `json:"moneyDetails"`
			RiskGroup      uint8  `json:"riskGroup"`
			LossPercentage string `json:"lossPercentage"`
		} `json:"account"`
		Links []struct {
			Relationship string `json:"rel"`
			Endpoint     string `json:"href"`
			Method       string `json:"method"`
		} `json:"links"`
	}
	respBody := new(responseBody)
	if err = json.Unmarshal(respBodyBuf, respBody); err != nil {
		slog.DebugContext(ctx, "failed to unmarshal body: "+string(respBodyBuf))
		return Account{}, err
	}
	slog.DebugContext(ctx, "parsed body")

	return Account{}, nil
}