github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/pkg/depends/testutil/httptransporttestutil/server/cmd/app/routes/restful.go (about)

     1  package routes
     2  
     3  import (
     4  	"context"
     5  
     6  	pkgerr "github.com/pkg/errors"
     7  
     8  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport"
     9  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
    10  	"github.com/machinefi/w3bstream/pkg/depends/kit/kit"
    11  	"github.com/machinefi/w3bstream/pkg/depends/testutil/httptransporttestutil/server/pkg/errors"
    12  	"github.com/machinefi/w3bstream/pkg/depends/testutil/httptransporttestutil/server/pkg/types"
    13  )
    14  
    15  var RestfulRouter = kit.NewRouter(httptransport.Group("/restful"))
    16  
    17  func init() {
    18  	RootRouter.Register(RestfulRouter)
    19  
    20  	RestfulRouter.Register(kit.NewRouter(HealthCheck{}))
    21  	RestfulRouter.Register(kit.NewRouter(Create{}))
    22  	RestfulRouter.Register(kit.NewRouter(DataProvider{}, UpdateByID{}))
    23  	RestfulRouter.Register(kit.NewRouter(DataProvider{}, GetByID{}))
    24  	RestfulRouter.Register(kit.NewRouter(DataProvider{}, RemoveByID{}))
    25  }
    26  
    27  type HealthCheck struct {
    28  	httpx.MethodHead
    29  	PullPolicy types.PullPolicy `name:"pullPolicy,omitempty" in:"query"`
    30  }
    31  
    32  func (HealthCheck) Output(ctx context.Context) (interface{}, error) {
    33  	return nil, nil
    34  }
    35  
    36  // Create
    37  type Create struct {
    38  	httpx.MethodPost
    39  	Data Data `in:"body"`
    40  }
    41  
    42  func (req Create) Output(ctx context.Context) (interface{}, error) {
    43  	return &req.Data, nil
    44  }
    45  
    46  type Data struct {
    47  	ID        string         `json:"id"`
    48  	Label     string         `json:"label"`
    49  	PtrString *string        `json:"ptrString,omitempty"`
    50  	SubData   *SubData       `json:"subData,omitempty"`
    51  	Protocol  types.Protocol `json:"protocol,omitempty"`
    52  }
    53  
    54  type SubData struct {
    55  	Name string `json:"name"`
    56  }
    57  
    58  // get by id
    59  type GetByID struct {
    60  	httpx.MethodGet
    61  	Protocol types.Protocol `name:"protocol,omitempty" in:"query"`
    62  	Name     string         `name:"name,omitempty" in:"query"`
    63  	Label    []string       `name:"label,omitempty" in:"query"`
    64  }
    65  
    66  func (req GetByID) Output(ctx context.Context) (interface{}, error) {
    67  	data := DataFromContext(ctx)
    68  	if len(req.Label) > 0 {
    69  		data.Label = req.Label[0]
    70  	}
    71  	return data, nil
    72  }
    73  
    74  // remove by id
    75  type RemoveByID struct {
    76  	httpx.MethodDelete
    77  }
    78  
    79  // @StatusErr[InternalServerError][500100001][InternalServerError]
    80  func callWithErr() error {
    81  	return errors.Unauthorized
    82  }
    83  
    84  func (RemoveByID) Output(ctx context.Context) (interface{}, error) {
    85  	if false {
    86  		return nil, callWithErr()
    87  	}
    88  	return nil, httpx.WrapMeta(httpx.Metadata("X-Num", "1"))(errors.InternalServerError)
    89  }
    90  
    91  // update by id
    92  type UpdateByID struct {
    93  	httpx.MethodPut
    94  	Data Data `in:"body"`
    95  }
    96  
    97  func (req UpdateByID) Output(ctx context.Context) (interface{}, error) {
    98  	return nil, pkgerr.Errorf("something wrong")
    99  }
   100  
   101  type DataProvider struct {
   102  	ID string `name:"id" in:"path" validate:"@string[6,]"`
   103  }
   104  
   105  func (DataProvider) ContextKey() string {
   106  	return "DataProvider"
   107  }
   108  
   109  func (DataProvider) Path() string {
   110  	return "/:id"
   111  }
   112  
   113  func DataFromContext(ctx context.Context) *Data {
   114  	return ctx.Value("DataProvider").(*Data)
   115  }
   116  
   117  func (req DataProvider) Output(ctx context.Context) (interface{}, error) {
   118  	return &Data{
   119  		ID: req.ID,
   120  	}, nil
   121  }