60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
package conduit
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"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()
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
if !statusOK {
|
|
return nil, fmt.Errorf("failed to create conduit (%d)", res.StatusCode)
|
|
}
|
|
|
|
var data CreateConduitsResponse
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &data, nil
|
|
}
|