request.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package rest
  2. import (
  3. "io"
  4. "git.ali33.ru/fcg-xvii/go-tools/json"
  5. )
  6. type (
  7. RequestType byte
  8. RequestFiles map[string]IReadCloserLen
  9. TokenGenerateMethod func(data json.Map, expire int64) (string, error)
  10. )
  11. const (
  12. RequestTypeIn RequestType = iota
  13. RequestTypeOut
  14. RequestTypeEvent
  15. )
  16. type IRequest interface {
  17. RType() RequestType
  18. RCommand() string
  19. RData() json.Map
  20. RFiles() RequestFiles
  21. RFile(string) (IReadCloserLen, bool)
  22. RClose()
  23. Fields(any, RequestFiles) (json.Map, IErrorArgs)
  24. }
  25. ////////////////////////////////////////////
  26. type Request struct {
  27. Type RequestType
  28. Command string
  29. Data json.Map
  30. Files RequestFiles
  31. }
  32. func (s *Request) RType() RequestType {
  33. return s.Type
  34. }
  35. func (s *Request) RCommand() string {
  36. return s.Command
  37. }
  38. func (s *Request) RData() json.Map {
  39. if s.Data == nil {
  40. return json.Map{}
  41. }
  42. return s.Data
  43. }
  44. func (s *Request) RFiles() RequestFiles {
  45. return s.Files
  46. }
  47. func (s *Request) RFile(name string) (IReadCloserLen, bool) {
  48. res, check := s.Files[name]
  49. return res, check
  50. }
  51. func (s *Request) RClose() {
  52. for _, file := range s.Files {
  53. file.Close()
  54. }
  55. s.Files = nil
  56. }
  57. func (s *Request) Fields(obj any, files RequestFiles) (json.Map, IErrorArgs) {
  58. return Fields(obj, files, s.Data.Slice("fields", nil)...)
  59. }
  60. ////////////////////////////////////////////////////////////
  61. type IRequestIn interface {
  62. IRequest
  63. Auth() json.Map
  64. SetAuth(json.Map)
  65. GenerateToken(data json.Map, expire int64) (string, error)
  66. ROwner() IOwner
  67. RCore() any
  68. OutSuccess(data json.Map, files RequestFiles) IRequestOut
  69. OutError(err IErrorArgs) IRequestOut
  70. ClientData(key string) (any, bool)
  71. SetClientData(key string, data any)
  72. }
  73. type RequestIn struct {
  74. IRequest
  75. Owner IOwner
  76. Core any
  77. GeneratorToken TokenGenerateMethod
  78. }
  79. func (s *RequestIn) Auth() json.Map {
  80. if store, check := s.Owner.(IStream); check {
  81. return store.Auth()
  82. }
  83. return nil
  84. }
  85. func (s *RequestIn) SetAuth(auth json.Map) {
  86. if store, check := s.Owner.(IStream); check {
  87. store.SetAuth(auth)
  88. }
  89. }
  90. func (s *RequestIn) GenerateToken(data json.Map, expire int64) (string, error) {
  91. return s.GeneratorToken(data, expire)
  92. }
  93. func (s *RequestIn) ClientData(key string) (any, bool) {
  94. if store, check := s.Owner.(IStream); check {
  95. return store.ClientData(key)
  96. }
  97. return nil, false
  98. }
  99. func (s *RequestIn) SetClientData(key string, data any) {
  100. if store, check := s.Owner.(IStream); check {
  101. store.SetClientData(key, data)
  102. }
  103. }
  104. func (s *RequestIn) ROwner() IOwner {
  105. return s.Owner
  106. }
  107. func (s *RequestIn) RCore() any {
  108. return s.Core
  109. }
  110. /////////////////////////////////////////////////////////
  111. type IRequestOut interface {
  112. IRequest
  113. Write(io.Writer) IErrorArgs
  114. }