diff --git a/RetroCoreFit/RequestBuilder.cs b/RetroCoreFit/RequestBuilder.cs index b1dec58..255e1d7 100644 --- a/RetroCoreFit/RequestBuilder.cs +++ b/RetroCoreFit/RequestBuilder.cs @@ -8,6 +8,20 @@ namespace RetroCoreFit { + public readonly struct Literal + { + public readonly string Value; + + public Literal(string value) + { + this.Value = value; + } + + public static explicit operator Literal(string s) => new Literal(s); + + public static Literal String(string s) => new Literal(s); + } + public delegate HttpRequestMessage BuilderDelegate(HttpRequestMessage msg); public class RequestBuilder @@ -109,20 +123,27 @@ public RequestBuilder Delete() => Append(this, (@this) => { Handler = (_) => new HttpRequestMessage(HttpMethod.Post, url) }; + public static RequestBuilder Post(FormattableString url) => Post(url.EscapeUriComponent()); + public static RequestBuilder Put(string url) => new RequestBuilder { Handler = (_) => new HttpRequestMessage(HttpMethod.Put, url) }; + public static RequestBuilder Put(FormattableString url) => Put(url.EscapeUriComponent()); + public static RequestBuilder Patch(string url) => new RequestBuilder { Handler = (_) => new HttpRequestMessage(new HttpMethod("PATCH"), url) }; + public static RequestBuilder Patch(FormattableString url) => Patch(url.EscapeUriComponent()); + public static RequestBuilder Delete(string url) => new RequestBuilder { Handler = (_) => new HttpRequestMessage(HttpMethod.Delete, url) }; + public static RequestBuilder Delete(FormattableString url) => Delete(url.EscapeUriComponent()); public RequestBuilder Header(string name, string value, bool validate = false) { @@ -302,5 +323,14 @@ public RequestBuilder MultipartFile(string name, HttpContent fileContent, public static RequestBuilder Get(string baseUrl) => new RequestBuilder() { Handler = (_) => new HttpRequestMessage(HttpMethod.Get, baseUrl) }; + public static RequestBuilder Get(FormattableString url) => + new RequestBuilder() { Handler = (_) => new HttpRequestMessage(HttpMethod.Get, url.EscapeUriComponent()) }; + + public static RequestBuilder New(string baseUrl) => + new RequestBuilder() { Handler = (_) => new HttpRequestMessage(HttpMethod.Get, baseUrl) }; + + public static RequestBuilder New(FormattableString url) => + new RequestBuilder() { Handler = (_) => new HttpRequestMessage(HttpMethod.Get, url.EscapeUriComponent()) }; + } } diff --git a/RetroCoreFit/UrlExtensions.cs b/RetroCoreFit/UrlExtensions.cs index 53784de..64be95e 100644 --- a/RetroCoreFit/UrlExtensions.cs +++ b/RetroCoreFit/UrlExtensions.cs @@ -13,5 +13,22 @@ public static string EscapeUriComponent(this string value) } return Uri.UnescapeDataString(value).Replace("%20", "+"); } + + public static string EscapeUriComponent(this FormattableString text) + { + var supplied = text.GetArguments(); + var args = new object?[text.ArgumentCount]; + for (int i = 0; i < args.Length; i++) + { + var v = supplied[i]; + if (v is Literal literal) + { + args[i] = literal.Value; + continue; + } + args[i] = v != null ? v.ToString().EscapeUriComponent() : v ; + } + return string.Format(text.Format, args); + } } }