-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
[API Proposal]: HttpMethod.From(string) #89161
Comments
Tagging subscribers to this area: @dotnet/ncl Issue DetailsBackground and motivationHttpMethod provides a constructor ( API Proposalnamespace System.Net.Http;
public class HttpMethod
{
+ public static HttpMethod From(string method) =>
method switch // could also decide to make this case-insensitive
{
"GET" => s_getMethod,
"PUT" => s_putMethod,
"POST" => s_postMethod,
"DELETE" => s_deleteMethod,
"HEAD" => s_headMethod,
"OPTIONS" => s_optionsMethod,
"TRACE" => s_traceMethod,
"PATCH" => s_patchMethod,
"CONNECT" => s_connectMethod,
_ => new HttpMethod(method);
};
} API Usagestring methodName = ...;
HttpMethod method = HttpMethod.From(methodName); Alternative DesignsNo response RisksNo response
|
I can take this one for implementation once it's approved! 🙌 |
Have you thought about naming it Yeah, I know that there is not much parsing involved, but still it seems to be more consistent with other APIs |
Since spans can now be matched against strings, |
Triage: Makes sense, top post updated with discussion results. |
namespace System.Net.Http;
public class HttpMethod
{
public static HttpMethod Parse(ReadOnlySpan<char> method);
} |
If you give us garbage we could just let the constructor throw and have the caller fix their substring/slice 🤷♂️ Don't care too much about it though. |
Maybe it should implement |
Background and motivation
HttpMethod provides a constructor (
string method = ...; ... = new HttpMethod(method);
), but the 99.999% case is that one of the static properties on HttpMethod suffices, and folks end up writing their own mapping tables to use those singletons. We can just provide that mapping once.API Proposal
namespace System.Net.Http; public class HttpMethod { // Case insensitive - to match our logic in Networking stack + public static HttpMethod From(ReadOnlySpan<char> method); // Alternative names: Parse / Get / Create // public static HttpMethod From(string method); ... OPTIONAL, Networking team thinks it is NOT necessary /* method switch { "GET" => s_getMethod, "PUT" => s_putMethod, "POST" => s_postMethod, "DELETE" => s_deleteMethod, "HEAD" => s_headMethod, "OPTIONS" => s_optionsMethod, "TRACE" => s_traceMethod, "PATCH" => s_patchMethod, "CONNECT" => s_connectMethod, _ => new HttpMethod(method); }; */ }
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: