123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- package rest
- import (
- "io"
- "git.ali33.ru/fcg-xvii/go-tools/json"
- )
- type (
- // RequestType - тип запроса
- RequestType byte
- // RequestFiles - словарь файлов запроса
- RequestFiles map[string]IReadCloserLen
- // TokenGenerateMethod - функция генерации токена авторизации
- TokenGenerateMethod func(data json.Map, expire int64) (string, error)
- )
- const (
- RequestTypeIn RequestType = iota
- RequestTypeOut
- RequestTypeEvent
- )
- type IRequest interface {
- RType() RequestType
- RCommand() string
- RData() json.Map
- RFiles() RequestFiles
- RFile(string) (IReadCloserLen, bool)
- RClose()
- Fields(any, RequestFiles) (json.Map, IErrorArgs)
- ParseOrder() (IOrderObject, IErrorArgs)
- ParseConditions() ([]ICondition, IErrorArgs)
- }
- ////////////////////////////////////////////
- type Request struct {
- Type RequestType
- Command string
- Data json.Map
- Files RequestFiles
- }
- func (s *Request) RType() RequestType {
- return s.Type
- }
- func (s *Request) RCommand() string {
- return s.Command
- }
- func (s *Request) RData() json.Map {
- if s.Data == nil {
- return json.Map{}
- }
- return s.Data
- }
- func (s *Request) RFiles() RequestFiles {
- return s.Files
- }
- func (s *Request) RFile(name string) (IReadCloserLen, bool) {
- res, check := s.Files[name]
- return res, check
- }
- func (s *Request) RClose() {
- for _, file := range s.Files {
- file.Close()
- }
- s.Files = nil
- }
- func (s *Request) Fields(obj any, files RequestFiles) (json.Map, IErrorArgs) {
- return Fields(obj, files, s.Data.Slice("fields", nil)...)
- }
- func (s *Request) ParseOffset() int {
- return s.Data.Int32("offset", 0)
- }
- func (s *Request) ParseLimit() int {
- return s.Data.Int32("limit", 20)
- }
- func (s *Request) ParseOrder() (IOrderObject, IErrorArgs) {
- res, err := parseOrder(
- s.Data.Map("order", json.Map{}),
- "",
- )
- if err != nil {
- return nil, ErrorFiled("order", err.Error())
- }
- return res, nil
- }
- func (s *Request) ParseConditions() (res []ICondition, rErr IErrorArgs) {
- s.Data.EachMap("conditions", func(index int, m json.Map) bool {
- cond, err := parseCondition(m, index)
- if err != nil {
- rErr = ErrorFiled("conditions", err.Error())
- return false
- }
- res = append(res, cond)
- return true
- })
- return
- }
- ////////////////////////////////////////////////////////////
- type IRequestIn interface {
- IRequest
- Auth() json.Map
- SetAuth(json.Map)
- GenerateToken(data json.Map, expire int64) (string, error)
- ROwner() IOwner
- RCore() any
- OutSuccess(data json.Map, files RequestFiles) IRequestOut
- OutError(err IErrorArgs) IRequestOut
- ClientData(key string) (any, bool)
- SetClientData(key string, data any)
- }
- type RequestIn struct {
- IRequest
- Owner IOwner
- Core any
- GeneratorToken TokenGenerateMethod
- }
- func (s *RequestIn) Auth() json.Map {
- if store, check := s.Owner.(IStream); check {
- return store.Auth()
- }
- return nil
- }
- func (s *RequestIn) SetAuth(auth json.Map) {
- if store, check := s.Owner.(IStream); check {
- store.SetAuth(auth)
- }
- }
- func (s *RequestIn) GenerateToken(data json.Map, expire int64) (string, error) {
- return s.GeneratorToken(data, expire)
- }
- func (s *RequestIn) ClientData(key string) (any, bool) {
- if store, check := s.Owner.(IStream); check {
- return store.ClientData(key)
- }
- return nil, false
- }
- func (s *RequestIn) SetClientData(key string, data any) {
- if store, check := s.Owner.(IStream); check {
- store.SetClientData(key, data)
- }
- }
- func (s *RequestIn) ROwner() IOwner {
- return s.Owner
- }
- func (s *RequestIn) RCore() any {
- return s.Core
- }
- func (s *RequestIn) OutSuccess(data json.Map, files RequestFiles) IRequestOut {
- return &RequestOut{
- Request: &Request{
- Type: RequestTypeOut,
- Command: s.RCommand(),
- Data: data,
- Files: files,
- },
- }
- }
- func (s *RequestIn) OutError(err IErrorArgs) IRequestOut {
- return &RequestOut{
- Err: err,
- }
- }
- /////////////////////////////////////////////////////////
- type IRequestOut interface {
- IRequest
- Write(io.Writer) IErrorArgs
- }
- /////////////////////////////////////////////////////////
- type RequestOut struct {
- *Request
- Err IErrorArgs
- }
- func (s *RequestOut) Write(w io.Writer) IErrorArgs {
- return nil
- }
|