go-twitch/api/conduit/create_conduits.go

54 lines
1.0 KiB
Go

package conduit
import (
"context"
"encoding/json"
"io"
"net/http"
"net/url"
)
type CreateConduitsRequest struct {
// The number of shards to create for this conduit.
ShardCount int `json:"shard_count"`
}
type CreateConduitsResponse struct {
Data []ConduitData `json:"data"`
}
// Creates a new conduit.
//
// Requires an app access token.
func (c *Conduit) CreateConduits(ctx context.Context, body *CreateConduitsRequest) (*CreateConduitsResponse, error) {
endpoint := c.baseUrl.ResolveReference(&url.URL{Path: "eventsub/conduits"})
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.MethodPost, endpoint.String(), r)
if err != nil {
return nil, err
}
res, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var data CreateConduitsResponse
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
return &data, nil
}