Skip to content

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

Closed

Description

@stephentoub

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

Activity

  1. added
    api-suggestion
    on Jul 19, 2023
  2. ghost added
    needs-area-label
    on Jul 19, 2023
  3. ghost added
    untriaged
    on Jul 19, 2023
  4. added and removed
    needs-area-label
    on Jul 19, 2023
  5. ShreyasJejurkar commented on Jul 19, 2023

    @ShreyasJejurkar
    Contributor

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

  6. KeterSCP commented on Jul 19, 2023

    @KeterSCP

    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

  7. neon-sunset commented on Jul 19, 2023

    @neon-sunset
    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.

  8. added this to the 9.0.0 milestone on Jul 20, 2023
  9. added
    api-ready-for-review
    and removed
    api-suggestion
    on Jul 20, 2023
  10. ghost removed
    untriaged
    on Jul 20, 2023
  11. karelz commented on Jul 20, 2023

    @karelz
    Member

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

  12. bartonjs commented on Jul 20, 2023

    @bartonjs
    Member

    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);
    }
  13. added
    api-approved
    and removed
    api-ready-for-review
    on Jul 20, 2023
  14. self-assigned this
    on Jul 20, 2023
  15. MihaZupan commented on Jul 20, 2023

    @MihaZupan
    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.

  16. ghost added
    in-pr
    on Jul 20, 2023
  17. Neme12 commented on Jul 20, 2023

    @Neme12

    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.

  18. ghost removed
    in-pr
    on Jul 20, 2023
  19. modified the milestones: 9.0.0, 8.0.0 on Aug 1, 2023
  20. ghost locked as resolved and limited conversation to collaborators on Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Net.Http

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions