github.com/yaegashi/msgraph.go@v0.1.4/gen/templates/extensions.go (about) 1 // +build templates 2 3 // This file is a part of msgraph.go/gen/templates. 4 // Anything until the first appearance of "// BEGIN" line will be ignored. 5 6 package msgraph 7 8 import ( 9 "context" 10 "fmt" 11 "net/url" 12 "strings" 13 ) 14 15 // BEGIN - everything below this line will be copied to the output 16 17 // ItemWithPath returns DriveItemRequestBuilder addressed by relative path 18 func (b *DriveItemRequestBuilder) ItemWithPath(path string) *DriveItemRequestBuilder { 19 bb := &DriveItemRequestBuilder{BaseRequestBuilder: b.BaseRequestBuilder} 20 if len(path) == 0 || path[0] != '/' { 21 path = "/" + path 22 } 23 bb.baseURL += ":" + path + ":" 24 return bb 25 } 26 27 // GetByPath returns SiteRequestBuilder addressed by hostname and path 28 func (b *GraphServiceSitesCollectionRequestBuilder) GetByPath(hostname, path string) *SiteRequestBuilder { 29 bb := &SiteRequestBuilder{BaseRequestBuilder: b.BaseRequestBuilder} 30 if len(path) == 0 || path[0] != '/' { 31 path = "/" + path 32 } 33 bb.baseURL += "/" + hostname + ":" + path + ":" 34 return bb 35 } 36 37 // GetDriveItemByURL returns DriveItemRequestBuilder addressed by URL 38 func (b *GraphServiceRequestBuilder) GetDriveItemByURL(ctx context.Context, itemURL string) (*DriveItemRequestBuilder, error) { 39 u, err := url.Parse(itemURL) 40 if err != nil { 41 return nil, err 42 } 43 u.RawQuery = "" 44 itemURL = u.String() 45 var site *Site 46 segments := strings.Split(u.Path, "/") 47 for i := 3; i <= len(segments); i++ { 48 site, err = b.Sites().GetByPath(u.Hostname(), strings.Join(segments[:i], "/")).Request().Get(ctx) 49 if err == nil { 50 break 51 } 52 } 53 if site == nil { 54 return nil, fmt.Errorf("Site for %s not found", itemURL) 55 } 56 drives, err := b.Sites().ID(*site.ID).Drives().Request().Get(ctx) 57 if err != nil { 58 return nil, err 59 } 60 for _, drive := range drives { 61 if strings.HasPrefix(itemURL, *drive.WebURL) { 62 path := itemURL[len(*drive.WebURL):] 63 return b.Drives().ID(*drive.ID).Root().ItemWithPath(path), nil 64 } 65 } 66 return nil, fmt.Errorf("DriveItem for %s not found", itemURL) 67 }