github.com/yaegashi/msgraph.go@v0.1.4/beta/extensions.go (about)

     1  // Code generated by msgraph.go/gen DO NOT EDIT.
     2  
     3  package msgraph
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"net/url"
     9  	"strings"
    10  )
    11  
    12  // ItemWithPath returns DriveItemRequestBuilder addressed by relative path
    13  func (b *DriveItemRequestBuilder) ItemWithPath(path string) *DriveItemRequestBuilder {
    14  	bb := &DriveItemRequestBuilder{BaseRequestBuilder: b.BaseRequestBuilder}
    15  	if len(path) == 0 || path[0] != '/' {
    16  		path = "/" + path
    17  	}
    18  	bb.baseURL += ":" + path + ":"
    19  	return bb
    20  }
    21  
    22  // GetByPath returns SiteRequestBuilder addressed by hostname and path
    23  func (b *GraphServiceSitesCollectionRequestBuilder) GetByPath(hostname, path string) *SiteRequestBuilder {
    24  	bb := &SiteRequestBuilder{BaseRequestBuilder: b.BaseRequestBuilder}
    25  	if len(path) == 0 || path[0] != '/' {
    26  		path = "/" + path
    27  	}
    28  	bb.baseURL += "/" + hostname + ":" + path + ":"
    29  	return bb
    30  }
    31  
    32  // GetDriveItemByURL returns DriveItemRequestBuilder addressed by URL
    33  func (b *GraphServiceRequestBuilder) GetDriveItemByURL(ctx context.Context, itemURL string) (*DriveItemRequestBuilder, error) {
    34  	u, err := url.Parse(itemURL)
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  	u.RawQuery = ""
    39  	itemURL = u.String()
    40  	var site *Site
    41  	segments := strings.Split(u.Path, "/")
    42  	for i := 3; i <= len(segments); i++ {
    43  		site, err = b.Sites().GetByPath(u.Hostname(), strings.Join(segments[:i], "/")).Request().Get(ctx)
    44  		if err == nil {
    45  			break
    46  		}
    47  	}
    48  	if site == nil {
    49  		return nil, fmt.Errorf("Site for %s not found", itemURL)
    50  	}
    51  	drives, err := b.Sites().ID(*site.ID).Drives().Request().Get(ctx)
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	for _, drive := range drives {
    56  		if strings.HasPrefix(itemURL, *drive.WebURL) {
    57  			path := itemURL[len(*drive.WebURL):]
    58  			return b.Drives().ID(*drive.ID).Root().ItemWithPath(path), nil
    59  		}
    60  	}
    61  	return nil, fmt.Errorf("DriveItem for %s not found", itemURL)
    62  }