6 people like it.

Stackoverflow API script

A script that consumes the Stackoverflow API in a dynamic fashion

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
// Just read this article about consuming the Stackoverflow API in powershell: 
// http://www.dougfinke.com/blog/index.php/2012/02/08/using-powershell-v3-to-consume-stackoverflow-json-api/
// and ported it to script-ish F# 
// using ideas from http://stackoverflow.com/a/4208663/21239

#r "System.Web.Extensions"

open System

let (?) (o:obj) (name:obj) = 
    match o with
    | :? Collections.Generic.Dictionary<_,_> as d -> d.[string name]
    | :? Array as a -> (a.GetValue(name :?> int))

let getStackOverflowUser id =
    let url = sprintf "http://api.stackoverflow.com/1.1/users/%d" id
    let request = Net.WebRequest.Create url :?> Net.HttpWebRequest
    request.AutomaticDecompression <- Net.DecompressionMethods.GZip
    use response = request.GetResponse()
    use rs = response.GetResponseStream()
    use reader = IO.StreamReader rs
    (Web.Script.Serialization.JavaScriptSerializer()).DeserializeObject(reader.ReadToEnd())

(getStackOverflowUser 22656)?users?(0)?display_name
namespace System
val o : obj
type obj = Object

Full name: Microsoft.FSharp.Core.obj
val name : obj
Multiple items
namespace System.Collections

--------------------
namespace Microsoft.FSharp.Collections
namespace System.Collections.Generic
Multiple items
type Dictionary<'TKey,'TValue> =
  new : unit -> Dictionary<'TKey, 'TValue> + 5 overloads
  member Add : key:'TKey * value:'TValue -> unit
  member Clear : unit -> unit
  member Comparer : IEqualityComparer<'TKey>
  member ContainsKey : key:'TKey -> bool
  member ContainsValue : value:'TValue -> bool
  member Count : int
  member GetEnumerator : unit -> Enumerator<'TKey, 'TValue>
  member GetObjectData : info:SerializationInfo * context:StreamingContext -> unit
  member Item : 'TKey -> 'TValue with get, set
  ...
  nested type Enumerator
  nested type KeyCollection
  nested type ValueCollection

Full name: System.Collections.Generic.Dictionary<_,_>

