Skip to content

Commit

Permalink
Merge pull request #5 from kiddom/tpan.add-search-endpoint
Browse files Browse the repository at this point in the history
add tests and fix query string
  • Loading branch information
ti55987 authored Apr 7, 2021
2 parents f577b29 + 7050929 commit 9c6aecd
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 29 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/kiddom/go-onedrive
go 1.15

require (
github.com/goh-chunlin/go-onedrive/onedrive v0.0.0-20201229161802-ea6494991717
github.com/h2non/filetype v1.1.0
golang.org/x/oauth2 v0.0.0-20201208152858-08078c50e5b5
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/goh-chunlin/go-onedrive v1.0.10 h1:z3OMLpUKoQ+aqZRKJGoVbvOcM34WVzkSiRgR4pGZw9g=
github.com/goh-chunlin/go-onedrive/onedrive v0.0.0-20201229161802-ea6494991717 h1:1/lnBmzPYWXRzvaltkFmyvSQl1Tgrjp/nsY0bBd4ojc=
github.com/goh-chunlin/go-onedrive/onedrive v0.0.0-20201229161802-ea6494991717/go.mod h1:JajaRcWdIDzrc65PNHztAANyZNph9nRDBPUFOwVZ44s=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand Down
21 changes: 0 additions & 21 deletions onedrive/driveItems.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"os"

Expand Down Expand Up @@ -180,26 +179,6 @@ func (s *DriveItemsService) ListSpecial(ctx context.Context, folderName DriveSpe
return oneDriveResponse, nil
}

// Search the hierarchy of items for items matching a query.
// You can search within a folder hierarchy, a whole drive, or files shared with the current user.
//
// OneDrive API docs: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/resources/driveitem?view=odsp-graph-online
func (s *DriveItemsService) Search(ctx context.Context, searchQuery string) (*OneDriveDriveItemsResponse, error) {
apiURL := "me/drive/root/search" + url.PathEscape(searchQuery)
req, err := s.client.NewRequest(http.MethodGet, apiURL, nil)
if err != nil {
return nil, err
}

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

return oneDriveResponse, nil
}

// Get an item in the default drive of the authenticated user.
//
// OneDrive API docs: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_get?view=odsp-graph-online
Expand Down
6 changes: 5 additions & 1 deletion onedrive/driveSearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package onedrive
import (
"context"
"fmt"
"net/url"
"strings"
)

Expand All @@ -24,7 +25,7 @@ type OneDriveDriveSearchResponse struct {
// Search the items in the default drive of the authenticated user.
//
// OneDrive API docs: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/api/driveitem_search?view=odsp-graph-online#request
func (s *DriveSearchService) Search(ctx context.Context, query string) (*OneDriveDriveSearchResponse, error) {
func (s *DriveSearchService) Search(ctx context.Context, query string, filter *string) (*OneDriveDriveSearchResponse, error) {
// For requests that use single quotes, if there are parameter values
// also containing single quotes, those must be double escaped; otherwise,
// the request will fail due to invalid syntax.
Expand All @@ -33,6 +34,9 @@ func (s *DriveSearchService) Search(ctx context.Context, query string) (*OneDriv
query = strings.Replace(query, "'", "''", -1)

apiURL := fmt.Sprintf("me/drive/root/search(q='%v')", query)
if filter != nil {
apiURL = apiURL + "?" + url.PathEscape(*filter)
}

req, err := s.client.NewRequest("GET", apiURL, nil)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions onedrive/driveSearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestDriveSearchService_SearchWithEmptyQuery_authenticatedUser(t *testing.T)
})

ctx := context.Background()
_, err := client.DriveSearch.Search(ctx, "")
_, err := client.DriveSearch.Search(ctx, "", nil)
if err == nil {
t.Errorf("There should be an error")
}
Expand All @@ -48,7 +48,7 @@ func TestDriveSearchService_SearchDriveItems_authenticatedUser(t *testing.T) {
})

ctx := context.Background()
gotOneDriveResponse, err := client.DriveSearch.Search(ctx, "Test")
gotOneDriveResponse, err := client.DriveSearch.Search(ctx, "Test", nil)
if err != nil {
t.Errorf("DriveSearch.Search returned error: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion onedrive/onedrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (c *Client) Do(ctx context.Context, req *http.Request, isUsingPlainHttpClie
var oneDriveError *ErrorResponse
json.NewDecoder(responseBodyReader).Decode(&oneDriveError)

if oneDriveError.Error != nil {
if oneDriveError != nil && oneDriveError.Error != nil {
if oneDriveError.Error.InnerError != nil {
return errors.New(oneDriveError.Error.Code + " - " + oneDriveError.Error.Message + " (" + oneDriveError.Error.InnerError.Date + ")")
}
Expand Down
2 changes: 1 addition & 1 deletion test/integration/driveItems_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/goh-chunlin/go-onedrive/onedrive"
"github.com/kiddom/go-onedrive/onedrive"
)

func TestDriveItems_GetItemsInDefaultDriveRoot(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions test/integration/driveSearch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ func TestDriveSearch_Search(t *testing.T) {
func TestDriveSearch_SearchWithApostrophe(t *testing.T) {
ctx, client := setup()

searchDriveItems, err := client.DriveSearch.Search(ctx, "Rabbit's")
filter := "filter=folder ne null"
searchDriveItems, err := client.DriveSearch.Search(ctx, "With open ended - 43340", &filter)
if err != nil {
t.Errorf("Error: %v\n", err)
return
}
for _, driveItem := range searchDriveItems.DriveItems {
fmt.Printf("Results: %v\n", driveItem.Name)
fmt.Printf("Results: %v\n", driveItem)
}
}
2 changes: 1 addition & 1 deletion test/integration/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"context"
"os"

"github.com/goh-chunlin/go-onedrive/onedrive"
"github.com/kiddom/go-onedrive/onedrive"

"golang.org/x/oauth2"
)
Expand Down

0 comments on commit 9c6aecd

Please sign in to comment.