request.go 3.3 KB

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