Created
June 8, 2017 15:59
-
-
Save JoseGonzalez321/6b2cca09d91892f2490daad814f50994 to your computer and use it in GitHub Desktop.
This script checks that a website is responding with a 200 using FSharp
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
// From | |
// https://fsharpforfunandprofit.com/posts/low-risk-ways-to-use-fsharp-at-work-2/#dev-website-responding | |
//This script checks that a website is responding with a 200. | |
//This might be useful as the basis for a post-deployment smoke test | |
#r @"..\Packages\FSharp.Data\lib\net40\FSharp.Data.dll" | |
open FSharp.Data | |
let queryServer uri queryParams = | |
try | |
let response = Http.Request(uri, query=queryParams, silentHttpErrors = true) | |
Some response | |
with | |
| :? System.Net.WebException as ex -> None // | :? Matching on subtypes - crude polymorphism | |
let sendAlert uri message = | |
printfn "Error for %s. Message=%O" uri message | |
// match...with explanation https://fsharpforfunandprofit.com/posts/match-expression/ | |
// match is the new switch/case...except that matter is important! | |
let checkServer (uri, queryParams) = | |
match queryServer uri queryParams with | |
| Some response -> | |
printfn "Response for %s is %O" uri response.StatusCode | |
if (response.StatusCode <> 200) then | |
sendAlert uri response.StatusCode | |
| None -> | |
sendAlert uri "No response" | |
let google = "http://google.com", ["q", "fsharp"] | |
let bad = "http://example.bad", [] | |
[google; bad] | |
|> List.iter checkServer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment