|
| 1 | +package pub |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "github.com/awakari/int-bluesky/model" |
| 9 | + "github.com/bytedance/sonic" |
| 10 | + "github.com/cloudevents/sdk-go/binding/format/protobuf/v2/pb" |
| 11 | + "io" |
| 12 | + "net/http" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +type Service interface { |
| 17 | + Publish(ctx context.Context, evt *pb.CloudEvent, groupId, userId string) (err error) |
| 18 | +} |
| 19 | + |
| 20 | +type service struct { |
| 21 | + clientHttp *http.Client |
| 22 | + url string |
| 23 | + token string |
| 24 | + timeout time.Duration |
| 25 | +} |
| 26 | + |
| 27 | +type payloadResp struct { |
| 28 | + AckCount uint32 `json:"ackCount"` |
| 29 | +} |
| 30 | + |
| 31 | +const valContentTypeJson = "application/json" |
| 32 | + |
| 33 | +var ErrNoAck = errors.New("publishing is not acknowledged") |
| 34 | +var ErrNoAuth = errors.New("unauthenticated request") |
| 35 | +var ErrInvalid = errors.New("invalid request") |
| 36 | +var ErrLimitReached = errors.New("publishing limit reached") |
| 37 | + |
| 38 | +func NewService(clientHttp *http.Client, url, token string, timeout time.Duration) Service { |
| 39 | + return service{ |
| 40 | + clientHttp: clientHttp, |
| 41 | + url: url, |
| 42 | + token: token, |
| 43 | + timeout: timeout, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (svc service) Publish(ctx context.Context, evt *pb.CloudEvent, groupId, userId string) (err error) { |
| 48 | + |
| 49 | + var reqData []byte |
| 50 | + reqData, err = MarshalEvent(evt) |
| 51 | + |
| 52 | + var req *http.Request |
| 53 | + if err == nil { |
| 54 | + ctxTimeout, cancel := context.WithTimeout(ctx, svc.timeout) |
| 55 | + defer cancel() |
| 56 | + req, err = http.NewRequestWithContext(ctxTimeout, http.MethodPost, svc.url, bytes.NewReader(reqData)) |
| 57 | + } |
| 58 | + |
| 59 | + var resp *http.Response |
| 60 | + if err == nil { |
| 61 | + req.Header.Add("Accept", valContentTypeJson) |
| 62 | + req.Header.Add("Authorization", "Bearer "+svc.token) |
| 63 | + req.Header.Add("Content-Type", valContentTypeJson) |
| 64 | + req.Header.Add(model.KeyGroupId, groupId) |
| 65 | + req.Header.Add(model.KeyUserId, userId) |
| 66 | + resp, err = svc.clientHttp.Do(req) |
| 67 | + } |
| 68 | + |
| 69 | + if err == nil { |
| 70 | + switch resp.StatusCode { |
| 71 | + case http.StatusServiceUnavailable: |
| 72 | + err = fmt.Errorf("%w: %s", ErrNoAck, evt.Id) |
| 73 | + case http.StatusUnauthorized: |
| 74 | + err = ErrNoAuth |
| 75 | + case http.StatusRequestTimeout: |
| 76 | + err = fmt.Errorf("%w: %s", ErrNoAck, evt.Id) |
| 77 | + case http.StatusBadRequest: |
| 78 | + err = fmt.Errorf("%w: %s", ErrInvalid, evt.Id) |
| 79 | + case http.StatusTooManyRequests: |
| 80 | + err = fmt.Errorf("%w: %s", ErrLimitReached, evt.Id) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + var respData []byte |
| 85 | + if err == nil { |
| 86 | + defer resp.Body.Close() |
| 87 | + respData, err = io.ReadAll(resp.Body) |
| 88 | + } |
| 89 | + |
| 90 | + var p payloadResp |
| 91 | + if err == nil { |
| 92 | + err = sonic.Unmarshal(respData, &p) |
| 93 | + } |
| 94 | + |
| 95 | + if err == nil && p.AckCount < 1 { |
| 96 | + err = fmt.Errorf("%w: %s", ErrNoAck, evt.Id) |
| 97 | + } |
| 98 | + |
| 99 | + return |
| 100 | +} |
0 commit comments