Skip to content

Commit

Permalink
Merge pull request #13 from mangotree2/drive_oapi_dev
Browse files Browse the repository at this point in the history
feat: add drive download API
  • Loading branch information
zhaoche27 authored Jan 20, 2021
2 parents 3e895d0 + 9605d3a commit 2c28bb3
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 69 deletions.
47 changes: 47 additions & 0 deletions sample/api/drive.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"context"
"crypto/rand"
"fmt"
Expand All @@ -19,6 +20,8 @@ var driveService = drivev1.NewService(test.GetInternalConf("online"))
func main() {
testFileUploadAll()
testFileUploadPart()
testMediaBatchGetTmpDownloadURLs()
testFileDownload()
}
func createRandomFileData(size int64) []byte {
randomData := make([]byte, size)
Expand Down Expand Up @@ -55,6 +58,7 @@ func testFileUploadAll() {
fmt.Printf("file token is empty")
return
}

}

func testFileUploadPart() {
Expand Down Expand Up @@ -134,3 +138,46 @@ func testFileUploadPart() {
return
}
}

func testMediaBatchGetTmpDownloadURLs() {

coreCtx := core.WrapContext(context.Background())
userAccessTokenOptFn := request.SetUserAccessToken("[user_access_token]")

reqCall := driveService.Medias.BatchGetTmpDownloadUrl(coreCtx, userAccessTokenOptFn)
reqCall.SetFileTokens([]string{"[file_token]"}...)

result, err := reqCall.Do()
fmt.Printf("request_id:%s", coreCtx.GetRequestID())
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Println(tools.Prettify(e))
return
}
fmt.Printf("reault:%s", tools.Prettify(result))

if len(result.TmpDownloadUrls) == 0 {
fmt.Printf("TmpDownloadUrls len invalid")
return
}
}

func testFileDownload() {
coreCtx := core.WrapContext(context.Background())

reqCall := driveService.Files.Download(coreCtx, request.SetUserAccessToken("[user_access_token]"))

reqCall.SetFileToken("[file_token]")

fileContent := bytes.NewBuffer(nil)
reqCall.SetResponseStream(fileContent)
_, err := reqCall.Do()
fmt.Printf("request_id:%s", coreCtx.GetRequestID())
fmt.Printf("http status code:%d", coreCtx.GetHTTPStatusCode())
if err != nil {
e := err.(*response.Error)
fmt.Printf(tools.Prettify(e))
return
}
}
221 changes: 160 additions & 61 deletions service/drive/v1/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2c28bb3

Please sign in to comment.