From 5b567afe2ec5974ed0d4a0bdb6f5da397e77fc81 Mon Sep 17 00:00:00 2001 From: wanfeng Date: Fri, 21 Nov 2025 10:36:07 +0800 Subject: [PATCH] define interface resources for each API --- graffiti/api/rest/rest.go | 73 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 graffiti/api/rest/rest.go diff --git a/graffiti/api/rest/rest.go b/graffiti/api/rest/rest.go new file mode 100644 index 00000000..c69e5f15 --- /dev/null +++ b/graffiti/api/rest/rest.go @@ -0,0 +1,73 @@ +package rest + +import ( + "errors" + "time" +) + +// ErrDuplicatedResource is returned when a resource is duplicated +var ErrDuplicatedResource = errors.New("duplicated resource") + +// ErrNotFound is returned when a resource could not be found +var ErrNotFound = errors.New("resource not found") + +// ErrNotUpdatable is returned when an resource could not be modified +var ErrNotUpdatable = errors.New("resource not updatable") + +// Resource used as interface resources for each API +type Resource interface { + GetID() string + SetID(string) + GetName() string + Validate() error +} + +// Handler describes resources for each API +type Handler interface { + Name() string + New() Resource + Index() map[string]Resource + Get(id string) (Resource, bool) + Decorate(resource Resource) + Create(resource Resource, createOpts *CreateOptions) error + Delete(id string) error + Update(id string, resource Resource) (Resource, bool, error) +} + +// CreateOptions describes the available options when creating a resource +type CreateOptions struct { + TTL time.Duration +} + +// ResourceHandler aims to creates new resource of an API +type ResourceHandler interface { + Name() string + New() Resource +} + +// BasicResource is a resource with a unique identifier +// easyjson:json +// swagger:ignore +type BasicResource struct { + UUID string `yaml:"UUID"` +} + +// GetID returns the resource ID +func (b *BasicResource) GetID() string { + return b.UUID +} + +// SetID sets the resource ID +func (b *BasicResource) SetID(i string) { + b.UUID = i +} + +// GetName returns the resource name +func (b *BasicResource) GetName() string { + return "BasicResource" +} + +// Validate integrity of the resource +func (b *BasicResource) Validate() error { + return nil +} -- Gitee