Skip to content

Commit

Permalink
Merge pull request #5 from pusher/add-sender
Browse files Browse the repository at this point in the history
Add instance info call to FCM client
  • Loading branch information
fbenevides authored Apr 8, 2024
2 parents 0fd934d + 7d63796 commit 176a1dd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
.idea/
main/
47 changes: 43 additions & 4 deletions fcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

const (
BaseURL = "https://fcm.googleapis.com/v1"
InstanceIdApiUrl = "https://iid.googleapis.com/iid/info"
FirebaseRequestScope = "https://www.googleapis.com/auth/firebase.messaging"
DefaultContentType = "application/json"
)
Expand Down Expand Up @@ -64,27 +65,65 @@ func NewFcmClient(serviceAccountFileContent string, ctx context.Context) (*fcmCl
}, nil
}

func (c *fcmClient) GetInstanceInfo(token string) (*InstanceInformationResponse, error) {
getSenderIdUrl := fmt.Sprintf("%s/%s", InstanceIdApiUrl, token)

response, err := c.httpClient.Get(getSenderIdUrl)
if err != nil {
return nil, fmt.Errorf("error making a http request to %s > %v", getSenderIdUrl, err)
}
defer response.Body.Close()

switch response.StatusCode {
case 401, 403:
return nil, fmt.Errorf("invalid FCM Service Account File")
case 404:
return nil, fmt.Errorf("device not found")
}

if response.StatusCode > 299 {
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}

responseBodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return &InstanceInformationResponse{}, fmt.Errorf("failed to read response body > %v", err)
}

responseBody := &InstanceInformationResponse{}
err = json.Unmarshal(responseBodyBytes, &responseBody)
if err != nil {
return nil, err
}

return responseBody, nil
}

func (c *fcmClient) Send(m FcmMessageBody) (*FcmSendHttpResponse, error) {
sendURL := fmt.Sprintf("%s/projects/%s/messages:send",
BaseURL, c.ServiceAccountConfig.ProjectId,
)

body, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("error unmarshaling message>%v", err)
return nil, fmt.Errorf("error marshaling message>%v", err)
}

resp, err := c.httpClient.Post(sendURL, DefaultContentType, bytes.NewReader(body))
if err != nil {
return nil, fmt.Errorf("error sending request to HTTP connection server>%v", err)
}

fcmResp := &FcmSendHttpResponse{Status: resp.StatusCode}
responseBody, _ := io.ReadAll(resp.Body)
defer resp.Body.Close()
fcmResp := &FcmSendHttpResponse{Status: resp.StatusCode}
responseBody, err := io.ReadAll(resp.Body)

if err != nil {
return nil, fmt.Errorf("failed to read response body > %v", err)
}

if fcmResp.Status != http.StatusOK {
return fcmResp, fmt.Errorf("could not send a message as the server returned: %s", resp.StatusCode)
return fcmResp, fmt.Errorf("could not send a message as the server returned: %d", resp.StatusCode)
}

err = json.Unmarshal(responseBody, &fcmResp)
Expand Down
4 changes: 4 additions & 0 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,7 @@ type FcmSendHttpResponse struct {
Status int `json:"-"`
Name string `json:"name"`
}

type InstanceInformationResponse struct {
AuthorizedEntity string `json:"authorizedEntity"`
}

0 comments on commit 176a1dd

Please sign in to comment.