elucidsoft / dotnet-stellar-sdk

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

Support ServerUri with a path included #376

Closed raymens closed 2 years ago

raymens commented 2 years 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 2 years ago

@Kirbyrawr Can you also review?

elucidsoft commented 2 years ago

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

raymens commented 2 years ago

I've rebased it