--------------------
Collections.Generic.Dictionary() : unit
Collections.Generic.Dictionary(capacity: int) : unit
Collections.Generic.Dictionary(comparer: Collections.Generic.IEqualityComparer<'TKey>) : unit
Collections.Generic.Dictionary(dictionary: Collections.Generic.IDictionary<'TKey,'TValue>) : unit
Collections.Generic.Dictionary(capacity: int, comparer: Collections.Generic.IEqualityComparer<'TKey>) : unit
Collections.Generic.Dictionary(dictionary: Collections.Generic.IDictionary<'TKey,'TValue>, comparer: Collections.Generic.IEqualityComparer<'TKey>) : unit
val d : Collections.Generic.Dictionary<string,obj>
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
type Array =
  member Clone : unit -> obj
  member CopyTo : array:Array * index:int -> unit + 1 overload
  member GetEnumerator : unit -> IEnumerator
  member GetLength : dimension:int -> int
  member GetLongLength : dimension:int -> int64
  member GetLowerBound : dimension:int -> int
  member GetUpperBound : dimension:int -> int
  member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
  member Initialize : unit -> unit
  member IsFixedSize : bool
  ...

Full name: System.Array
val a : Array
Array.GetValue([<ParamArray>] indices: int64 []) : obj
Array.GetValue(index: int64) : obj
Array.GetValue(index: int) : obj
Array.GetValue([<ParamArray>] indices: int []) : obj
Array.GetValue(index1: int64, index2: int64) : obj
Array.GetValue(index1: int, index2: int) : obj
Array.GetValue(index1: int64, index2: int64, index3: int64) : obj
Array.GetValue(index1: int, index2: int, index3: int) : obj
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val getStackOverflowUser : id:int -> obj

Full name: Script.getStackOverflowUser
val id : int
val url : string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val request : Net.HttpWebRequest
namespace System.Net
type WebRequest =
  inherit MarshalByRefObject
  member Abort : unit -> unit
  member AuthenticationLevel : AuthenticationLevel with get, set
  member BeginGetRequestStream : callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginGetResponse : callback:AsyncCallback * state:obj -> IAsyncResult
  member CachePolicy : RequestCachePolicy with get, set
  member ConnectionGroupName : string with get, set
  member ContentLength : int64 with get, set
  member ContentType : string with get, set
  member Credentials : ICredentials with get, set
  member EndGetRequestStream : asyncResult:IAsyncResult -> Stream
  ...

Full name: System.Net.WebRequest
Net.WebRequest.Create(requestUri: Uri) : Net.WebRequest
Net.WebRequest.Create(requestUriString: string) : Net.WebRequest
type HttpWebRequest =
  inherit WebRequest
  member Abort : unit -> unit
  member Accept : string with get, set
  member AddRange : range:int -> unit + 7 overloads
  member Address : Uri
  member AllowAutoRedirect : bool with get, set
  member AllowWriteStreamBuffering : bool with get, set
  member AutomaticDecompression : DecompressionMethods with get, set
  member BeginGetRequestStream : callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginGetResponse : callback:AsyncCallback * state:obj -> IAsyncResult
  member ClientCertificates : X509CertificateCollection with get, set
  ...

Full name: System.Net.HttpWebRequest
property Net.HttpWebRequest.AutomaticDecompression: Net.DecompressionMethods
type DecompressionMethods =
  | None = 0
  | GZip = 1
  | Deflate = 2

Full name: System.Net.DecompressionMethods
field Net.DecompressionMethods.GZip = 1
val response : Net.WebResponse
Net.HttpWebRequest.GetResponse() : Net.WebResponse
val rs : IO.Stream
Net.WebResponse.GetResponseStream() : IO.Stream
val reader : IO.StreamReader
namespace System.IO
Multiple items
type StreamReader =
  inherit TextReader
  new : stream:Stream -> StreamReader + 9 overloads
  member BaseStream : Stream
  member Close : unit -> unit
  member CurrentEncoding : Encoding
  member DiscardBufferedData : unit -> unit
  member EndOfStream : bool
  member Peek : unit -> int
  member Read : unit -> int + 1 overload
  member ReadLine : unit -> string
  member ReadToEnd : unit -> string
  ...

Full name: System.IO.StreamReader

--------------------
IO.StreamReader(stream: IO.Stream) : unit
IO.StreamReader(path: string) : unit
IO.StreamReader(stream: IO.Stream, detectEncodingFromByteOrderMarks: bool) : unit
IO.StreamReader(stream: IO.Stream, encoding: Text.Encoding) : unit
IO.StreamReader(path: string, detectEncodingFromByteOrderMarks: bool) : unit
IO.StreamReader(path: string, encoding: Text.Encoding) : unit
IO.StreamReader(stream: IO.Stream, encoding: Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
IO.StreamReader(path: string, encoding: Text.Encoding, detectEncodingFromByteOrderMarks: bool) : unit
IO.StreamReader(stream: IO.Stream, encoding: Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
IO.StreamReader(path: string, encoding: Text.Encoding, detectEncodingFromByteOrderMarks: bool, bufferSize: int) : unit
namespace System.Web
namespace System.Web.Script
namespace System.Web.Script.Serialization
Multiple items
type JavaScriptSerializer =
  new : unit -> JavaScriptSerializer + 1 overload
  member ConvertToType<'T> : obj:obj -> 'T + 1 overload
  member Deserialize<'T> : input:string -> 'T + 1 overload
  member DeserializeObject : input:string -> obj
  member MaxJsonLength : int with get, set
  member RecursionLimit : int with get, set
  member RegisterConverters : converters:IEnumerable<JavaScriptConverter> -> unit
  member Serialize : obj:obj -> string + 1 overload

Full name: System.Web.Script.Serialization.JavaScriptSerializer

--------------------
Web.Script.Serialization.JavaScriptSerializer() : unit
Web.Script.Serialization.JavaScriptSerializer(resolver: Web.Script.Serialization.JavaScriptTypeResolver) : unit
IO.StreamReader.ReadToEnd() : string
Raw view Test code New version

More information

Link:http://fssnip.net/aB
Posted:12 years ago
Author:Mauricio Scheffer
Tags: dynamic , api , json , web