go-twitch/api/extensions/update_extension_bits_produ...

90 lines
2.8 KiB
Go
Raw Permalink Normal View History

2024-03-02 22:45:59 -05:00
package extensions
import (
"context"
"encoding/json"
"fmt"
2024-03-02 22:45:59 -05:00
"io"
"net/http"
"time"
"go.fifitido.net/twitch/api/endpoint"
2024-03-02 22:45:59 -05:00
)
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 doesnt 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 extensions client ID.
func (e *Extensions) UpdateExtensionBitsProduct(ctx context.Context, body *UpdateExtensionBitsProductRequest) (*UpdateExtensionBitsProductResponse, error) {
2024-03-02 22:45:59 -05:00
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)
2024-03-02 22:45:59 -05:00
if err != nil {
return nil, err
}
res, err := e.client.Do(req)
2024-03-02 22:45:59 -05:00
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)
}
2024-03-02 22:45:59 -05:00
var data UpdateExtensionBitsProductResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}