90 lines
2.8 KiB
Go
90 lines
2.8 KiB
Go
package extensions
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"io"
|
||
"net/http"
|
||
"time"
|
||
|
||
"go.fifitido.net/twitch/api/endpoint"
|
||
)
|
||
|
||
type UpdateExtensionBitsProductRequest struct {
|
||
// The product's SKU. The SKU must be unique within an extension. The product's SKU cannot be changed.
|
||
// The SKU may contain only alphanumeric characters, dashes (-), underscores (_), and periods (.) and is limited to a maximum of 255 characters. No spaces.
|
||
SKU string `json:"sku"`
|
||
|
||
// An object that contains the product's cost information.
|
||
Cost struct {
|
||
// The product's price.
|
||
Amount int `json:"amount"`
|
||
|
||
// The type of currency. Possible values are:
|
||
//
|
||
// bits
|
||
Type string `json:"type"`
|
||
} `json:"cost"`
|
||
|
||
// The product's name as displayed in the extension. The maximum length is 255 characters.
|
||
DisplayName string `json:"display_name"`
|
||
|
||
// A Boolean value that indicates whether the product is in development.
|
||
// Set to true if the product is in development and not available for public use.
|
||
// The default is false.
|
||
InDevelopment *bool `json:"in_development"`
|
||
|
||
// The date and time, in RFC3339 format, when the product expires.
|
||
Expiration *time.Time `json:"expiration"`
|
||
|
||
// A Boolean value that determines whether Bits product purchase events are broadcast to all instances of the extension on a channel.
|
||
// The events are broadcast via the onTransactionComplete helper callback. The default is false.
|
||
IsBroadcast *bool `json:"is_broadcast"`
|
||
}
|
||
|
||
type UpdateExtensionBitsProductResponse struct {
|
||
// A list of Bits products that the extension created. The list is in ascending SKU order.
|
||
// The list is empty if the extension hasn't created any products or they're all expired or disabled.
|
||
Data []BitsProduct `json:"data"`
|
||
}
|
||
|
||
// Adds or updates a Bits product that the extension created.
|
||
// If the SKU doesn’t exist, the product is added. You may update all fields except the sku field.
|
||
//
|
||
// Requires an app access token. The client ID in the app access token must match the extension’s client ID.
|
||
func (e *Extensions) UpdateExtensionBitsProduct(ctx context.Context, body *UpdateExtensionBitsProductRequest) (*UpdateExtensionBitsProductResponse, error) {
|
||
r, w := io.Pipe()
|
||
|
||
go func() {
|
||
if err := json.NewEncoder(w).Encode(body); err != nil {
|
||
w.CloseWithError(err)
|
||
} else {
|
||
w.Close()
|
||
}
|
||
}()
|
||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, endpoint.Make(e.baseUrl, "bits/extensions"), r)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
res, err := e.client.Do(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
defer res.Body.Close()
|
||
|
||
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
||
if !statusOK {
|
||
return nil, fmt.Errorf("failed to update extension bits product (%d)", res.StatusCode)
|
||
}
|
||
|
||
var data UpdateExtensionBitsProductResponse
|
||
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &data, nil
|
||
}
|