graph

package
v0.14.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 18, 2023 License: GPL-3.0 Imports: 21 Imported by: 0

Documentation

Overview

Package graph provides the basic APIs to interact with Microsoft Graph. This includes the DriveItem resource and supporting resources which are the basis of working with files and folders through the Microsoft Graph API.

Index

Constants

View Source
const (
	DriveTypePersonal   = "personal"
	DriveTypeBusiness   = "business"
	DriveTypeSharepoint = "documentLibrary"
)

DriveTypePersonal and friends represent the possible different values for a drive's type when fetched from the API.

View Source
const GraphURL = "https://graph.microsoft.com/v1.0"

GraphURL is the API endpoint of Microsoft Graph

Variables

This section is empty.

Functions

func Delete

func Delete(resource string, auth *Auth, headers ...Header) error

Delete performs an HTTP delete

func Get

func Get(resource string, auth *Auth, headers ...Header) ([]byte, error)

Get is a convenience wrapper around Request

func GetItemContent

func GetItemContent(id string, auth *Auth) ([]byte, uint64, error)

GetItemContent retrieves an item's content from the Graph endpoint.

func GetItemContentStream added in v0.14.0

func GetItemContentStream(id string, auth *Auth, output io.Writer) (uint64, error)

GetItemContentStream is the same as GetItemContent, but writes data to an output reader. This function assumes a brand-new io.Writer is used, so "output" must be truncated if there is content already in the io.Writer prior to use.

func IDPath added in v0.12.0

func IDPath(id string) string

IDPath computes the resource path for an item by ID

func IsOffline

func IsOffline(err error) bool

IsOffline checks if an error string from Request() is indicative of being offline.

func Patch

func Patch(resource string, auth *Auth, content io.Reader, headers ...Header) ([]byte, error)

Patch is a convenience wrapper around Request

func Post

func Post(resource string, auth *Auth, content io.Reader, headers ...Header) ([]byte, error)

Post is a convenience wrapper around Request

func Put

func Put(resource string, auth *Auth, content io.Reader, headers ...Header) ([]byte, error)

Put is a convenience wrapper around Request

func QuickXORHash added in v0.9.1

func QuickXORHash(data *[]byte) string

QuickXORHash computes the Microsoft-specific QuickXORHash. Reusing rclone's implementation until I get the chance to rewrite/add test cases to remove the dependency.

func QuickXORHashStream added in v0.14.0

func QuickXORHashStream(reader io.ReadSeeker) string

QuickXORHashStream hashes a stream.

func Remove

func Remove(id string, auth *Auth) error

Remove removes a directory or file by ID

func Rename

func Rename(itemID string, itemName string, parentID string, auth *Auth) error

Rename moves and/or renames an item on the server. The itemName and parentID arguments correspond to the *new* basename or id of the parent.

func Request

func Request(resource string, auth *Auth, method string, content io.Reader, headers ...Header) ([]byte, error)

Request performs an authenticated request to Microsoft Graph

func ResourcePath

func ResourcePath(path string) string

ResourcePath translates an item's path to the proper path used by Graph

func SHA1Hash added in v0.9.1

func SHA1Hash(data *[]byte) string

SHA1Hash returns the SHA1 hash of some data as a string

func SHA1HashStream added in v0.14.0

func SHA1HashStream(reader io.ReadSeeker) string

SHA1HashStream hashes the contents of a stream.

func SHA256Hash added in v0.14.0

func SHA256Hash(data *[]byte) string

func SHA256HashStream added in v0.14.0

func SHA256HashStream(reader io.ReadSeeker) string

Types

type Auth

type Auth struct {
	AuthConfig   `json:"config"`
	Account      string `json:"account"`
	ExpiresIn    int64  `json:"expires_in"` // only used for parsing
	ExpiresAt    int64  `json:"expires_at"`
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
	// contains filtered or unexported fields
}

Auth represents a set of oauth2 authentication tokens

func Authenticate

func Authenticate(config AuthConfig, path string, headless bool) *Auth

Authenticate performs authentication to Graph or load auth/refreshes it from an existing file. If headless is true, we will authenticate in the terminal.

func (*Auth) FromFile

func (a *Auth) FromFile(file string) error

FromFile populates an auth struct from a file

func (*Auth) Refresh

func (a *Auth) Refresh()

Refresh auth tokens if expired.

func (Auth) ToFile

func (a Auth) ToFile(file string) error

ToFile writes auth tokens to a file

type AuthConfig added in v0.13.0

type AuthConfig struct {
	ClientID    string `json:"clientID" yaml:"clientID"`
	CodeURL     string `json:"codeURL" yaml:"codeURL"`
	TokenURL    string `json:"tokenURL" yaml:"tokenURL"`
	RedirectURL string `json:"redirectURL" yaml:"redirectURL"`
}

AuthConfig configures the authentication flow

type AuthError added in v0.9.2

type AuthError struct {
	Error            string `json:"error"`
	ErrorDescription string `json:"error_description"`
	ErrorCodes       []int  `json:"error_codes"`
	ErrorURI         string `json:"error_uri"`
	Timestamp        string `json:"timestamp"` // json.Unmarshal doesn't like this timestamp format
	TraceID          string `json:"trace_id"`
	CorrelationID    string `json:"correlation_id"`
}

AuthError is an authentication error from the Microsoft API. Generally we don't see these unless something goes catastrophically wrong with Microsoft's authentication services.

