request.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. ////////////////////////////////////////////////////////////
  70. type IRequestIn interface {
  71. IRequest
  72. Auth() json.Map
  73. SetAuth(json.Map)
  74. GenerateToken(data json.Map, expire int64) (string, error)
  75. ROwner() IOwner
  76. RCore() any
  77. OutSuccess(data json.Map, files RequestFiles) IRequestOut
  78. OutError(err IErrorArgs) IRequestOut
  79. ClientData(key string) (any, bool)
  80. SetClientData(key string, data any)
  81. }
  82. type RequestIn struct {
  83. IRequest
  84. Owner IOwner
  85. Core any
  86. GeneratorToken TokenGenerateMethod
  87. }
  88. func (s *RequestIn) Auth() json.Map {
  89. if store, check := s.Owner.(IStream); check {
  90. return store.Auth()
  91. }
  92. return nil
  93. }
  94. func (s *RequestIn) SetAuth(auth json.Map) {
  95. if store, check := s.Owner.(IStream); check {
  96. store.SetAuth(auth)
  97. }
  98. }
  99. func (s *RequestIn) GenerateToken(data json.Map, expire int64) (string, error) {
  100. return s.GeneratorToken(data, expire)
  101. }
  102. func (s *RequestIn) ClientData(key string) (any, bool) {
  103. if store, check := s.Owner.(IStream); check {
  104. return store.ClientData(key)
  105. }
  106. return nil, false
  107. }
  108. func (s *RequestIn) SetClientData(key string, data any) {
  109. if store, check := s.Owner.(IStream); check {
  110. store.SetClientData(key, data)
  111. }
  112. }
  113. func (s *RequestIn) ROwner() IOwner {
  114. return s.Owner
  115. }
  116. func (s *RequestIn) RCore() any {
  117. return s.Core
  118. }
  119. func (s *RequestIn) OutSuccess(data json.Map, files RequestFiles) IRequestOut {
  120. return &RequestOut{
  121. Request: &Request{
  122. Type: RequestTypeOut,
  123. Command: s.RCommand(),
  124. Data: data,
  125. Files: files,
  126. },
  127. }
  128. }
  129. func (s *RequestIn) OutError(err IErrorArgs) IRequestOut {
  130. return &RequestOut{
  131. Err: err,
  132. }
  133. }
  134. /////////////////////////////////////////////////////////
  135. type IRequestOut interface {
  136. IRequest
  137. Write(io.Writer) IErrorArgs
  138. }
  139. /////////////////////////////////////////////////////////
  140. type RequestOut struct {
  141. *Request
  142. Err IErrorArgs
  143. }
  144. func (s *RequestOut) Write(w io.Writer) IErrorArgs {
  145. return nil
  146. }