request.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. }
  28. ////////////////////////////////////////////
  29. type Request struct {
  30. Type RequestType
  31. Command string
  32. Data json.Map
  33. Files RequestFiles
  34. }
  35. func (s *Request) RType() RequestType {
  36. return s.Type
  37. }
  38. func (s *Request) RCommand() string {
  39. return s.Command
  40. }
  41. func (s *Request) RData() json.Map {
  42. if s.Data == nil {
  43. return json.Map{}
  44. }
  45. return s.Data
  46. }
  47. func (s *Request) RFiles() RequestFiles {
  48. return s.Files
  49. }
  50. func (s *Request) RFile(name string) (IReadCloserLen, bool) {
  51. res, check := s.Files[name]
  52. return res, check
  53. }
  54. func (s *Request) RClose() {
  55. for _, file := range s.Files {
  56. file.Close()
  57. }
  58. s.Files = nil
  59. }
  60. func (s *Request) Fields(obj any, files RequestFiles) (json.Map, IErrorArgs) {
  61. return Fields(obj, files, s.Data.Slice("fields", nil)...)
  62. }
  63. func (s *Request) ParseOffset() int {
  64. return s.Data.Int32("offset", 0)
  65. }
  66. func (s *Request) ParseLimit() int {
  67. return s.Data.Int32("limit", 20)
  68. }
  69. func (s *Request) ParseOrder() (IOrderObject, IErrorArgs) {
  70. res, err := parseOrder(
  71. s.Data.Map("order", json.Map{}),
  72. "",
  73. )
  74. if err != nil {
  75. return nil, ErrorFiled("order", err.Error())
  76. }
  77. return res, nil
  78. }
  79. func (s *Request) ParseConditions() (res []ICondition, rErr IErrorArgs) {
  80. s.Data.EachMap("conditions", func(index int, m json.Map) bool {
  81. cond, err := parseCondition(m, index)
  82. if err != nil {
  83. rErr = ErrorFiled("conditions", err.Error())
  84. return false
  85. }
  86. res = append(res, cond)
  87. return true
  88. })
  89. return
  90. }
  91. ////////////////////////////////////////////////////////////
  92. type IRequestIn interface {
  93. IRequest
  94. Auth() json.Map
  95. SetAuth(json.Map)
  96. GenerateToken(data json.Map, expire int64) (string, error)
  97. ROwner() IOwner
  98. RCore() any
  99. OutSuccess(data json.Map, files RequestFiles) IRequestOut
  100. OutError(err IErrorArgs) IRequestOut
  101. ClientData(key string) (any, bool)
  102. SetClientData(key string, data any)
  103. }
  104. type RequestIn struct {
  105. IRequest
  106. Owner IOwner
  107. Core any
  108. GeneratorToken TokenGenerateMethod
  109. }
  110. func (s *RequestIn) Auth() json.Map {
  111. if store, check := s.Owner.(IStream); check {
  112. return store.Auth()
  113. }
  114. return nil
  115. }
  116. func (s *RequestIn) SetAuth(auth json.Map) {
  117. if store, check := s.Owner.(IStream); check {
  118. store.SetAuth(auth)
  119. }
  120. }
  121. func (s *RequestIn) GenerateToken(data json.Map, expire int64) (string, error) {
  122. return s.GeneratorToken(data, expire)
  123. }
  124. func (s *RequestIn) ClientData(key string) (any, bool) {
  125. if store, check := s.Owner.(IStream); check {
  126. return store.ClientData(key)
  127. }
  128. return nil, false
  129. }
  130. func (s *RequestIn) SetClientData(key string, data any) {
  131. if store, check := s.Owner.(IStream); check {
  132. store.SetClientData(key, data)
  133. }
  134. }
  135. func (s *RequestIn) ROwner() IOwner {
  136. return s.Owner
  137. }
  138. func (s *RequestIn) RCore() any {
  139. return s.Core
  140. }
  141. func (s *RequestIn) OutSuccess(data json.Map, files RequestFiles) IRequestOut {
  142. return &RequestOut{
  143. Request: &Request{
  144. Type: RequestTypeOut,
  145. Command: s.RCommand(),
  146. Data: data,
  147. Files: files,
  148. },
  149. }
  150. }
  151. func (s *RequestIn) OutError(err IErrorArgs) IRequestOut {
  152. return &RequestOut{
  153. Err: err,
  154. }
  155. }
  156. /////////////////////////////////////////////////////////
  157. type IRequestOut interface {
  158. IRequest
  159. Write(io.Writer) IErrorArgs
  160. }
  161. /////////////////////////////////////////////////////////
  162. type RequestOut struct {
  163. *Request
  164. Err IErrorArgs
  165. }
  166. func (s *RequestOut) Write(w io.Writer) IErrorArgs {
  167. return nil
  168. }