Spaces:
Paused
Paused
ML Intern deploy
Deploy httpSMS gateway to Hugging Face Spaces (Docker SDK): nginx :7860 -> Go API :8000, Nuxt SPA, Android APK, bootstrap-mode entrypoint
83a59ca Download api/pkg/services/emulator_fcm_client.go from red-kit/httpsms-gateway: direct link, hf CLI and curl.
- Browser
- Download file 2.85 kB
-
https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/main/api/pkg/services/emulator_fcm_client.go
- Command line
-
hf download hf://spaces/red-kit/httpsms-gateway/api/pkg/services/emulator_fcm_client.go
-
curl -L -o emulator_fcm_client.go https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/main/api/pkg/services/emulator_fcm_client.go
2.85 kB
| package services | |
| import ( | |
| "bytes" | |
| "context" | |
| "encoding/json" | |
| "fmt" | |
| "io" | |
| "net/http" | |
| "firebase.google.com/go/messaging" | |
| "github.com/NdoleStudio/httpsms/pkg/telemetry" | |
| "github.com/NdoleStudio/stacktrace" | |
| "github.com/google/uuid" | |
| ) | |
| // EmulatorFCMClient sends FCM messages to the phone emulator via HTTP. | |
| type EmulatorFCMClient struct { | |
| httpClient *http.Client | |
| endpoint string | |
| logger telemetry.Logger | |
| } | |
| // NewEmulatorFCMClient creates a new EmulatorFCMClient. | |
| func NewEmulatorFCMClient(httpClient *http.Client, endpoint string, logger telemetry.Logger) *EmulatorFCMClient { | |
| return &EmulatorFCMClient{ | |
| httpClient: httpClient, | |
| endpoint: endpoint, | |
| logger: logger, | |
| } | |
| } | |
| // emulatorFCMRequest is the payload sent to the emulator's FCM endpoint. | |
| type emulatorFCMRequest struct { | |
| Message *emulatorFCMMessage `json:"message"` | |
| } | |
| type emulatorFCMMessage struct { | |
| Token string `json:"token"` | |
| Data map[string]string `json:"data,omitempty"` | |
| Android *emulatorAndroid `json:"android,omitempty"` | |
| } | |
| type emulatorAndroid struct { | |
| Priority string `json:"priority,omitempty"` | |
| } | |
| // emulatorFCMResponse is the response from the emulator. | |
| type emulatorFCMResponse struct { | |
| Name string `json:"name"` | |
| } | |
| // Send sends a message to the emulator's FCM endpoint. | |
| func (c *EmulatorFCMClient) Send(ctx context.Context, message *messaging.Message, _ uuid.UUID) (string, error) { | |
| payload := &emulatorFCMRequest{ | |
| Message: &emulatorFCMMessage{ | |
| Token: message.Token, | |
| Data: message.Data, | |
| }, | |
| } | |
| if message.Android != nil { | |
| payload.Message.Android = &emulatorAndroid{ | |
| Priority: message.Android.Priority, | |
| } | |
| } | |
| body, err := json.Marshal(payload) | |
| if err != nil { | |
| return "", stacktrace.Propagatef(err, "cannot marshal FCM request for emulator") | |
| } | |
| url := fmt.Sprintf("%s/v1/projects/httpsms-test/messages:send", c.endpoint) | |
| req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) | |
| if err != nil { | |
| return "", stacktrace.Propagatef(err, "cannot create HTTP request for emulator FCM") | |
| } | |
| req.Header.Set("Content-Type", "application/json") | |
| resp, err := c.httpClient.Do(req) | |
| if err != nil { | |
| return "", stacktrace.Propagatef(err, "cannot send FCM to emulator at [%s]", url) | |
| } | |
| defer resp.Body.Close() | |
| respBody, err := io.ReadAll(resp.Body) | |
| if err != nil { | |
| return "", stacktrace.Propagatef(err, "cannot read emulator FCM response body") | |
| } | |
| if resp.StatusCode != http.StatusOK { | |
| return "", stacktrace.NewErrorf("emulator FCM returned status %d: %s", resp.StatusCode, string(respBody)) | |
| } | |
| var result emulatorFCMResponse | |
| if err = json.Unmarshal(respBody, &result); err != nil { | |
| return "", stacktrace.Propagatef(err, "cannot decode emulator FCM response") | |
| } | |
| c.logger.Info(fmt.Sprintf("emulator FCM sent successfully: %s", result.Name)) | |
| return result.Name, nil | |
| } | |