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

Enforcing trailing slash required by API #75

Open
AKrasheninnikov opened this issue Jan 23, 2019 · 8 comments
Open

Enforcing trailing slash required by API #75

AKrasheninnikov opened this issue Jan 23, 2019 · 8 comments

Comments

@AKrasheninnikov
Copy link

Hi!

I'm a new user of your library and it looks very convenient and promising in most cases. However, I am consuming an API that has resources containing dashes and requiring trailing slashes in the endpoint URLs in some cases. I haven't deeply debugged RestClient yet but it seems that the slash I provide in Resource("endpoint/") is omitted. How can I customize RestClient to use URLs I provide verbatim without any trimming?

Thank you in advance.

@DalSoft
Copy link
Owner

DalSoft commented Jan 23, 2019

Sure I've had to work with an API like that before. You can use a Delegating Handler to add the trailing slash:

var config = new Config()
                .UseHandler((request, token, next) =>
                {
                    request.RequestUri = new Uri(request.RequestUri.ToString() + "/");
                    return next(request, token);
                });
            
dynamic client = new RestClient("https://jsonplaceholder.typicode.com", config);

// With the Delegating Handler above this would call https://jsonplaceholder.typicode.com/my-users/
var user = await client
                .Resource("my-users")
                .Get();

Let me know if this works for you.

D

@AKrasheninnikov
Copy link
Author

Hello, D!
Thanks a lot for the sample! It works on a particular request, but I'll have to figure out a way to keep the Uri verbatim rather than forcefully append the trailing slash because some endpoints require the slash and some prohibit it. Anyway, your help was (and will be) greatly appreciated!

@AKrasheninnikov
Copy link
Author

I need to intercept the RequestUri change before (or exactly where) it happens. Can you tell me where to look?

image

@AKrasheninnikov
Copy link
Author

AKrasheninnikov commented Jan 24, 2019

Found this in HttpVerbCommand.cs. Looks like I need a custom build without those lines for now:)

        private static Uri ParseUri(string httpMethod, string currentUri, object[] args)
        {
            if (args.Length > 0 && httpMethod.IsImmutableVerb())
            {
                ResourceCommand.ValidateResourceArgs(args);
                currentUri += "/" + args[0];
            }

            **if (currentUri.EndsWith("/"))
                currentUri = currentUri.TrimEnd("/".ToCharArray());**

            if (!Uri.TryCreate(currentUri, UriKind.Absolute, out var uri))
                throw new ArgumentException($"{currentUri} is not a valid Absolute Uri");

            return uri;
        }

@DalSoft
Copy link
Owner

DalSoft commented Jan 24, 2019

Yes that's the place to do it, I tested it and it works as expected. Unfortunately it will need to be a custom build for now. I have a story in the backlog for allowing chain extension with your own dynamic commands.

Then it would be as simple as writing your command and doing restClient.MyCommandDoesntTrimUri("my-resource/")

Sorry it wasn't as easy as it should of been to do this.

@DalSoft DalSoft closed this as completed Jan 24, 2019
@DalSoft DalSoft reopened this Jan 24, 2019
@DalSoft
Copy link
Owner

DalSoft commented Jan 24, 2019

I also have thought of a easy change I can add that might workaround this issue, I try and get something out today.

@AKrasheninnikov
Copy link
Author

AKrasheninnikov commented Jan 24, 2019

Perhaps an optional parameter would fit a call like .Resource(string, SlashTrimmingOption.NoTrimming)?

enum SlashTrimmingOption {

  • NoTrimming
  • TrimStart
  • TrimEnd
  • TrimAll

}

At least from the .Resource(...) usage perspective it looks an appropriate place to put. If we used chaining to build the Uri, then we need some other solution Like an explicit .Slash() call.

@DalSoft
Copy link
Owner

DalSoft commented Jan 24, 2019

What I was thinking is if you passed a slash in Resource() not to trim it, unlikely to be a breaking change and keeps it clean. I'll definitely add this just half though 3.3.3 ate the moment. I leave this open and close when I release 3.3.3 with this fix.

@DalSoft DalSoft added this to To do in RestClient 4.2.0 Aug 24, 2019
@DalSoft DalSoft removed this from To do in RestClient 4.2.0 Aug 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants