client.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package dapr_client
  2. import (
  3. "context"
  4. dapr "github.com/dapr/go-sdk/client"
  5. "os"
  6. "time"
  7. )
  8. const (
  9. daprPortDefault = "50001"
  10. daprPortEnvVarName = "DAPR_GRPC_PORT"
  11. )
  12. type Client struct {
  13. client dapr.Client
  14. timeoutSec int64
  15. }
  16. func newClient(timeoutSec int64) (*Client, error) {
  17. port := os.Getenv(daprPortEnvVarName)
  18. if port == "" {
  19. port = daprPortDefault
  20. }
  21. client, err := dapr.NewClientWithPort(port)
  22. if err != nil {
  23. return nil, err
  24. }
  25. c := new(Client)
  26. c.client = client
  27. c.timeoutSec = timeoutSec
  28. return c, nil
  29. }
  30. func destroyClient(client *Client) {
  31. if client == nil {
  32. return
  33. }
  34. client.client.Close()
  35. client.client = nil
  36. client = nil
  37. }
  38. func (client *Client) InvokeGetMethod(appID string, methodName string) ([]byte, error) {
  39. return client.invokeMethod(appID, methodName, "get")
  40. }
  41. func (client *Client) InvokePostMethodJson(appID string, methodName string, jsonData []byte) ([]byte, error) {
  42. return client.invokeMethodJson(appID, methodName, "post", jsonData)
  43. }
  44. func (client *Client) InvokePutMethodJson(appID string, methodName string, jsonData []byte) ([]byte, error) {
  45. return client.invokeMethodJson(appID, methodName, "put", jsonData)
  46. }
  47. func (client *Client) InvokeDeleteMethod(appID string, methodName string) ([]byte, error) {
  48. return client.invokeMethod(appID, methodName, "delete")
  49. }
  50. func (client *Client) Publish(pubsubName string, topic string, content []byte) error {
  51. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
  52. defer cancel()
  53. return client.client.PublishEvent(ctx, pubsubName, topic, content)
  54. }
  55. func (client *Client) invokeMethod(appID string, methodName string, verb string) ([]byte, error) {
  56. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
  57. defer cancel()
  58. return client.client.InvokeMethod(ctx, appID, methodName, verb)
  59. }
  60. func (client *Client) invokeMethodJson(appID string, methodName string, verb string, jsonData []byte) ([]byte, error) {
  61. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
  62. defer cancel()
  63. return client.client.InvokeMethodWithContent(ctx, appID, methodName, verb,
  64. &dapr.DataContent{
  65. Data: jsonData,
  66. ContentType: "application/json",
  67. })
  68. }
  69. func (client *Client) SaveState(stateStoreName string, key string, value []byte, meta map[string]string, so ...dapr.StateOption) error {
  70. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
  71. defer cancel()
  72. return client.client.SaveState(ctx, stateStoreName, key, value, meta, so...)
  73. }
  74. func (client *Client) DeleteState(stateStoreName string, key string, meta map[string]string) error {
  75. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
  76. defer cancel()
  77. return client.client.DeleteState(ctx, stateStoreName, key, meta)
  78. }
  79. func (client *Client) GetState(stateStoreName string, key string, meta map[string]string) (*dapr.StateItem, error) {
  80. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
  81. defer cancel()
  82. return client.client.GetState(ctx, stateStoreName, key, meta)
  83. }
  84. func (client *Client) ExecuteStateTransaction(stateStoreName string, meta map[string]string, ops []*dapr.StateOperation) error {
  85. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(client.timeoutSec)*time.Second)
  86. defer cancel()
  87. return client.client.ExecuteStateTransaction(ctx, stateStoreName, meta, ops)
  88. }