GeezerVison is showing github.com — protondrive: align move crypto and revision fallback with the SDK by tdawe1 · Pull Request #4 · rclone/Proton-API-Bridge
protondrive: align move crypto and revision fallback with the SDK
#4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter
Filter by extension
.go
(12)
dotfile
(1)
All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
protondrive: align bridge upload flow with SDK behavior
Update the bridge to match SDK-era ProtonDrive request shapes and request headers while keeping mixed-version compatibility.
This removes the duplicate outer upload retry loop, centralizes manager retry configuration, improves draft-conflict recovery, and adds focused tests for compatibility, upload delegation, and manager retry behavior.
Loading branch information
Uh oh!
There was an error while loading. Please reload this page.
commit 87eb655de608a72f94ad511156eb8b61f312258a
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
.DS_Store
.credential
.*.credential
.env
data
config.toml
config.toml
Original file line number
Diff line number
Diff line change
Expand Up
@@ -7,8 +7,9 @@ import (
type Config struct {
/* Constants */
AppVersion string
UserAgent string
AppVersion string
DriveSDKVersion string
UserAgent string
/* Login */
FirstLoginCredential *FirstLoginCredentialData
Expand Down
Expand Up
@@ -44,8 +45,9 @@ type ReusableCredentialData struct {
func NewConfigWithDefaultValues() *Config {
return &Config{
AppVersion: "",
UserAgent: "",
AppVersion: "",
DriveSDKVersion: "",
UserAgent: "",
FirstLoginCredential: &FirstLoginCredentialData{
Username: "",
Expand Down
Expand Up
@@ -75,6 +77,7 @@ func NewConfigWithDefaultValues() *Config {
func NewConfigForIntegrationTests() *Config {
appVersion := os.Getenv("PROTON_API_BRIDGE_APP_VERSION")
driveSDKVersion := os.Getenv("PROTON_API_BRIDGE_DRIVE_SDK_VERSION")
userAgent := os.Getenv("PROTON_API_BRIDGE_USER_AGENT")
username := os.Getenv("PROTON_API_BRIDGE_TEST_USERNAME")
Expand All
@@ -93,8 +96,9 @@ func NewConfigForIntegrationTests() *Config {
saltedKeyPass := os.Getenv("PROTON_API_BRIDGE_TEST_SALTEDKEYPASS")
return &Config{
AppVersion: appVersion,
UserAgent: userAgent,
AppVersion: appVersion,
DriveSDKVersion: driveSDKVersion,
UserAgent: userAgent,
FirstLoginCredential: &FirstLoginCredentialData{
Username: username,
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -2,12 +2,33 @@ package common
import (
"github.com/rclone/go-proton-api"
"github.com/go-resty/resty/v2"
)
// Applies to all API calls made by the shared proton.Manager.
const defaultAPIRequestRetryCount = 3
type preRequestHookClient interface {
AddPreRequestHook(resty.RequestMiddleware)
}
func attachDriveSDKHeaderHook(client preRequestHookClient, driveSDKVersion string) {
if driveSDKVersion == "" {
return
}
client.AddPreRequestHook(func(_ *resty.Client, req *resty.Request) error {
req.SetHeader("x-pm-drive-sdk-version", driveSDKVersion)
return nil
})
}
func getProtonManager(appVersion string, userAgent string) *proton.Manager {
/* Notes on API calls: if the app version is not specified, the api calls will be rejected. */
options := []proton.Option{
proton.WithAppVersion(appVersion),
proton.WithRetryCount(defaultAPIRequestRetryCount),
proton.WithUserAgent(userAgent),
}
m := proton.New(options...)
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,124 @@
package common
import (
"context"
"net/http"
"net/http/httptest"
"reflect"
"sync/atomic"
"testing"
"time"
"unsafe"
"github.com/go-resty/resty/v2"
"github.com/rclone/go-proton-api"
)
type fakePreRequestHookClient struct {
hooks []resty.RequestMiddleware
}
func (f *fakePreRequestHookClient) AddPreRequestHook(hook resty.RequestMiddleware) {
f.hooks = append(f.hooks, hook)
}
func TestAttachDriveSDKHeaderHookSkipsEmptyVersion(t *testing.T) {
fakeClient := &fakePreRequestHookClient{}
attachDriveSDKHeaderHook(fakeClient, "")
if len(fakeClient.hooks) != 0 {
t.Fatalf("expected no hooks, got %d", len(fakeClient.hooks))
}
}
func TestAttachDriveSDKHeaderHookSetsHeader(t *testing.T) {
fakeClient := &fakePreRequestHookClient{}
attachDriveSDKHeaderHook(fakeClient, "[email protected] ")
if len(fakeClient.hooks) != 1 {
t.Fatalf("expected one hook, got %d", len(fakeClient.hooks))
}
r := resty.New().R().SetContext(context.Background())
if err := fakeClient.hooks[0](resty.New(), r); err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if got := r.Header.Get("x-pm-drive-sdk-version"); got != "[email protected] " {
t.Fatalf("expected drive sdk header to be set, got %q", got)
}
}
func TestGetProtonManagerRetriesRequests(t *testing.T) {
t.Run("succeeds after retry budget is consumed", func(t *testing.T) {
// Objective (positive): prove getProtonManager enables manager-level retries by succeeding
// only after the initial call plus default retry count have been attempted.
var callCount atomic.Int32
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/core/v4/addresses" {
t.Fatalf("expected request path %q, got %q", "/core/v4/addresses", r.URL.Path)
}
if callCount.Add(1) <= int32(defaultAPIRequestRetryCount) {
w.Header().Set("Retry-After", "-10")
w.WriteHeader(http.StatusServiceUnavailable)
_, _ = w.Write([]byte(`{"Error":"temporary outage"}`))
return
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"Addresses":[]}`))
}))
defer server.Close()
// Arrange
manager := getProtonManager("bridge-test", "bridge-test-agent")
configureManagerForRetryTest(t, manager, server.URL)
client := manager.NewClient("", "", "")
defer client.Close()
defer manager.Close()
// Act
addresses, err := client.GetAddresses(context.Background())
// Assert
if err != nil {
t.Fatalf("expected request to succeed after retries, got error: %v", err)
}
if len(addresses) != 0 {
t.Fatalf("expected no addresses from fake response, got %d", len(addresses))
}
expectedCalls := int32(defaultAPIRequestRetryCount + 1)
if got := callCount.Load(); got != expectedCalls {
t.Fatalf("expected %d calls (initial + retries), got %d", expectedCalls, got)
}
})
}
func configureManagerForRetryTest(t *testing.T, manager *proton.Manager, baseURL string) {
t.Helper()
managerValue := reflect.ValueOf(manager)
if managerValue.Kind() != reflect.Pointer || managerValue.IsNil() {
t.Fatal("expected non-nil proton manager pointer")
}
rcField := managerValue.Elem().FieldByName("rc")
if !rcField.IsValid() {
t.Fatal("expected manager to contain resty client field")
}
rcValue := reflect.NewAt(rcField.Type(), unsafe.Pointer(rcField.UnsafeAddr())).Elem()
rc, ok := rcValue.Interface().(*resty.Client)
if !ok || rc == nil {
t.Fatal("expected manager resty client to be accessible")
}
rc.SetBaseURL(baseURL)
rc.SetRetryWaitTime(time.Millisecond)
rc.SetRetryMaxWaitTime(time.Millisecond)
}
Original file line number
Diff line number
Diff line change
Expand Up
@@ -59,6 +59,7 @@ func Login(ctx context.Context, config *Config, authHandler proton.AuthHandler,
if config.UseReusableLogin {
c = m.NewClient(config.ReusableCredential.UID, config.ReusableCredential.AccessToken, config.ReusableCredential.RefreshToken)
attachDriveSDKHeaderHook(c, config.DriveSDKVersion)
c.AddAuthHandler(authHandler)
c.AddDeauthHandler(deAuthHandler)
Expand Down
Expand Up
@@ -90,6 +91,7 @@ func Login(ctx context.Context, config *Config, authHandler proton.AuthHandler,
if err != nil {
return nil, nil, nil, nil, nil, nil, err
}
attachDriveSDKHeaderHook(c, config.DriveSDKVersion)
c.AddAuthHandler(authHandler)
c.AddDeauthHandler(deAuthHandler)
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,94 @@
package proton_api_bridge
import (
"fmt"
"reflect"
)
func findAndCallMethod(target any, methodName string, args ...any) (_ []reflect.Value, called bool, err error) {
defer func() {
if recovered := recover(); recovered != nil {
called = true
err = fmt.Errorf("%s panic: %v", methodName, recovered)
}
}()
if target == nil {
return nil, false, nil
}
targetValue := reflect.ValueOf(target)
if !targetValue.IsValid() {
return nil, false, nil
}
method := targetValue.MethodByName(methodName)
if !method.IsValid() {
return nil, false, nil
}
methodType := method.Type()
if methodType.NumIn() != len(args) {
return nil, true, fmt.Errorf("%s has incompatible argument count", methodName)
}
callArgs := make([]reflect.Value, len(args))
for i := range args {
paramType := methodType.In(i)
argValue, err := getCallableValue(paramType, args[i])
if err != nil {
return nil, true, fmt.Errorf("%s argument %d: %w", methodName, i, err)
}
callArgs[i] = argValue
}
return method.Call(callArgs), true, nil
}
func getCallableValue(paramType reflect.Type, arg any) (reflect.Value, error) {
if arg == nil {
switch paramType.Kind() {
case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice, reflect.Func, reflect.Chan:
return reflect.Zero(paramType), nil
default:
return reflect.Value{}, fmt.Errorf("nil not assignable to %s", paramType)
}
}
v := reflect.ValueOf(arg)
if v.Type().AssignableTo(paramType) {
return v, nil
}
if v.Type().ConvertibleTo(paramType) {
return v.Convert(paramType), nil
}
return reflect.Value{}, fmt.Errorf("%s not assignable to %s", v.Type(), paramType)
}
func extractErrorResult(value reflect.Value) (error, error) {
errorType := reflect.TypeOf((*error)(nil)).Elem()
if !value.Type().Implements(errorType) {
return nil, fmt.Errorf("result does not implement error")
}
if isNilableKind(value.Kind()) && value.IsNil() {
return nil, nil
}
err, ok := value.Interface().(error)
if !ok {
return nil, fmt.Errorf("result cannot be converted to error")
}
return err, nil
}
func isNilableKind(kind reflect.Kind) bool {
switch kind {
case reflect.Interface, reflect.Pointer, reflect.Map, reflect.Slice, reflect.Func, reflect.Chan:
return true
default:
return false
}
}
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,67 @@
package proton_api_bridge
import (
"errors"
"reflect"
"testing"
)
type noArgMethodTarget struct{}
func (noArgMethodTarget) Foo() {}
type panicMethodTarget struct{}
func (panicMethodTarget) Foo(string) {
panic("boom")
}
type concreteErr string
func (e concreteErr) Error() string {
return string(e)
}
func TestFindAndCallMethodHandlesNilTarget(t *testing.T) {
results, called, err := findAndCallMethod(nil, "Foo")
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if called {
t.Fatalf("expected called=false")
}
if results != nil {
t.Fatalf("expected nil results")
}
}
func TestFindAndCallMethodHandlesIncompatibleArgCount(t *testing.T) {
_, called, err := findAndCallMethod(noArgMethodTarget{}, "Foo", "arg")
if !called {
t.Fatalf("expected called=true")
}
if err == nil {
t.Fatalf("expected error for incompatible signature")
}
}
func TestFindAndCallMethodConvertsPanicToError(t *testing.T) {
_, called, err := findAndCallMethod(panicMethodTarget{}, "Foo", "arg")
if !called {
t.Fatalf("expected called=true")
}
if err == nil {
t.Fatalf("expected panic to be converted to error")
}
}
func TestExtractErrorResultHandlesConcreteErrorKind(t *testing.T) {
errValue := reflect.ValueOf(concreteErr("boom"))
err, extractErr := extractErrorResult(errValue)
if extractErr != nil {
t.Fatalf("expected nil extract error, got %v", extractErr)
}
if !errors.Is(err, concreteErr("boom")) {
t.Fatalf("expected concrete error, got %v", err)
}
}
1 change: 1 addition & 0 deletions
1
drive.go
Original file line number
Diff line number
Diff line change
Expand Up
@@ -71,6 +71,7 @@ func NewProtonDrive(ctx context.Context, config *common.Config, authHandler prot
// iOS drive: first active volume
if volumes[i].State == proton.VolumeStateActive {
mainShareID = volumes[i].Share.ShareID
break
}
}
// log.Println("total volumes", len(volumes), "mainShareID", mainShareID)
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.