|
| 1 | +package services |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "firebase.google.com/go/messaging" |
| 12 | + "github.com/NdoleStudio/httpsms/pkg/telemetry" |
| 13 | + "github.com/palantir/stacktrace" |
| 14 | +) |
| 15 | + |
| 16 | +// EmulatorFCMClient sends FCM messages to the phone emulator via HTTP. |
| 17 | +type EmulatorFCMClient struct { |
| 18 | + httpClient *http.Client |
| 19 | + endpoint string |
| 20 | + logger telemetry.Logger |
| 21 | +} |
| 22 | + |
| 23 | +// NewEmulatorFCMClient creates a new EmulatorFCMClient. |
| 24 | +func NewEmulatorFCMClient(httpClient *http.Client, endpoint string, logger telemetry.Logger) *EmulatorFCMClient { |
| 25 | + return &EmulatorFCMClient{ |
| 26 | + httpClient: httpClient, |
| 27 | + endpoint: endpoint, |
| 28 | + logger: logger, |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// emulatorFCMRequest is the payload sent to the emulator's FCM endpoint. |
| 33 | +type emulatorFCMRequest struct { |
| 34 | + Message *emulatorFCMMessage `json:"message"` |
| 35 | +} |
| 36 | + |
| 37 | +type emulatorFCMMessage struct { |
| 38 | + Token string `json:"token"` |
| 39 | + Data map[string]string `json:"data,omitempty"` |
| 40 | + Android *emulatorAndroid `json:"android,omitempty"` |
| 41 | +} |
| 42 | + |
| 43 | +type emulatorAndroid struct { |
| 44 | + Priority string `json:"priority,omitempty"` |
| 45 | +} |
| 46 | + |
| 47 | +// emulatorFCMResponse is the response from the emulator. |
| 48 | +type emulatorFCMResponse struct { |
| 49 | + Name string `json:"name"` |
| 50 | +} |
| 51 | + |
| 52 | +// Send sends a message to the emulator's FCM endpoint. |
| 53 | +func (c *EmulatorFCMClient) Send(ctx context.Context, message *messaging.Message) (string, error) { |
| 54 | + payload := &emulatorFCMRequest{ |
| 55 | + Message: &emulatorFCMMessage{ |
| 56 | + Token: message.Token, |
| 57 | + Data: message.Data, |
| 58 | + }, |
| 59 | + } |
| 60 | + if message.Android != nil { |
| 61 | + payload.Message.Android = &emulatorAndroid{ |
| 62 | + Priority: message.Android.Priority, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + body, err := json.Marshal(payload) |
| 67 | + if err != nil { |
| 68 | + return "", stacktrace.Propagate(err, "cannot marshal FCM request for emulator") |
| 69 | + } |
| 70 | + |
| 71 | + url := fmt.Sprintf("%s/v1/projects/httpsms-test/messages:send", c.endpoint) |
| 72 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) |
| 73 | + if err != nil { |
| 74 | + return "", stacktrace.Propagate(err, "cannot create HTTP request for emulator FCM") |
| 75 | + } |
| 76 | + req.Header.Set("Content-Type", "application/json") |
| 77 | + |
| 78 | + resp, err := c.httpClient.Do(req) |
| 79 | + if err != nil { |
| 80 | + return "", stacktrace.Propagate(err, fmt.Sprintf("cannot send FCM to emulator at [%s]", url)) |
| 81 | + } |
| 82 | + defer resp.Body.Close() |
| 83 | + |
| 84 | + respBody, err := io.ReadAll(resp.Body) |
| 85 | + if err != nil { |
| 86 | + return "", stacktrace.Propagate(err, "cannot read emulator FCM response body") |
| 87 | + } |
| 88 | + |
| 89 | + if resp.StatusCode != http.StatusOK { |
| 90 | + return "", stacktrace.NewError("emulator FCM returned status %d: %s", resp.StatusCode, string(respBody)) |
| 91 | + } |
| 92 | + |
| 93 | + var result emulatorFCMResponse |
| 94 | + if err = json.Unmarshal(respBody, &result); err != nil { |
| 95 | + return "", stacktrace.Propagate(err, "cannot decode emulator FCM response") |
| 96 | + } |
| 97 | + |
| 98 | + c.logger.Info(fmt.Sprintf("emulator FCM sent successfully: %s", result.Name)) |
| 99 | + return result.Name, nil |
| 100 | +} |
0 commit comments