Skip to content

Instantly share code, notes, and snippets.

@jamessdixon
Created September 15, 2014 00:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jamessdixon/f81b230d91c3f500ae14 to your computer and use it in GitHub Desktop.
Save jamessdixon/f81b230d91c3f500ae14 to your computer and use it in GitHub Desktop.
Consume an Azure ML API (Request/Response) using F#
#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