request.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // TokenParseMethod - функция для расшифровки токена
  14. TokenParseMethod func(token string) (json.Map, error)
  15. )
  16. const (
  17. RequestTypeIn RequestType = iota
  18. RequestTypeOut
  19. RequestTypeEvent
  20. )
  21. type IRequest interface {
  22. RType() RequestType
  23. RCommand() string
  24. RData() json.Map
  25. RFiles() RequestFiles
  26. RFile(string) (IReadCloserLen, bool)
  27. RClose()
  28. Fields(any, RequestFiles) (json.Map, 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. ////////////////////////////////////////////////////////////
  72. type IRequestIn interface {
  73. IRequest
  74. Auth() json.Map
  75. SetAuth(json.Map)
  76. GenerateToken(data json.Map, expire int64) (string, error)
  77. ParseToken(token string) (json.Map, error)
  78. ROwner() IOwner
  79. RCore() any
  80. OutSuccess(data json.Map, files RequestFiles) IRequestOut
  81. OutError(err IErrorArgs) IRequestOut
  82. ClientData(key string) (any, bool)
  83. SetClientData(key string, data any)
  84. }
  85. type RequestIn struct {
  86. IRequest
  87. Owner IOwner
  88. Core any
  89. GeneratorToken TokenGenerateMethod
  90. ParserToken TokenParseMethod
  91. }
  92. func (s *RequestIn) Auth() json.Map {
  93. if store, check := s.Owner.(IStream); check {
  94. return store.Auth()
  95. }
  96. return nil
  97. }
  98. func (s *RequestIn) SetAuth(auth json.Map) {
  99. if store, check := s.Owner.(IStream); check {
  100. store.SetAuth(auth)
  101. }
  102. }
  103. func (s *RequestIn) GenerateToken(data json.Map, expire int64) (string, error) {
  104. return s.GeneratorToken(data, expire)
  105. }
  106. func (s *RequestIn) ParseToken(token string) (json.Map, error) {
  107. return s.ParserToken(token)
  108. }
  109. func (s *RequestIn) ClientData(key string) (any, bool) {
  110. if store, check := s.Owner.(IStream); check {
  111. return store.ClientData(key)
  112. }
  113. return nil, false
  114. }
  115. func (s *RequestIn) SetClientData(key string, data any) {
  116. if store, check := s.Owner.(IStream); check {
  117. store.SetClientData(key, data)
  118. }
  119. }
  120. func (s *RequestIn) ROwner() IOwner {
  121. return s.Owner
  122. }
  123. func (s *RequestIn) RCore() any {
  124. return s.Core
  125. }
  126. func (s *RequestIn) OutSuccess(data json.Map, files RequestFiles) IRequestOut {
  127. return &RequestOut{
  128. Request: &Request{
  129. Type: RequestTypeOut,
  130. Command: s.RCommand(),
  131. Data: data,
  132. Files: files,
  133. },
  134. }
  135. }
  136. func (s *RequestIn) OutError(err IErrorArgs) IRequestOut {
  137. return &RequestOut{
  138. Err: err,
  139. }
  140. }
  141. /////////////////////////////////////////////////////////
  142. type IRequestOut interface {
  143. IRequest
  144. Write(io.Writer) IErrorArgs
  145. }
  146. /////////////////////////////////////////////////////////
  147. type RequestOut struct {
  148. *Request
  149. Err IErrorArgs
  150. }
  151. func (s *RequestOut) Write(w io.Writer) IErrorArgs {
  152. return nil
  153. }