A fluent interface allows us to create API calls dynamically, without the need to pre-define every endpoint. For example, we can use `client.path.to.the.endpoint.get()` for a call to: `GET /path/to/the/endpoint` without having to define methods for `path`, `to`, `the`, and `endpoint`.
To chain our method calls together, in general, we simply return `this`. However, in this case we are returning a new version of the `Client` object, because we want to preserve fragments of the URL for later reuse
see line 29.
To capture the method calls dynamically, we need to use C#’s dynamic `TryGetMember`
C# docs method, which is called whenever the object can't find the method you are calling
see line 20. We end the chain by trying to invoke the method, using C#’s dynamic `TryInvokeMember`
C# docs method
See line 43.
To handle special cases or allow users to specify a complete path as a string, we use the `_()` method
see line 32. You pass in a string and receive a new object with the name of your string added to the URL path variable.
The following references helped me understand the concepts described in this post:
Thank you for dropping by, and if you decide to build something with this library or have some contributions, please
open an issue.
What’s next? Stay tuned as we continue to release similar blog posts for all the other languages we support
Python,
PHP, Ruby, Node.js, Go, and Java.