Created
May 6, 2023 14:47
-
-
Save jovaneyck/9ae7562d02e11972f673d441aad6c582 to your computer and use it in GitHub Desktop.
An example on how to use SwaggerProvider: a type provider that automatically generates strongly-typed clients for openapi REST api's
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//https://fsprojects.github.io/SwaggerProvider/#/ | |
(* | |
#r "nuget: SwaggerProvider, Version=2.0.0-beta9" | |
*) | |
open SwaggerProvider | |
open System | |
open System.Net.Http | |
type PetStore = OpenApiClientProvider<"https://petstore3.swagger.io/api/v3/openapi.json"> | |
let baseUri = Uri("https://petstore3.swagger.io/api/v3/") | |
let bearerToken = "01832905-a2db-4caa-bdab-5818ef5ec491" | |
let httpClient = new HttpClient(BaseAddress = baseUri) | |
httpClient.DefaultRequestHeaders.Authorization <- Headers.AuthenticationHeaderValue("Bearer", bearerToken) | |
let client = PetStore.Client(httpClient) | |
let createdPet = | |
client | |
.AddPet( | |
PetStore.Pet( | |
id = Option.Some(123), | |
name = (sprintf "Pet:%A" (Guid.NewGuid())), | |
photoUrls = [||], | |
status = "available" | |
) | |
) | |
.Result | |
let retrieved = | |
createdPet.Id | |
|> Option.map (fun id -> client.GetPetById(id).Result) | |
printfn "Retrieved the newly created pet: %A" retrieved | |
let pets = client.FindPetsByStatus("available").Result | |
pets | |
|> Seq.rev | |
|> Seq.take 3 | |
|> Seq.iter (fun p -> printfn "%A" p.Name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment