Skip to content

Commit

Permalink
Added User service (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
VishishtK authored Apr 21, 2023
1 parent e8ad009 commit 8d068e2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions onedrive/driveitems.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type DriveItem struct {
Id string `json:"id"`
DownloadURL string `json:"@microsoft.graph.downloadUrl"`
Description string `json:"description"`
Size int64 `json:"size"`
WebURL string `json:"webUrl"`
Audio *OneDriveAudio `json:"audio"`
Video *OneDriveVideo `json:"video"`
Expand Down
2 changes: 2 additions & 0 deletions onedrive/onedrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Client struct {
common service // Reuse a single struct instead of allocating one for each service on the heap.

// Services used for talking to different parts of the OneDrive API.
User *UserService
Drives *DrivesService
DriveItems *DriveItemsService
DriveSearch *DriveSearchService
Expand All @@ -58,6 +59,7 @@ func NewClient(httpClient *http.Client) *Client {

c.common.client = c

c.User = (*UserService)(&c.common)
c.Drives = (*DrivesService)(&c.common)
c.DriveItems = (*DriveItemsService)(&c.common)
c.DriveSearch = (*DriveSearchService)(&c.common)
Expand Down
26 changes: 26 additions & 0 deletions onedrive/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,34 @@

package onedrive

import (
"context"
)

type UserService service

// User represents an user in Microsoft Live.
type User struct {
Id string `json:"id"`
DisplayName string `json:"displayName"`
Email string `json:"mail"`

}

// OneDrive API docs: https://learn.microsoft.com/en-us/graph/api/user-get?view=graph-rest-1.0&tabs=http
func (s *UserService) GetCurrentUserDetails(ctx context.Context) (*User, error) {
apiURL := "me"

req, err := s.client.NewRequest("GET", apiURL, nil)
if err != nil {
return nil, err
}

var oneDriveResponse *User
err = s.client.Do(ctx, req, false, &oneDriveResponse)
if err != nil {
return nil, err
}

return oneDriveResponse, nil
}

0 comments on commit 8d068e2

Please sign in to comment.