request.go 3.6 KB

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