Skip to content

Commit

Permalink
Added FormattableString
Browse files Browse the repository at this point in the history
  • Loading branch information
ackava committed Jul 15, 2022
1 parent c305275 commit 9246534
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
30 changes: 30 additions & 0 deletions RetroCoreFit/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()) };

}
}
17 changes: 17 additions & 0 deletions RetroCoreFit/UrlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 9246534

Please sign in to comment.