Skip to content

Commit

Permalink
Move SendMail method into graphClient
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidArchibald committed Sep 2, 2021
1 parent a2b6df6 commit 9905cba
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 62 deletions.
26 changes: 26 additions & 0 deletions GraphClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,32 @@ func (g *GraphClient) CreateUser(userInput User, opts ...CreateQueryOption) (Use
return user, err
}

// SendMail sends a message.
//
// See https://docs.microsoft.com/en-us/graph/api/user-sendmail
func (g *GraphClient) SendMail(userID string, message Message, saveToSentItems bool) error {
if g == nil {
return ErrNotGraphClientSourced
}

bodyBytes, err := json.Marshal(struct {
Message Message `json:"message,omitempty"`
SaveToSentItems bool `json:"saveToSentItems,omitempty"`
}{
message,
saveToSentItems,
})
if err != nil {
return fmt.Errorf("could not marshal message body: %w", err)
}

reader := bytes.NewReader(bodyBytes)

resource := fmt.Sprintf("/users/%v/sendMail", userID)

return g.makePOSTAPICall(resource, compileCreateQueryOptions(nil), reader, nil)
}

// UnmarshalJSON implements the json unmarshal to be used by the json-library.
// This method additionally to loading the TenantID, ApplicationID and ClientSecret
// immediately gets a Token from msgraph (hence initialize this GraphAPI instance)
Expand Down
27 changes: 0 additions & 27 deletions Message.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,4 @@ type Message struct {
Extensions []Extension `json:"extensions,omitempty"`
MultiValueExtendedProperties []MultiValueLegacyExtendedProperty `json:"multiValueExtendedProperties,omitempty"`
SingleValueExtendedProperties []SingleValueLegacyExtendedProperty `json:"singleValueExtendedProperties,omitempty"`

graphClient *GraphClient
}

// setGraphClient sets the graphClient instance in this instance and all child-instances (if any)
func (m *Message) setGraphClient(graphClient *GraphClient) {
m.graphClient = graphClient

for _, bccRecipient := range m.BCCRecipients {
bccRecipient.setGraphClient(graphClient)
}

for _, ccRecipient := range m.CCRecipients {
ccRecipient.setGraphClient(graphClient)
}

m.From.setGraphClient(graphClient)

for _, replyToRecipient := range m.ReplyTo {
replyToRecipient.setGraphClient(graphClient)
}

m.Sender.setGraphClient(graphClient)

for _, toRecipient := range m.ToRecipients {
toRecipient.setGraphClient(graphClient)
}
}
9 changes: 0 additions & 9 deletions Messages.go

This file was deleted.

26 changes: 0 additions & 26 deletions User.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,32 +105,6 @@ func (u User) ListCalendarView(startDateTime, endDateTime time.Time, opts ...Lis
return calendarEvents, u.graphClient.makeGETAPICall(resource, reqOpt, &calendarEvents)
}

// SendMail sends a message.
//
// See https://docs.microsoft.com/en-us/graph/api/user-sendmail
func (u User) SendMail(message Message, saveToSentItems bool) error {
if u.graphClient == nil {
return ErrNotGraphClientSourced
}

bodyBytes, err := json.Marshal(struct {
Message Message `json:"message,omitempty"`
SaveToSentItems bool `json:"saveToSentItems,omitempty"`
}{
message,
saveToSentItems,
})
if err != nil {
return fmt.Errorf("could not marshal message body: %w", err)
}

reader := bytes.NewReader(bodyBytes)

resource := fmt.Sprintf("/users/%v/sendMail", u.ID)

return u.graphClient.makePOSTAPICall(resource, nil, reader, nil)
}

// getTimeZoneChoices grabs all supported time zones from microsoft for this user.
// This should actually be the same for every user. Only used internally by this
// msgraph package.
Expand Down

0 comments on commit 9905cba

Please sign in to comment.