diff options
| author | DJ O'Leary <dijitol@proton.me> | 2026-09-06 23:52:13 +0200 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2026-09-06 23:52:13 +0200 |
| commit | 4c1525d84968a5302ce2d87f923b84b56c5777eb (patch) | |
| tree | cf553d12a0f529b62532b4dbb610ef1735dce0e5 /src | |
| parent | 497c6b9793e4745e7d180d2faec1e26ff6a28788 (diff) | |
feat(cardmarket): implement some endpoints of the v2 api
Diffstat (limited to 'src')
| -rw-r--r-- | src/cardmarket/account.go | 142 | ||||
| -rw-r--r-- | src/cardmarket/article.go | 118 | ||||
| -rw-r--r-- | src/cardmarket/game.go | 24 | ||||
| -rw-r--r-- | src/cardmarket/hateoas.go | 9 | ||||
| -rw-r--r-- | src/cardmarket/http.go | 36 |
5 files changed, 126 insertions, 203 deletions
diff --git a/src/cardmarket/account.go b/src/cardmarket/account.go index 3f9fbd2..bdef2bd 100644 --- a/src/cardmarket/account.go +++ b/src/cardmarket/account.go @@ -2,97 +2,69 @@ package cardmarket import ( "context" - "encoding/json" - "io" - "log/slog" ) -type Account struct{} +type 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"` +} 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"` + Account Account `json:"account"` + Links Links `json:"links"` } - respBody := new(responseBody) - if err = json.Unmarshal(respBodyBuf, respBody); err != nil { - slog.DebugContext(ctx, "failed to unmarshal body: "+string(respBodyBuf)) + respBody := &responseBody{} + if err := c.get(ctx, "/account", respBody); err != nil { return Account{}, err } - slog.DebugContext(ctx, "parsed body") - - return Account{}, nil + return respBody.Account, nil } diff --git a/src/cardmarket/article.go b/src/cardmarket/article.go deleted file mode 100644 index bc8ae21..0000000 --- a/src/cardmarket/article.go +++ /dev/null @@ -1,118 +0,0 @@ -package cardmarket - -import ( - "bytes" - "context" - "encoding/json" -) - -type ArticleCondition = string - -const ( - Mint ArticleCondition = "MT" - NearMint ArticleCondition = "NM" - Excellent ArticleCondition = "EX" - Good ArticleCondition = "GD" - LightPlayed ArticleCondition = "LP" - Played ArticleCondition = "PL" - Poor ArticleCondition = "PO" -) - -type LanguageID = uint8 - -const ( - English LanguageID = iota + 1 - French - German - Spanish - Italian - SimplifiedChinese - Japanese - Portuguese - Russian - Korean - TraditionalChinese -) - -type Article struct { - ProductID uint16 - Count uint8 - LanguageID LanguageID - Comments []byte - Price float32 - Condition ArticleCondition - IsFoil bool - IsSigned bool - IsAltered bool -} - -func (c *ApiClient) CreateArticle( - ctx context.Context, - productID uint16, - count uint8, - languageID LanguageID, - comments []byte, - price float32, - condition ArticleCondition, - isFoil *bool, - isSigned *bool, - isAltered *bool, -) (*Article, error) { - type requestBodyArticle struct { - ProductID uint16 `json:"idProduct"` - Count uint8 `json:"count"` - LanguageID LanguageID `json:"idLanguage"` - Comments []byte `json:"comments"` - Price float32 `json:"price"` - Condition ArticleCondition `json:"condition"` - IsFoil *bool `json:"isFoil,omitempty"` - IsSigned *bool `json:"isSigned,omitempty"` - IsAltered *bool `json:"isAltered,omitempty"` - } - reqBody := []requestBodyArticle{ - { - ProductID: productID, - Count: count, - LanguageID: languageID, - Comments: comments, - Price: price, - Condition: condition, - IsFoil: isFoil, - IsSigned: isSigned, - IsAltered: isAltered, - }, - } - - reqBodyBuf, err := json.Marshal(reqBody) - if err != nil { - return nil, err - } - - resp, err := c.httpClient.PostWithContext( - ctx, - c.baseURL+"/stock", - "application/json", - bytes.NewReader(reqBodyBuf), - ) - if err != nil { - return nil, err - } - - respBodyBuf := make([]byte, 1024) - _, err = resp.Body.Read(respBodyBuf) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - type responseBody struct { - Inserted map[string]string - Error string `json:"error"` - } - respBody := new(responseBody) - if err = json.Unmarshal(respBodyBuf, respBody); err != nil { - return nil, err - } - - return &Article{}, nil -} diff --git a/src/cardmarket/game.go b/src/cardmarket/game.go new file mode 100644 index 0000000..995162d --- /dev/null +++ b/src/cardmarket/game.go @@ -0,0 +1,24 @@ +package cardmarket + +import ( + "context" +) + +type Game struct { + GameID uint32 `json:"idGame"` + Name string `json:"name"` + Abbreviation string `json:"abbreviation"` + Links Links `json:"links"` +} + +func (c *ApiClient) GetGames(ctx context.Context) ([]Game, error) { + type responseBody struct { + Game []Game `json:"game"` + Links Links `json:"links"` + } + respBody := &responseBody{} + if err := c.get(ctx, "/games", respBody); err != nil { + return nil, err + } + return respBody.Game, nil +} diff --git a/src/cardmarket/hateoas.go b/src/cardmarket/hateoas.go new file mode 100644 index 0000000..c3cd92c --- /dev/null +++ b/src/cardmarket/hateoas.go @@ -0,0 +1,9 @@ +package cardmarket + +type Link struct { + Relationship string `json:"rel"` + Endpoint string `json:"href"` + Method string `json:"method"` +} + +type Links = []Link diff --git a/src/cardmarket/http.go b/src/cardmarket/http.go new file mode 100644 index 0000000..4789fdf --- /dev/null +++ b/src/cardmarket/http.go @@ -0,0 +1,36 @@ +package cardmarket + +import ( + "context" + "encoding/json" + "io" + "log/slog" +) + +func (c *ApiClient) get(ctx context.Context, endpoint string, respBody any) error { + slog.DebugContext(ctx, "requesting entity info...") + resp, err := c.httpClient.GetWithContext(ctx, c.baseURL+endpoint) + if err != nil { + slog.DebugContext(ctx, "failed to get entity from API") + return err + } + slog.DebugContext(ctx, "received entity info") + + slog.DebugContext(ctx, "reading body...") + respBodyBuf, err := io.ReadAll(resp.Body) + if err != nil { + slog.DebugContext(ctx, "failed to read body") + return err + } + defer resp.Body.Close() + slog.DebugContext(ctx, "read body") + + slog.DebugContext(ctx, "parsing body...", "respBodyBuf", string(respBodyBuf)) + if err = json.Unmarshal(respBodyBuf, respBody); err != nil { + slog.DebugContext(ctx, "failed to unmarshal body: "+string(respBodyBuf)) + return err + } + slog.DebugContext(ctx, "parsed body") + + return nil +} |
