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

View File

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

View File

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