Files
dhkatz 145ebf25c1
CI / build (push) Successful in 16s
Release / release (push) Successful in 3s
CI / image (push) Successful in 12s
feat: sign in to IMVU for Access Pass products (#4)
Co-authored-by: dhkatz <[email protected]>
2026-11-08 16:15:05 +00:00

233 lines
7.4 KiB
Go

// Package hub talks to T5DE's hub as a catalog's importer: t5de-hub's docs/products.md ("For importers") and
// docs/imports.md. Every request carries `Authorization: Importer <catalog ID>:<key>`.
package hub
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// Client is one catalog's importer at a hub.
type Client struct {
url string
auth string
userAgent string
http *http.Client
}
// New makes a client for the hub at hubURL (its base, without /api/v1).
func New(hubURL, catalog, key, userAgent string, client *http.Client) *Client {
if client == nil {
client = http.DefaultClient
}
return &Client{
url: strings.TrimRight(hubURL, "/"),
auth: "Importer " + catalog + ":" + key,
userAgent: userAgent,
http: client,
}
}
// Error is the hub's refusal: its status and `{error, message}`.
type Error struct {
Status int
Code string
Message string
}
func (e *Error) Error() string {
if e.Message != "" {
return fmt.Sprintf("hub: %d %s: %s", e.Status, e.Code, e.Message)
}
return fmt.Sprintf("hub: %d %s", e.Status, e.Code)
}
// Settings are the importer's settings from the console.
type Settings struct {
Rate float64 `json:"rate"` // requests a second to IMVU, 0.2-10
Concurrency int `json:"concurrency"` // jobs at once, 1-8
}
// Account is an IMVU account the catalog's team added. Sealed is set until the importer says it has it.
type Account struct {
ID string `json:"id"`
IMVUID int64 `json:"imvuId"`
Username string `json:"username"`
Sealed *string `json:"sealed"`
}
// Config is what the importer runs with.
type Config struct {
Paused bool `json:"paused"`
Settings Settings `json:"settings"`
Fingerprint string `json:"fingerprint"`
Accounts []Account `json:"accounts"`
}
// Job is a claimed job. Rating is IMVU's ("general", "adult"), or "" when IMVU hides the product from the hub.
type Job struct {
ID string `json:"id"`
Kind string `json:"kind"` // "import", "recheck", or "list" (a whole shop, signed in)
IMVUProduct int64 `json:"imvuProduct"`
IMVUCreator int64 `json:"imvuCreator"` // a "list" job's shop
LastRevision int `json:"lastRevision"` // the newest IMVU revision imported already, or 0
Rating string `json:"rating"`
}
// Seen is a product as the importer's IMVU account sees it.
type Seen struct {
ID int64 `json:"id"`
Name string `json:"name"`
Rating string `json:"rating"`
CreatorID int64 `json:"creatorId"`
CreatorName string `json:"creatorName"`
Purchasable bool `json:"purchasable"`
}
// Listing reports a "list" job's shop, as the importer's account lists it; the hub keeps and queues its products.
func (c *Client) Listing(ctx context.Context, job string, products []Seen) (listed, queued int, err error) {
if products == nil {
products = []Seen{}
}
var r struct {
Listed int `json:"listed"`
Queued int `json:"queued"`
}
err = c.do(ctx, http.MethodPost, "/api/v1/importer/jobs/"+url.PathEscape(job)+"/listing", map[string]any{"products": products}, &r)
return r.Listed, r.Queued, err
}
// Info reports a job's product as the importer's account sees it (nil: not at all). The hub answers its rating
// (now known, "" if not) and whether the job's grant covers it.
func (c *Client) Info(ctx context.Context, job string, p *Seen) (rating string, covered bool, err error) {
body := map[string]any{"found": false}
if p != nil {
body = map[string]any{"found": true, "name": p.Name, "rating": p.Rating, "creatorId": p.CreatorID, "creatorName": p.CreatorName, "purchasable": p.Purchasable}
}
var r struct {
Rating *string `json:"rating"`
Covered bool `json:"covered"`
}
if err := c.do(ctx, http.MethodPost, "/api/v1/importer/jobs/"+url.PathEscape(job)+"/info", body, &r); err != nil {
return "", false, err
}
if r.Rating != nil {
rating = *r.Rating
}
return rating, r.Covered, nil
}
// Hello sends the importer's version and public key (base64 SPKI) and returns its config.
func (c *Client) Hello(ctx context.Context, version, publicKey string) (*Config, error) {
var cfg Config
err := c.do(ctx, http.MethodPost, "/api/v1/importer/hello", map[string]string{"version": version, "publicKey": publicKey}, &cfg)
return &cfg, err
}
// Config fetches the importer's config.
func (c *Client) Config(ctx context.Context) (*Config, error) {
var cfg Config
err := c.do(ctx, http.MethodGet, "/api/v1/importer/config", nil, &cfg)
return &cfg, err
}
// Claim claims up to limit (1-20) queued jobs.
func (c *Client) Claim(ctx context.Context, limit int) ([]Job, error) {
var r struct {
Jobs []Job `json:"jobs"`
}
err := c.do(ctx, http.MethodPost, "/api/v1/importer/jobs/claim", map[string]int{"limit": limit}, &r)
return r.Jobs, err
}
// Token gets a t5de-import token for a claimed job's IMVU revision, and the catalog's upload URL.
func (c *Client) Token(ctx context.Context, job string, imvuRevision int) (token, uploadURL string, err error) {
var r struct {
Token string `json:"token"`
URL string `json:"url"`
}
err = c.do(ctx, http.MethodPost, "/api/v1/importer/jobs/"+url.PathEscape(job)+"/token", map[string]int{"imvuRevision": imvuRevision}, &r)
return r.Token, r.URL, err
}
// Outcome is a job's outcome, for Report.
type Outcome struct {
Status string `json:"status"` // "done", "unchanged" or "failed"
IMVURevision int `json:"imvuRevision,omitempty"`
Reason string `json:"reason,omitempty"`
}
// Report reports a job's outcome. (An upload the catalog registered marks its job done already.)
func (c *Client) Report(ctx context.Context, job string, o Outcome) error {
if len(o.Reason) > 2000 {
o.Reason = o.Reason[:2000]
}
return c.do(ctx, http.MethodPost, "/api/v1/importer/jobs/"+url.PathEscape(job), o, nil)
}
// AccountReport is what the importer says about an account.
type AccountReport struct {
Received bool `json:"received,omitempty"`
State string `json:"state,omitempty"` // "ok", "sign_in_failed" or "needs_password"
AccessPass *bool `json:"accessPass,omitempty"`
Message string `json:"message,omitempty"`
}
// ReportAccount reports on an account: that the importer has its password, then how signing in went.
func (c *Client) ReportAccount(ctx context.Context, id string, r AccountReport) error {
return c.do(ctx, http.MethodPost, "/api/v1/importer/accounts/"+url.PathEscape(id), r, nil)
}
func (c *Client) do(ctx context.Context, method, path string, in, out any) error {
var body io.Reader
if in != nil {
b, err := json.Marshal(in)
if err != nil {
return err
}
body = bytes.NewReader(b)
}
req, err := http.NewRequestWithContext(ctx, method, c.url+path, body)
if err != nil {
return err
}
req.Header.Set("Authorization", c.auth)
req.Header.Set("User-Agent", c.userAgent)
if in != nil {
req.Header.Set("Content-Type", "application/json")
}
resp, err := c.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
if err != nil {
return err
}
if resp.StatusCode/100 != 2 {
e := &Error{Status: resp.StatusCode}
var r struct {
Error string `json:"error"`
Message string `json:"message"`
}
if json.Unmarshal(data, &r) == nil {
e.Code, e.Message = r.Error, r.Message
}
return e
}
if out == nil {
return nil
}
if err := json.Unmarshal(data, out); err != nil {
return fmt.Errorf("hub: %s %s: %w", method, path, err)
}
return nil
}