Created
November 26, 2021 11:14
-
-
Save marcingolenia/4d9821e70bb53552456992f043d5115d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module SupportOperations.TestApi | |
open System.Net.Http | |
open Microsoft.AspNetCore.Mvc.Testing | |
open Newtonsoft.Json | |
open System | |
open FsToolkit.ErrorHandling | |
open Program | |
let create () = (new WebApplicationFactory<Program>()).Server | |
type HttpClient with | |
member this.Put (path: string) (payload: obj) = | |
let json = JsonConvert.SerializeObject payload | |
use content = | |
new StringContent(json, Text.Encoding.UTF8, "application/json") | |
this.PutAsync(path, content) |> Async.AwaitTask | |
member this.Post (path: string) (payload: obj) = | |
let json = JsonConvert.SerializeObject payload | |
use content = | |
new StringContent(json, Text.Encoding.UTF8, "application/json") | |
this.PostAsync(path, content) |> Async.AwaitTask | |
member this.Get<'a>(path: string) = | |
this.GetAsync(path) | |
|> Async.AwaitTask | |
|> Async.bind | |
(fun resp -> | |
resp.Content.ReadAsStringAsync() | |
|> Async.AwaitTask | |
|> Async.map JsonConvert.DeserializeObject<'a>) | |
member this.GetString(path: string) = | |
this.GetStringAsync(path) |> Async.AwaitTask | |
// in Program.fs add -> type Program() = class end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment