diff options
| author | DJ O'Leary <dijitol@proton.me> | 2026-09-03 02:43:45 +0200 |
|---|---|---|
| committer | DJ O'Leary <dijitol@proton.me> | 2026-09-03 02:43:45 +0200 |
| commit | 4f9fc42b75fbf99aeb43fc2cd05531d2ea929b99 (patch) | |
| tree | f3ff912419426b1cc0cc5958a28519fb9572502c /src/cardmarket/article.go | |
| parent | e1c4e483691ed9f3cdccc637b89e4a0d546b9b97 (diff) | |
feat(cardmarket): implement some endpoints of the v2 api
Diffstat (limited to 'src/cardmarket/article.go')
| -rw-r--r-- | src/cardmarket/article.go | 118 |
1 files changed, 118 insertions, 0 deletions
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 +} |
