forked from yitsushi/go-misskey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yitsushi#65 add admin/promo/create endpoint
- Loading branch information
Tom Anderson
committed
Oct 7, 2023
1 parent
51b8fd6
commit 18a5be5
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package promo | ||
|
||
import ( | ||
"github.com/yitsushi/go-misskey/core" | ||
) | ||
|
||
// CreateRequest is the request object for a Create request. | ||
type CreateRequest struct { | ||
NoteID string `json:"noteId"` // required: <misskey:id> | ||
ExpiresAt int64 `json:"expiresAt"` // required: time.Now().Add(0,86400000,0).Unix() | ||
} | ||
|
||
// Validate the request. | ||
func (r CreateRequest) Validate() error { | ||
if r.NoteID == "" { | ||
return core.RequestValidationError{ | ||
Request: r, | ||
Message: core.UndefinedRequiredField, | ||
Field: "noteId", | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Create is the endpoint to create a promo. | ||
func (s *Service) Create(request CreateRequest) error { | ||
err := s.Call( | ||
&core.JSONRequest{ | ||
Request: &request, | ||
Path: "/admin/promo/create", | ||
}, | ||
&core.EmptyResponse{}, | ||
) | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package promo_test | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/yitsushi/go-misskey/core" | ||
"github.com/yitsushi/go-misskey/services/admin/promo" | ||
"github.com/yitsushi/go-misskey/test" | ||
) | ||
|
||
func TestService_Create(t *testing.T) { | ||
client := test.MakeMockClient(test.SimpleMockOptions{ | ||
Endpoint: "/api/admin/promo/create", | ||
RequestData: &promo.CreateRequest{}, | ||
ResponseFile: "create.json", | ||
StatusCode: http.StatusOK, | ||
}) | ||
|
||
err := client.Admin().Promo().Create(promo.CreateRequest{ | ||
NoteID: "noteID", | ||
ExpiresAt: time.Now().Add(86400 * 24 * time.Hour).Unix(), | ||
}) | ||
if !assert.NoError(t, err) { | ||
return | ||
} | ||
} | ||
|
||
func TestService_Create_Error(t *testing.T) { | ||
client := test.MakeMockClient(test.SimpleMockOptions{ | ||
Endpoint: "/api/admin/promo/create", | ||
RequestData: &promo.CreateRequest{}, | ||
ResponseFile: "create.json", | ||
StatusCode: http.StatusOK, | ||
}) | ||
|
||
// success | ||
err := client.Admin().Promo().Create(promo.CreateRequest{ | ||
NoteID: "noteID", | ||
ExpiresAt: time.Now().Add(86400 * 24 * time.Hour).Unix(), | ||
}) | ||
if !assert.NoError(t, err) { | ||
log.Printf("[Admin/Promos/Create] %s", err) | ||
|
||
return | ||
} | ||
|
||
// error | ||
|
||
} | ||
|
||
func TestPromoRequest_Validate(t *testing.T) { | ||
test.ValidateRequests( | ||
t, | ||
[]core.BaseRequest{ | ||
promo.CreateRequest{}, | ||
}, | ||
[]core.BaseRequest{ | ||
promo.CreateRequest{NoteID: "8zwxx3cpy7", ExpiresAt: 0}, | ||
promo.CreateRequest{NoteID: "8zwxx3cpy8", ExpiresAt: 0}, | ||
promo.CreateRequest{NoteID: "8zwxx3cpy9", ExpiresAt: time.Now().Add(86400 * 24 * time.Hour).Unix()}, | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package promo | ||
|
||
import ( | ||
"github.com/yitsushi/go-misskey/core" | ||
) | ||
|
||
// Service is the base for all the endpoints on this service. | ||
type Service struct { | ||
Call core.RequestHandlerFunc | ||
} | ||
|
||
// NewService creates a new Service instance. | ||
func NewService(requestHandler core.RequestHandlerFunc) *Service { | ||
return &Service{Call: requestHandler} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters