2024-02-28 14:25:19 -05:00
|
|
|
|
package entitlements
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
2024-03-07 19:41:05 -05:00
|
|
|
|
"fmt"
|
2024-02-28 14:25:19 -05:00
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
2024-03-07 20:52:42 -05:00
|
|
|
|
|
|
|
|
|
"go.fifitido.net/twitch/api/endpoint"
|
2024-02-28 14:25:19 -05:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UpdateDropsEntitlementsRequest struct {
|
|
|
|
|
// A list of IDs that identify the entitlements to update. You may specify a maximum of 100 IDs.
|
|
|
|
|
EntitlementIDs *[]string `json:"entitlement_ids,omitempty"`
|
|
|
|
|
|
|
|
|
|
// The fulfillment status to set the entitlements to.
|
|
|
|
|
FulfillmentStatus *FulfillmentStatus `json:"fulfillment_status,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UpdateDropsEntitlementsResponse struct {
|
|
|
|
|
// A list that indicates which entitlements were successfully updated and those that weren’t.
|
|
|
|
|
Data []UpdateDropsEntitlementsData `json:"data"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type UpdateDropsEntitlementsData struct {
|
|
|
|
|
// A string that indicates whether the status of the entitlements in the ids field were successfully updated.
|
|
|
|
|
Status UpdateStatus `json:"status"`
|
|
|
|
|
|
|
|
|
|
// The list of entitlements that the status in the status field applies to.
|
|
|
|
|
IDs []string `json:"ids"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Updates the Drop entitlement’s fulfillment status.
|
|
|
|
|
//
|
|
|
|
|
// The following table identifies which entitlements are updated based on the type of access token used.
|
|
|
|
|
// Access token type | Data that’s updated
|
|
|
|
|
// ------------------|------------------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
// App | Updates all entitlements with benefits owned by the organization in the access token.
|
|
|
|
|
// User | Updates all entitlements owned by the user in the access token and where the benefits are owned by the organization in the access token.
|
|
|
|
|
//
|
|
|
|
|
// Requires an app access token or user access token. The client ID in the access token must own the game.
|
2024-03-07 20:52:42 -05:00
|
|
|
|
func (e *Entitlements) UpdateDropsEntitlements(ctx context.Context, request *UpdateDropsEntitlementsRequest) (*UpdateDropsEntitlementsResponse, error) {
|
2024-02-28 14:25:19 -05:00
|
|
|
|
r, w := io.Pipe()
|
|
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
if err := json.NewEncoder(w).Encode(request); err != nil {
|
|
|
|
|
w.CloseWithError(err)
|
|
|
|
|
} else {
|
|
|
|
|
w.Close()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, endpoint.Make(e.baseUrl, "entitlements/drops"), r)
|
2024-02-28 14:25:19 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 20:52:42 -05:00
|
|
|
|
res, err := e.client.Do(req)
|
2024-02-28 14:25:19 -05:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-03-07 19:41:05 -05:00
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
|
|
statusOK := res.StatusCode >= 200 && res.StatusCode < 300
|
|
|
|
|
if !statusOK {
|
|
|
|
|
return nil, fmt.Errorf("failed to update drops entitlements (%d)", res.StatusCode)
|
|
|
|
|
}
|
2024-02-28 14:25:19 -05:00
|
|
|
|
|
|
|
|
|
var data UpdateDropsEntitlementsResponse
|
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&data); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &data, nil
|
|
|
|
|
}
|