Accept empty Path attr (#85)

This commit is contained in:
Patrick Stevens
2024-02-06 20:49:51 +00:00
committed by GitHub
parent a0fb7ee43a
commit 4e18e8b1bf
3 changed files with 21 additions and 9 deletions

View File

@@ -117,7 +117,7 @@ module PureGymApi =
} }
|> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct)) |> (fun a -> Async.StartAsTask (a, ?cancellationToken = ct))
member _.GetGym (gymId : int, ct : CancellationToken option) = member _.GetGym (gym : int, ct : CancellationToken option) =
async { async {
let! ct = Async.CancellationToken let! ct = Async.CancellationToken
@@ -127,8 +127,8 @@ module PureGymApi =
| null -> System.Uri "https://whatnot.com" | null -> System.Uri "https://whatnot.com"
| v -> v), | v -> v),
System.Uri ( System.Uri (
"v1/gyms/{gym_id}" "v1/gyms/{gym}"
.Replace ("{gym_id}", gymId.ToString () |> System.Web.HttpUtility.UrlEncode), .Replace ("{gym}", gym.ToString () |> System.Web.HttpUtility.UrlEncode),
System.UriKind.Relative System.UriKind.Relative
) )
) )

View File

@@ -20,8 +20,8 @@ type IPureGymApi =
[<RestEase.GetAttribute "v1/member">] [<RestEase.GetAttribute "v1/member">]
abstract GetMember : ?ct : CancellationToken -> Member Task abstract GetMember : ?ct : CancellationToken -> Member Task
[<RestEase.Get "v1/gyms/{gym_id}">] [<RestEase.Get "v1/gyms/{gym}">]
abstract GetGym : [<Path "gym_id">] gymId : int * ?ct : CancellationToken -> Task<Gym> abstract GetGym : [<Path>] gym : int * ?ct : CancellationToken -> Task<Gym>
[<GetAttribute "v1/member/activity">] [<GetAttribute "v1/member/activity">]
abstract GetMemberActivity : ?ct : CancellationToken -> Task<MemberActivityDto> abstract GetMemberActivity : ?ct : CancellationToken -> Task<MemberActivityDto>

View File

@@ -20,10 +20,15 @@ module internal HttpClientGenerator =
open Fantomas.FCS.Text.Range open Fantomas.FCS.Text.Range
open Myriad.Core.Ast open Myriad.Core.Ast
[<RequireQualifiedAccess>]
type PathSpec =
| Verbatim of string
| MatchArgName
type HttpAttribute = type HttpAttribute =
// TODO: Format parameter to these attrs // TODO: Format parameter to these attrs
| Query of string option | Query of string option
| Path of string | Path of PathSpec
| Body | Body
type Parameter = type Parameter =
@@ -230,18 +235,23 @@ module internal HttpClientGenerator =
(template, arg.Attributes) (template, arg.Attributes)
||> List.fold (fun template attr -> ||> List.fold (fun template attr ->
match attr with match attr with
| HttpAttribute.Path s -> | HttpAttribute.Path spec ->
let varName = let varName =
match arg.Id with match arg.Id with
| None -> failwith "TODO: anonymous args" | None -> failwith "TODO: anonymous args"
| Some id -> id | Some id -> id
let substituteId =
match spec with
| PathSpec.Verbatim s -> s
| PathSpec.MatchArgName -> varName.idText
template template
|> SynExpr.callMethodArg |> SynExpr.callMethodArg
"Replace" "Replace"
(SynExpr.CreateParenedTuple (SynExpr.CreateParenedTuple
[ [
SynExpr.CreateConstString ("{" + s + "}") SynExpr.CreateConstString ("{" + substituteId + "}")
SynExpr.callMethod "ToString" (SynExpr.CreateIdent varName) SynExpr.callMethod "ToString" (SynExpr.CreateIdent varName)
|> SynExpr.pipeThroughFunction ( |> SynExpr.pipeThroughFunction (
SynExpr.CreateLongIdent ( SynExpr.CreateLongIdent (
@@ -710,7 +720,9 @@ module internal HttpClientGenerator =
| "Path" | "Path"
| "PathAttribute" -> | "PathAttribute" ->
match attr.ArgExpr with match attr.ArgExpr with
| SynExpr.Const (SynConst.String (s, SynStringKind.Regular, _), _) -> Some (HttpAttribute.Path s) | SynExpr.Const (SynConst.String (s, SynStringKind.Regular, _), _) ->
Some (HttpAttribute.Path (PathSpec.Verbatim s))
| SynExpr.Const (SynConst.Unit, _) -> Some (HttpAttribute.Path PathSpec.MatchArgName)
| SynExpr.Const (a, _) -> failwith $"unrecognised constant arg to the Path attribute: %+A{a}" | SynExpr.Const (a, _) -> failwith $"unrecognised constant arg to the Path attribute: %+A{a}"
| _ -> None | _ -> None
| "Body" | "Body"