Consume an Azure ML API (Request/Response) using F#
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
#r @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Net.Http.dll" | |
#r @"..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.Net.Http.Formatting.dll" | |
open System | |
open System.Net.Http | |
open System.Net.Http.Headers | |
open System.Net.Http.Formatting | |
open System.Collections.Generic | |
type ScoreData (featureVector:Dictionary<string,string>, globalParameters:Dictionary<string,string>) = | |
member this.FeatureVector = featureVector | |
member this.GlobalParameters = globalParameters | |
type ScoreRequest (id:string, instance:ScoreData) = | |
member this.Id = id | |
member this.Instance = instance | |
let invokeService () = async { | |
let apiKey = "" | |
let uri = "" | |
use client = new HttpClient() | |
client.DefaultRequestHeaders.Authorization <- new AuthenticationHeaderValue("Bearer",apiKey) | |
client.BaseAddress <- new Uri(uri) | |
let input = new Dictionary<string,string>() | |
input.Add("Zip Code","27519") | |
input.Add("Race","W") | |
input.Add("Party","UNA") | |
input.Add("Gender","M") | |
input.Add("Age","45") | |
input.Add("Voted Ind","1") | |
let scoreData = new ScoreData(input,new Dictionary<string,string>()) | |
let scoreRequest = new ScoreRequest("score00001",scoreData) | |
let! response = client.PostAsJsonAsync("",scoreRequest) |> Async.AwaitTask | |
let! result = response.Content.ReadAsStringAsync() |> Async.AwaitTask | |
if response.IsSuccessStatusCode then | |
printfn "%s" result | |
else | |
printfn "FAILED: %s" result | |
response |> ignore | |
} | |
invokeService() |> Async.RunSynchronously |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment