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

[API Proposal]: HttpMethod.From(string) #89161

Closed
stephentoub opened this issue Jul 19, 2023 · 8 comments · Fixed by #89270
Closed

[API Proposal]: HttpMethod.From(string) #89161

stephentoub opened this issue Jul 19, 2023 · 8 comments · Fixed by #89270
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Net.Http
Milestone

Comments

@stephentoub
Copy link
Member

stephentoub commented Jul 19, 2023

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

string methodName = ...;
HttpMethod method = HttpMethod.From(methodName);

Alternative Designs

No response

Risks

No response

@stephentoub stephentoub added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jul 19, 2023
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jul 19, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 19, 2023
@stephentoub stephentoub added area-System.Net.Http and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jul 19, 2023
@ghost
Copy link

ghost commented Jul 19, 2023

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

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
{
+   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 Usage

string methodName = ...;
HttpMethod method = HttpMethod.From(methodName);

Alternative Designs

No response

Risks

No response

Author: stephentoub
Assignees: -
Labels:

api-suggestion, area-System.Net.Http, untriaged, needs-area-label

Milestone: -

@ShreyasJejurkar
Copy link
Contributor

I can take this one for implementation once it's approved! 🙌

@KeterSCP
Copy link

Have you thought about naming it Parse?

Yeah, I know that there is not much parsing involved, but still it seems to be more consistent with other APIs

@neon-sunset
Copy link
Contributor

Since spans can now be matched against strings, From(ReadOnlySpan<char> method) overload might make sense too.
Alternatively, as @KeterSCP suggested, it might as well be better to implement ISpanParsable<HttpMethod> instead.

@karelz karelz added this to the 9.0.0 milestone Jul 20, 2023
@karelz karelz added api-ready-for-review API is ready for review, it is NOT ready for implementation and removed api-suggestion Early API idea and discussion, it is NOT ready for implementation labels Jul 20, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jul 20, 2023
@karelz
Copy link
Member

karelz commented Jul 20, 2023

Triage: Makes sense, top post updated with discussion results.

@bartonjs
Copy link
Member

bartonjs commented Jul 20, 2023

Video

  • We think that the method should be Parse instead of From.
  • It's probably reasonable for the method to do case normalization and maybe trimming ("get " -> "GET")
  • It's mildly unfortunate for the 0.01% case when it hits the unknown case that it could be string -> ROS-char -> new allocated string, but since it's expected to be 0.01%, it's fine.
namespace System.Net.Http;

public class HttpMethod
{
    public static HttpMethod Parse(ReadOnlySpan<char> method);
}

@bartonjs bartonjs added api-approved API was approved in API review, it can be implemented and removed api-ready-for-review API is ready for review, it is NOT ready for implementation labels Jul 20, 2023
@stephentoub stephentoub self-assigned this Jul 20, 2023
@MihaZupan
Copy link
Member

and maybe trimming

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.

@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Jul 20, 2023
@Neme12
Copy link

Neme12 commented Jul 20, 2023

Maybe it should implement IParsable/ISpanParsable as well if it has a Parse method? That just seems like general goodness so that serialization frameworks would automatically be able to deserialize this type.

@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jul 20, 2023
@karelz karelz modified the milestones: 9.0.0, 8.0.0 Aug 1, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Aug 31, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Net.Http
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants