request.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package ami
  2. import (
  3. "fmt"
  4. "git.ali33.ru/fcg-xvii/go-tools/json"
  5. )
  6. func InitRequest(action string) Request {
  7. return Request{
  8. ActionData: ActionData{
  9. "Action": action,
  10. },
  11. }
  12. }
  13. type Request struct {
  14. ActionData
  15. Variables json.Map
  16. chanResponse chan Response
  17. sended bool
  18. }
  19. func (s *Request) SetParam(key, value string) {
  20. if len(value) > 0 {
  21. s.ActionData[key] = value
  22. }
  23. }
  24. func (s *Request) SetVariable(key, value string) {
  25. if s.Variables == nil {
  26. s.Variables = json.NewMap()
  27. }
  28. s.Variables[key] = value
  29. }
  30. func (s *Request) SetVariables(m json.Map) {
  31. if s.Variables == nil {
  32. s.Variables = json.NewMap()
  33. }
  34. for key, val := range m {
  35. s.Variables[key] = val
  36. }
  37. }
  38. func (s *Request) raw() []byte {
  39. if len(s.Variables) > 0 {
  40. vars, count := "", 0
  41. for key, val := range s.Variables {
  42. vars += fmt.Sprintf("%v=%v", key, val)
  43. if count < len(s.Variables)-1 {
  44. vars += "," // todo 1.5 or lower splitter is '|'
  45. }
  46. count++
  47. }
  48. s.ActionData["Variable"] = vars
  49. }
  50. return s.ActionData.raw()
  51. }