type Deleted

type Deleted struct {
	State string `json:"state,omitempty"`
}

Deleted is used for detecting when items get deleted on the server https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/deleted

type Drive

type Drive struct {
	ID        string     `json:"id"`
	DriveType string     `json:"driveType"` // personal | business | documentLibrary
	Quota     DriveQuota `json:"quota,omitempty"`
}

Drive has some general information about the user's OneDrive https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/drive

func GetDrive

func GetDrive(auth *Auth) (Drive, error)

GetDrive is used to fetch the details of the user's OneDrive.

type DriveItem

type DriveItem struct {
	ID               string           `json:"id,omitempty"`
	Name             string           `json:"name,omitempty"`
	Size             uint64           `json:"size,omitempty"`
	ModTime          *time.Time       `json:"lastModifiedDatetime,omitempty"`
	Parent           *DriveItemParent `json:"parentReference,omitempty"`
	Folder           *Folder          `json:"folder,omitempty"`
	File             *File            `json:"file,omitempty"`
	Deleted          *Deleted         `json:"deleted,omitempty"`
	ConflictBehavior string           `json:"@microsoft.graph.conflictBehavior,omitempty"`
	ETag             string           `json:"eTag,omitempty"`
}

DriveItem contains the data fields from the Graph API https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/driveitem

func GetItem

func GetItem(id string, auth *Auth) (*DriveItem, error)

GetItem fetches a DriveItem by ID. ID can also be "root" for the root item.

func GetItemChild added in v0.12.0

func GetItemChild(id string, name string, auth *Auth) (*DriveItem, error)

GetItemChild fetches the named child of an item.

func GetItemChildren added in v0.9.1

func GetItemChildren(id string, auth *Auth) ([]*DriveItem, error)

GetItemChildren fetches all children of an item denoted by ID.

func GetItemChildrenPath added in v0.9.1

func GetItemChildrenPath(path string, auth *Auth) ([]*DriveItem, error)

GetItemChildrenPath fetches all children of an item denoted by path.

func GetItemPath

func GetItemPath(path string, auth *Auth) (*DriveItem, error)

GetItemPath fetches a DriveItem by path. Only used in special cases, like for the root item.

func Mkdir

func Mkdir(name string, parentID string, auth *Auth) (*DriveItem, error)

Mkdir creates a directory on the server at the specified parent ID.

func (*DriveItem) ETagIsMatch added in v0.11.0

func (d *DriveItem) ETagIsMatch(etag string) bool

ETagIsMatch returns true if the etag matches the one in the DriveItem

func (*DriveItem) IsDir added in v0.12.0

func (d *DriveItem) IsDir() bool

IsDir returns if the DriveItem represents a directory or not

func (*DriveItem) ModTimeUnix added in v0.12.0

func (d *DriveItem) ModTimeUnix() uint64

ModTimeUnix returns the modification time as a unix uint64 time

func (*DriveItem) VerifyChecksum added in v0.9.1

func (d *DriveItem) VerifyChecksum(checksum string) bool

VerifyChecksum checks to see if a DriveItem's checksum matches what it's supposed to be. This is less of a cryptographic check and more of a file integrity check.

type DriveItemParent

type DriveItemParent struct {
	//TODO Path is technically available, but we shouldn't use it
	Path      string `json:"path,omitempty"`
	ID        string `json:"id,omitempty"`
	DriveID   string `json:"driveId,omitempty"`
	DriveType string `json:"driveType,omitempty"` // personal | business | documentLibrary
}

DriveItemParent describes a DriveItem's parent in the Graph API https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/itemreference

type DriveQuota

type DriveQuota struct {
	Deleted   uint64 `json:"deleted"`   // bytes in recycle bin
	FileCount uint64 `json:"fileCount"` // unavailable on personal accounts
	Remaining uint64 `json:"remaining"`
	State     string `json:"state"` // normal | nearing | critical | exceeded
	Total     uint64 `json:"total"`
	Used      uint64 `json:"used"`
}

DriveQuota is used to parse the User's current storage quotas from the API https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/quota

type File

type File struct {
	Hashes Hashes `json:"hashes,omitempty"`
}

File is used for checking for changes in local files (relative to the server). https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/file

type Folder

type Folder struct {
	ChildCount uint32 `json:"childCount,omitempty"`
}

Folder is used for parsing only https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/folder

type Hashes

type Hashes struct {
	SHA1Hash     string `json:"sha1Hash,omitempty"`
	QuickXorHash string `json:"quickXorHash,omitempty"`
}

Hashes are integrity hashes used to determine if file content has changed. https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/hashes

type Header struct {
	// contains filtered or unexported fields
}

This is an additional header that can be specified to Request

type User

type User struct {
	UserPrincipalName string `json:"userPrincipalName"`
}

User represents the user. Currently only used to fetch the account email so we can display it in file managers with .xdg-volume-info https://docs.microsoft.com/en-ca/graph/api/user-get

func GetUser

func GetUser(auth *Auth) (User, error)

GetUser fetches the current user details from the Graph API.

Directories

Path Synopsis
Package quickxorhash provides the quickXorHash algorithm which is a quick, simple non-cryptographic hash algorithm that works by XORing the bytes in a circular-shifting fashion.
Package quickxorhash provides the quickXorHash algorithm which is a quick, simple non-cryptographic hash algorithm that works by XORing the bytes in a circular-shifting fashion.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL