Skip to content
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

Include trailing slash and disable compression #9

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions ssclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"net/http"
"strings"
"time"

kabs "github.com/microsoft/kiota-abstractions-go"
khttp "github.com/microsoft/kiota-http-go"
Expand All @@ -12,16 +14,65 @@ import (
)

func New(httpClient *http.Client, baseURL, username, key string) (*kiota.Client, error) {
authProvider := NewStorageServiceAuthProvider(username, key)
adapter, err := khttp.NewNetHttpRequestAdapter(authProvider)
if httpClient == nil {
httpClient = http.DefaultClient
}

if err := configureMiddleware(httpClient); err != nil {
return nil, fmt.Errorf("configure client middleware: %v", err)
}

adapter, err := khttp.NewNetHttpRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClient(
&authProvider{username: username, key: key},
nil,
nil,
httpClient,
)
if err != nil {
return nil, fmt.Errorf("create client adapter: %v", err)
}

adapter.SetBaseUrl(baseURL)

return kiota.NewClient(adapter), nil
}

// configureMiddleware installs the middlewares needed by this client.
func configureMiddleware(client *http.Client) error {
var middlewares []khttp.Middleware

userAgentOpts := khttp.UserAgentHandlerOptions{
Enabled: true,
ProductName: "ssclient-go",
ProductVersion: "v0",
}
compressionOpts := khttp.NewCompressionOptions(false)
retryOpts := khttp.RetryHandlerOptions{
ShouldRetry: func(delay time.Duration, executionCount int, request *http.Request, response *http.Response) bool {
// TODO: we use go-retryablehttp but this could be provided instead.
return false
},
}

// We rely on the default set of middlewares provided by Kiota with a small
// number of customizations.
middlewares, err := khttp.GetDefaultMiddlewaresWithOptions(
&userAgentOpts,
&compressionOpts,
&retryOpts,
)
if err != nil {
return err
}

// Introduce our middleware to inject the trailing slash.
middlewares = append(middlewares, appendTrailingSlashHandler{})

client.Transport = khttp.NewCustomTransportWithParentTransport(client.Transport, middlewares...)

return nil
}

type authProvider struct {
username string
key string
Expand All @@ -39,3 +90,25 @@ func (p *authProvider) AuthenticateRequest(ctx context.Context, request *kabs.Re

return nil
}

// appendTrailingSlashHandler is a middleware that ensures that the path has a
// trailing slash when expected by Archivematica Storage Service API. This is
// not something that we have not been able to describe using TypeSpec yet.
type appendTrailingSlashHandler struct{}

var useSlash = map[string]struct{}{
http.MethodPost: {},
http.MethodPut: {},
http.MethodPatch: {},
http.MethodDelete: {},
}

func (middleware appendTrailingSlashHandler) Intercept(pipeline khttp.Pipeline, middlewareIndex int, req *http.Request) (*http.Response, error) {
if _, ok := useSlash[req.Method]; ok {
if !strings.HasSuffix(req.URL.Path, "/") {
req.URL.Path += "/"
}
}

return pipeline.Next(req, middlewareIndex)
}
4 changes: 1 addition & 3 deletions ssclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"reflect"
"testing"

kiotahttpgo "github.com/microsoft/kiota-http-go"

"go.artefactual.dev/ssclient"
)

Expand All @@ -25,7 +23,7 @@ func TestClient(t *testing.T) {
"Accept": {"application/json"},
"Accept-Encoding": {"gzip"},
"Authorization": {"ApiKey test:test"},
"User-Agent": {"kiota-go/" + kiotahttpgo.NewUserAgentHandlerOptions().ProductVersion},
"User-Agent": {"ssclient-go/v0"},
}))

w.Header().Set("Content-Type", "application/json")
Expand Down