From 4f9fc42b75fbf99aeb43fc2cd05531d2ea929b99 Mon Sep 17 00:00:00 2001 From: DJ O'Leary Date: Thu, 3 Sep 2026 02:43:45 +0200 Subject: feat(cardmarket): implement some endpoints of the v2 api --- src/cardmarket/account.go | 88 ++++++++++++++++++++++++++++++++++ src/cardmarket/article.go | 118 ++++++++++++++++++++++++++++++++++++++++++++++ src/cardmarket/client.go | 14 ++++++ 3 files changed, 220 insertions(+) create mode 100644 src/cardmarket/account.go create mode 100644 src/cardmarket/article.go create mode 100644 src/cardmarket/client.go (limited to 'src') diff --git a/src/cardmarket/account.go b/src/cardmarket/account.go new file mode 100644 index 0000000..3baa747 --- /dev/null +++ b/src/cardmarket/account.go @@ -0,0 +1,88 @@ +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 { + UserID uint32 `json:"idUser"` + Username []byte `json:"username"` + Country []byte `json:"country"` + Type uint8 `json:"isCommercial"` + SellerStatus uint8 `json:"sellerActivation"` + RiskGroup uint8 `json:"riskGroup"` + Reputation uint8 `json:"reputation"` + ShipsFast uint8 `json:"shipsFast"` + SalesCount uint32 `json:"sellCount"` + SoldItemCount uint32 `json:"soldItems"` + AvgShippingTime uint8 `json:"avgShippingTime"` + OnVacation bool `json:"onVacation"` + DisplayLanguageID uint8 `json:"idDisplayLanguage"` + Name struct { + First []byte `json:"firstName"` + Last []byte `json:"lastName"` + } `json:"name"` + HomeAddress struct { + ReceiverName []byte `json:"name"` + Extra []byte `json:"extra"` + Street []byte `json:"street"` + PostCode []byte `json:"zip"` + City []byte `json:"city"` + Country []byte `json:"country"` + } `json:"homeAddress"` + Email []byte `json:"email"` + PhoneNumber []byte `json:"phoneNumber"` + VAT []byte `json:"vat"` + RegistrationDate []byte `json:"registerDate"` + IsActivated bool `json:"isActivated"` + Wallet struct { + TotalBalance float32 `json:"totalBalance"` + MoneyBalance float32 `json:"moneyBalance"` + BonusBalance float32 `json:"bonusBalance"` + UnpaidAmount float32 `json:"unpaidAmount"` + ProviderRechargeAmount float32 `json:"providerRechargeAmount"` + } `json:"moneyDetails"` + BankAccount struct { + AccountOwner []byte `json:"accountOwner"` + IBAN []byte `json:"iban"` + BIC []byte `json:"bic"` + BankName []byte `json:"bankName"` + } `json:"bankAccount"` + ArticlesInShoppingCart uint16 `json:"articlesInShoppingCart"` + UnreadMessages uint16 `json:"unreadMessages"` + MaySell bool `json:"maySell"` + Links map[string]string `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 +} diff --git a/src/cardmarket/article.go b/src/cardmarket/article.go new file mode 100644 index 0000000..bc8ae21 --- /dev/null +++ b/src/cardmarket/article.go @@ -0,0 +1,118 @@ +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/client.go b/src/cardmarket/client.go new file mode 100644 index 0000000..4de93ba --- /dev/null +++ b/src/cardmarket/client.go @@ -0,0 +1,14 @@ +package cardmarket + +import ( + "codeberg.org/dijitol/collector_ouphe/src/oauth" +) + +type ApiClient struct { + baseURL string + httpClient *oauth.Client +} + +func NewApiClient(baseURL string, client *oauth.Client) *ApiClient { + return &ApiClient{baseURL: baseURL, httpClient: client} +} -- cgit v1.2.3