request.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package rest
  2. import (
  3. "io"
  4. "git.ali33.ru/fcg-xvii/go-tools/json"
  5. )
  6. type (
  7. // RequestType - тип запроса
  8. RequestType byte
  9. // RequestFiles - словарь файлов запроса
  10. RequestFiles map[string]IReadCloserLen
  11. // TokenGenerateMethod - функция генерации токена авторизации
  12. TokenGenerateMethod func(data json.Map, expire int64) (string, error)
  13. )
  14. const (
  15. RequestTypeIn RequestType = iota
  16. RequestTypeOut
  17. RequestTypeEvent
  18. )
  19. type IRequest interface {
  20. RType() RequestType
  21. RCommand() string
  22. RData() json.Map
  23. RFiles() RequestFiles
  24. RFile(string) (IReadCloserLen, bool)
  25. RClose()
  26. Fields(any, RequestFiles) (json.Map, IErrorArgs)
  27. ParseOrder() (IOrderObject, IErrorArgs)
  28. ParseConditions() ([]ICondition, IErrorArgs)
  29. }
  30. ////////////////////////////////////////////
  31. type Request struct {
  32. Type RequestType
  33. Command string
  34. Data json.Map
  35. Files RequestFiles
  36. }
  37. func (s *Request) RType() RequestType {
  38. return s.Type
  39. }
  40. func (s *Request) RCommand() string {
  41. return s.Command
  42. }
  43. func (s *Request) RData() json.Map {
  44. if s.Data == nil {
  45. return json.Map{}
  46. }
  47. return s.Data
  48. }
  49. func (s *Request) RFiles() RequestFiles {
  50. return s.Files
  51. }
  52. func (s *Request) RFile(name string) (IReadCloserLen, bool) {
  53. res, check := s.Files[name]
  54. return res, check
  55. }
  56. func (s *Request) RClose() {
  57. for _, file := range s.Files {
  58. file.Close()
  59. }
  60. s.Files = nil
  61. }
  62. func (s *Request) Fields(obj any, files RequestFiles) (json.Map, IErrorArgs) {
  63. return Fields(obj, files, s.Data.Slice("fields", nil)...)
  64. }
  65. func (s *Request) ParseOffset() int {
  66. return s.Data.Int32("offset", 0)
  67. }
  68. func (s *Request) ParseLimit() int {
  69. return s.Data.Int32("limit", 20)
  70. }
  71. func (s *Request) ParseOrder() (IOrderObject, IErrorArgs) {
  72. res, err := parseOrder(
  73. s.Data.Map("order", json.Map{}),
  74. "",
  75. )
  76. if err != nil {
  77. return nil, ErrorFiled("order", err.Error())
  78. }
  79. return res, nil
  80. }
  81. func (s *Request) ParseConditions() (res []ICondition, rErr IErrorArgs) {
  82. s.Data.EachMap("conditions", func(index int, m json.Map) bool {
  83. cond, err := parseCondition(m, index)
  84. if err != nil {
  85. rErr = ErrorFiled("conditions", err.Error())
  86. return false
  87. }
  88. res = append(res, cond)
  89. return true
  90. })
  91. return
  92. }
  93. ////////////////////////////////////////////////////////////
  94. type IRequestIn interface {
  95. IRequest
  96. Auth() json.Map
  97. SetAuth(json.Map)
  98. GenerateToken(data json.Map, expire int64) (string, error)
  99. ROwner() IOwner
  100. RCore() any
  101. OutSuccess(data json.Map, files RequestFiles) IRequestOut
  102. OutError(err IErrorArgs) IRequestOut
  103. ClientData(key string) (any, bool)
  104. SetClientData(key string, data any)
  105. }
  106. type RequestIn struct {
  107. IRequest
  108. Owner IOwner
  109. Core any
  110. GeneratorToken TokenGenerateMethod
  111. }
  112. func (s *RequestIn) Auth() json.Map {
  113. if store, check := s.Owner.(IStream); check {
  114. return store.Auth()
  115. }
  116. return nil
  117. }
  118. func (s *RequestIn) SetAuth(auth json.Map) {
  119. if store, check := s.Owner.(IStream); check {
  120. store.SetAuth(auth)
  121. }
  122. }
  123. func (s *RequestIn) GenerateToken(data json.Map, expire int64) (string, error) {
  124. return s.GeneratorToken(data, expire)
  125. }
  126. func (s *RequestIn) ClientData(key string) (any, bool) {
  127. if store, check := s.Owner.(IStream); check {
  128. return store.ClientData(key)
  129. }
  130. return nil, false
  131. }
  132. func (s *RequestIn) SetClientData(key string, data any) {
  133. if store, check := s.Owner.(IStream); check {
  134. store.SetClientData(key, data)
  135. }
  136. }
  137. func (s *RequestIn) ROwner() IOwner {
  138. return s.Owner
  139. }
  140. func (s *RequestIn) RCore() any {
  141. return s.Core
  142. }
  143. func (s *RequestIn) OutSuccess(data json.Map, files RequestFiles) IRequestOut {
  144. return &RequestOut{
  145. Request: &Request{
  146. Type: RequestTypeOut,
  147. Command: s.RCommand(),
  148. Data: data,
  149. Files: files,
  150. },
  151. }
  152. }
  153. func (s *RequestIn) OutError(err IErrorArgs) IRequestOut {
  154. return &RequestOut{
  155. Err: err,
  156. }
  157. }
  158. /////////////////////////////////////////////////////////
  159. type IRequestOut interface {
  160. IRequest
  161. Write(io.Writer) IErrorArgs
  162. }
  163. /////////////////////////////////////////////////////////
  164. type RequestOut struct {
  165. *Request
  166. Err IErrorArgs
  167. }
  168. func (s *RequestOut) Write(w io.Writer) IErrorArgs {
  169. return nil
  170. }