elucidsoft / dotnet-stellar-sdk

Stellar API SDK for .NET 6.x
Apache License 2.0
117 stars 55 forks source link

Support ServerUri with a path included #376

Closed raymens closed 1 year ago

raymens commented 1 year ago

We are having issues using a Node API provider that requires a path prefix like (https://provider.com/rpc/xlm/{auth-key}/). Currently the SDK only uses the domain part of the supplied Horizon URI, and the path is fully replaced by the segments generated by the various RequestBuilders.

Through this change the original path is now stored and used later on when adding the segments again.

I'm not entirely sure in what category this falls (bug/ feature/ breaking). If users have set-up the serverUri with a path that shouldn't be there, it would be included now. I'm happy to update the version once it's clear if it's major/minor/fix.

Workaround

As a temporary workaround we're using the following:

let prefixUriBuilderOfSnd (first: string) (second: Uri) =
    let urlBuilder = new UriBuilder(first)
    let target = new UriBuilder(second)

    // do not update if the path is the default `/`
    if urlBuilder.Path = "/" then
        target.Uri
    elif target.Path.StartsWith("/") then
        target.Path <- urlBuilder.Path + target.Path[1..]
        target.Uri
    else
        target.Path <- urlBuilder.Path + target.Path
        target.Uri

let private overrideServerUriHandler =
    { new DelegatingHandler(InnerHandler = new HttpClientHandler()) with
        member x.Send(request, cancellationToken) =
            request.RequestUri <- prefixUriBuilderOfSnd serverUri request.RequestUri
            base.Send(request, cancellationToken)
        member x.SendAsync(request, cancellationToken) =
            request.RequestUri <- prefixUriBuilderOfSnd serverUri request.RequestUri
            base.SendAsync(request, cancellationToken)
    }

Types of changes

elucidsoft commented 1 year ago

@Kirbyrawr Can you also review?

elucidsoft commented 1 year ago

Someone needs to merge this into master since we upgraded to .NET 6

raymens commented 1 year ago

I've rebased it