Stackoverflow API script

A script that consumes the Stackoverflow API in a dynamic fashion

Copy Source
Copy Link
Tools:
 1: // Just read this article about consuming the Stackoverflow API in powershell: 
 2: // http://www.dougfinke.com/blog/index.php/2012/02/08/using-powershell-v3-to-consume-stackoverflow-json-api/
 3: // and ported it to script-ish F# 
 4: // using ideas from http://stackoverflow.com/a/4208663/21239
 5: 
 6: #r "System.Web.Extensions"
 7: 
 8: open System
 9: 
10: let (?) (o:obj) (name:obj) = 
11:     match o with
12:     | :? Collections.Generic.Dictionary<_,_> as d -> d.[string name]
13:     | :? Array as a -> (a.GetValue(name :?> int))
14: 
15: let getStackOverflowUser id =
16:     let url = sprintf "http://api.stackoverflow.com/1.1/users/%d" id
17:     let request = Net.WebRequest.Create url :?> Net.HttpWebRequest
18:     request.AutomaticDecompression <- Net.DecompressionMethods.GZip
19:     use response = request.GetResponse()
20:     use rs = response.GetResponseStream()
21:     use reader = IO.StreamReader rs
22:     (Web.Script.Serialization.JavaScriptSerializer()).DeserializeObject(reader.ReadToEnd())
23: 
24: (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
type Dictionary<'TKey,'TValue> =
  class
    new : unit -> System.Collections.Generic.Dictionary<'TKey,'TValue>
    new : int -> System.Collections.Generic.Dictionary<'TKey,'TValue>
    new : System.Collections.Generic.IEqualityComparer<'TKey> -> System.Collections.Generic.Dictionary<'TKey,'TValue>
    new : int * System.Collections.Generic.IEqualityComparer<'TKey> -> System.Collections.Generic.Dictionary<'TKey,'TValue>
    new : System.Collections.Generic.IDictionary<'TKey,'TValue> -> System.Collections.Generic.Dictionary<'TKey,'TValue>
    new : System.Collections.Generic.IDictionary<'TKey,'TValue> * System.Collections.Generic.IEqualityComparer<'TKey> -> System.Collections.Generic.Dictionary<'TKey,'TValue>
    member Add : 'TKey * 'TValue -> unit
    member Clear : unit -> unit
    member Comparer : System.Collections.Generic.IEqualityComparer<'TKey>
    member ContainsKey : 'TKey -> bool
    member ContainsValue : 'TValue -> bool
    member Count : int
    member GetEnumerator : unit -> Enumerator<'TKey,'TValue>
    member GetObjectData : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> unit
    member Item : 'TKey -> 'TValue with get, set
    member Keys : KeyCollection<'TKey,'TValue>
    member OnDeserialization : obj -> unit
    member Remove : 'TKey -> bool
    member TryGetValue : 'TKey * 'TValue -> bool
    member Values : ValueCollection<'TKey,'TValue>
    type Enumerator =
      struct
        member Current : System.Collections.Generic.KeyValuePair<'TKey,'TValue>
        member Dispose : unit -> unit
        member MoveNext : unit -> bool
      end
    type KeyCollection =
      class
        new : System.Collections.Generic.Dictionary<'TKey,'TValue> -> KeyCollection
        member CopyTo : 'TKey [] * int -> unit
        member Count : int
        member GetEnumerator : unit -> Enumerator<'TKey,'TValue>
        type Enumerator =
          struct
            member Current : 'TKey
            member Dispose : unit -> unit
            member MoveNext : unit -> bool
          end
      end
    type ValueCollection =
      class
        new : System.Collections.Generic.Dictionary<'TKey,'TValue> -> ValueCollection
        member CopyTo : 'TValue [] * int -> unit
        member Count : int
        member GetEnumerator : unit -> Enumerator<'TKey,'TValue>
        type Enumerator =
          struct
            member Current : 'TValue
            member Dispose : unit -> unit
            member MoveNext : unit -> bool
          end
      end
  end

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

  type: Collections.Generic.Dictionary<'TKey,'TValue>
  implements: Collections.Generic.IDictionary<'TKey,'TValue>
  implements: Collections.Generic.ICollection<Collections.Generic.KeyValuePair<'TKey,'TValue>>
  implements: seq<Collections.Generic.KeyValuePair<'TKey,'TValue>>
  implements: Collections.IDictionary
  implements: Collections.ICollection
  implements: Collections.IEnumerable
  implements: Runtime.Serialization.ISerializable
  implements: Runtime.Serialization.IDeserializationCallback
val d : Collections.Generic.Dictionary<string,obj>

  type: Collections.Generic.Dictionary<string,obj>
  implements: Collections.Generic.IDictionary<string,obj>
  implements: Collections.Generic.ICollection<Collections.Generic.KeyValuePair<string,obj>>
  implements: seq<Collections.Generic.KeyValuePair<string,obj>>
  implements: Collections.IDictionary
  implements: Collections.ICollection
  implements: Collections.IEnumerable
  implements: Runtime.Serialization.ISerializable
  implements: Runtime.Serialization.IDeserializationCallback
Multiple items
val string : 'T -> string

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

--------------------

type string = String

Full name: Microsoft.FSharp.Core.string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
type Array =
  class
    member Clone : unit -> obj
    member CopyTo : System.Array * int -> unit
    member CopyTo : System.Array * int64 -> unit
    member GetEnumerator : unit -> System.Collections.IEnumerator
    member GetLength : int -> int
    member GetLongLength : int -> int64
    member GetLowerBound : int -> int
    member GetUpperBound : int -> int
    member GetValue : int [] -> obj
    member GetValue : int -> obj
    member GetValue : int64 -> obj
    member GetValue : int64 [] -> obj
    member GetValue : int * int -> obj
    member GetValue : int64 * int64 -> obj
    member GetValue : int * int * int -> obj
    member GetValue : int64 * int64 * int64 -> obj
    member Initialize : unit -> unit
    member IsFixedSize : bool
    member IsReadOnly : bool
    member IsSynchronized : bool
    member Length : int
    member LongLength : int64
    member Rank : int
    member SetValue : obj * int -> unit
    member SetValue : obj * int [] -> unit
    member SetValue : obj * int64 -> unit
    member SetValue : obj * int64 [] -> unit
    member SetValue : obj * int * int -> unit
    member SetValue : obj * int64 * int64 -> unit
    member SetValue : obj * int * int * int -> unit
    member SetValue : obj * int64 * int64 * int64 -> unit
    member SyncRoot : obj
    static member AsReadOnly<'T> : 'T [] -> System.Collections.ObjectModel.ReadOnlyCollection<'T>
    static member BinarySearch : System.Array * obj -> int
    static member BinarySearch<'T> : 'T [] * 'T -> int
    static member BinarySearch : System.Array * obj * System.Collections.IComparer -> int
    static member BinarySearch<'T> : 'T [] * 'T * System.Collections.Generic.IComparer<'T> -> int
    static member BinarySearch : System.Array * int * int * obj -> int
    static member BinarySearch<'T> : 'T [] * int * int * 'T -> int
    static member BinarySearch : System.Array * int * int * obj * System.Collections.IComparer -> int
    static member BinarySearch<'T> : 'T [] * int * int * 'T * System.Collections.Generic.IComparer<'T> -> int
    static member Clear : System.Array * int * int -> unit
    static member ConstrainedCopy : System.Array * int * System.Array * int * int -> unit
    static member ConvertAll<'TInput,'TOutput> : 'TInput [] * System.Converter<'TInput,'TOutput> -> 'TOutput []
    static member Copy : System.Array * System.Array * int -> unit
    static member Copy : System.Array * System.Array * int64 -> unit
    static member Copy : System.Array * int * System.Array * int * int -> unit
    static member Copy : System.Array * int64 * System.Array * int64 * int64 -> unit
    static member CreateInstance : System.Type * int -> System.Array
    static member CreateInstance : System.Type * int [] -> System.Array
    static member CreateInstance : System.Type * int64 [] -> System.Array
    static member CreateInstance : System.Type * int * int -> System.Array
    static member CreateInstance : System.Type * int [] * int [] -> System.Array
    static member CreateInstance : System.Type * int * int * int -> System.Array
    static member Exists<'T> : 'T [] * System.Predicate<'T> -> bool
    static member Find<'T> : 'T [] * System.Predicate<'T> -> 'T
    static member FindAll<'T> : 'T [] * System.Predicate<'T> -> 'T []
    static member FindIndex<'T> : 'T [] * System.Predicate<'T> -> int
    static member FindIndex<'T> : 'T [] * int * System.Predicate<'T> -> int
    static member FindIndex<'T> : 'T [] * int * int * System.Predicate<'T> -> int
    static member FindLast<'T> : 'T [] * System.Predicate<'T> -> 'T
    static member FindLastIndex<'T> : 'T [] * System.Predicate<'T> -> int
    static member FindLastIndex<'T> : 'T [] * int * System.Predicate<'T> -> int
    static member FindLastIndex<'T> : 'T [] * int * int * System.Predicate<'T> -> int
    static member ForEach<'T> : 'T [] * System.Action<'T> -> unit
    static member IndexOf : System.Array * obj -> int
    static member IndexOf<'T> : 'T [] * 'T -> int
    static member IndexOf : System.Array * obj * int -> int
    static member IndexOf<'T> : 'T [] * 'T * int -> int
    static member IndexOf : System.Array * obj * int * int -> int
    static member IndexOf<'T> : 'T [] * 'T * int * int -> int
    static member LastIndexOf : System.Array * obj -> int
    static member LastIndexOf<'T> : 'T [] * 'T -> int
    static member LastIndexOf : System.Array * obj * int -> int
    static member LastIndexOf<'T> : 'T [] * 'T * int -> int
    static member LastIndexOf : System.Array * obj * int * int -> int
    static member LastIndexOf<'T> : 'T [] * 'T * int * int -> int
    static member Resize<'T> : 'T [] * int -> unit
    static member Reverse : System.Array -> unit
    static member Reverse : System.Array * int * int -> unit
    static member Sort : System.Array -> unit
    static member Sort<'T> : 'T [] -> unit
    static member Sort : System.Array * System.Array -> unit
    static member Sort : System.Array * System.Collections.IComparer -> unit
    static member Sort<'TKey,'TValue> : 'TKey [] * 'TValue [] -> unit
    static member Sort<'T> : 'T [] * System.Collections.Generic.IComparer<'T> -> unit
    static member Sort<'T> : 'T [] * System.Comparison<'T> -> unit
    static member Sort : System.Array * int * int -> unit
    static member Sort : System.Array * System.Array * System.Collections.IComparer -> unit
    static member Sort<'T> : 'T [] * int * int -> unit
    static member Sort<'TKey,'TValue> : 'TKey [] * 'TValue [] * System.Collections.Generic.IComparer<'TKey> -> unit
    static member Sort : System.Array * System.Array * int * int -> unit
    static member Sort : System.Array * int * int * System.Collections.IComparer -> unit
    static member Sort<'TKey,'TValue> : 'TKey [] * 'TValue [] * int * int -> unit
    static member Sort<'T> : 'T [] * int * int * System.Collections.Generic.IComparer<'T> -> unit
    static member Sort : System.Array * System.Array * int * int * System.Collections.IComparer -> unit
    static member Sort<'TKey,'TValue> : 'TKey [] * 'TValue [] * int * int * System.Collections.Generic.IComparer<'TKey> -> unit
    static member TrueForAll<'T> : 'T [] * System.Predicate<'T> -> bool
  end

Full name: System.Array

  type: Array
  implements: ICloneable
  implements: Collections.IList
  implements: Collections.ICollection
  implements: Collections.IEnumerable
  implements: Collections.IStructuralComparable
  implements: Collections.IStructuralEquatable
val a : Array

  type: Array
  implements: ICloneable
  implements: Collections.IList
  implements: Collections.ICollection
  implements: Collections.IEnumerable
  implements: Collections.IStructuralComparable
  implements: Collections.IStructuralEquatable
Multiple overloads
Array.GetValue(indices: int64 []) : obj
Array.GetValue(index: int64) : obj
Array.GetValue(index: int) : obj
Array.GetValue(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 : 'T -> int (requires member op_Explicit)

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

--------------------

type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>

  type: int<'Measure>
  implements: IComparable
  implements: IConvertible
  implements: IFormattable
  implements: IComparable<int<'Measure>>
  implements: IEquatable<int<'Measure>>
  inherits: ValueType


--------------------

type int = int32

Full name: Microsoft.FSharp.Core.int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
val getStackOverflowUser : int -> 'a

Full name: Snippet.getStackOverflowUser
val id : int

  type: int
  implements: IComparable
  implements: IFormattable
  implements: IConvertible
  implements: IComparable<int>
  implements: IEquatable<int>
  inherits: ValueType
val url : string

  type: string
  implements: IComparable
  implements: ICloneable
  implements: IConvertible
  implements: IComparable<string>
  implements: seq<char>
  implements: Collections.IEnumerable
  implements: IEquatable<string>
val sprintf : Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val request : Net.HttpWebRequest

  type: Net.HttpWebRequest
  implements: Runtime.Serialization.ISerializable
  inherits: Net.WebRequest
  inherits: MarshalByRefObject
namespace System.Net
type WebRequest =
  class
    inherit System.MarshalByRefObject
    member Abort : unit -> unit
    member AuthenticationLevel : System.Net.Security.AuthenticationLevel with get, set
    member BeginGetRequestStream : System.AsyncCallback * obj -> System.IAsyncResult
    member BeginGetResponse : System.AsyncCallback * obj -> System.IAsyncResult
    member CachePolicy : System.Net.Cache.RequestCachePolicy with get, set
    member ConnectionGroupName : string with get, set
    member ContentLength : int64 with get, set
    member ContentType : string with get, set
    member Credentials : System.Net.ICredentials with get, set
    member EndGetRequestStream : System.IAsyncResult -> System.IO.Stream
    member EndGetResponse : System.IAsyncResult -> System.Net.WebResponse
    member GetRequestStream : unit -> System.IO.Stream
    member GetResponse : unit -> System.Net.WebResponse
    member Headers : System.Net.WebHeaderCollection with get, set
    member ImpersonationLevel : System.Security.Principal.TokenImpersonationLevel with get, set
    member Method : string with get, set
    member PreAuthenticate : bool with get, set
    member Proxy : System.Net.IWebProxy with get, set
    member RequestUri : System.Uri
    member Timeout : int with get, set
    member UseDefaultCredentials : bool with get, set
    static member Create : string -> System.Net.WebRequest
    static member Create : System.Uri -> System.Net.WebRequest
    static member CreateDefault : System.Uri -> System.Net.WebRequest
    static member CreateHttp : string -> System.Net.HttpWebRequest
    static member CreateHttp : System.Uri -> System.Net.HttpWebRequest
    static member DefaultCachePolicy : System.Net.Cache.RequestCachePolicy with get, set
    static member DefaultWebProxy : System.Net.IWebProxy with get, set
    static member GetSystemWebProxy : unit -> System.Net.IWebProxy
    static member RegisterPrefix : string * System.Net.IWebRequestCreate -> bool
  end

Full name: System.Net.WebRequest

  type: Net.WebRequest
  implements: Runtime.Serialization.ISerializable
  inherits: MarshalByRefObject
Multiple overloads
Net.WebRequest.Create(requestUri: Uri) : Net.WebRequest
Net.WebRequest.Create(requestUriString: string) : Net.WebRequest
type HttpWebRequest =
  class
    inherit System.Net.WebRequest
    member Abort : unit -> unit
    member Accept : string with get, set
    member AddRange : int -> unit
    member AddRange : int64 -> unit
    member AddRange : int * int -> unit
    member AddRange : int64 * int64 -> unit
    member AddRange : string * int -> unit
    member AddRange : string * int64 -> unit
    member AddRange : string * int * int -> unit
    member AddRange : string * int64 * int64 -> unit
    member Address : System.Uri
    member AllowAutoRedirect : bool with get, set
    member AllowReadStreamBuffering : bool with get, set
    member AllowWriteStreamBuffering : bool with get, set
    member AutomaticDecompression : System.Net.DecompressionMethods with get, set
    member BeginGetRequestStream : System.AsyncCallback * obj -> System.IAsyncResult
    member BeginGetResponse : System.AsyncCallback * obj -> System.IAsyncResult
    member ClientCertificates : System.Security.Cryptography.X509Certificates.X509CertificateCollection with get, set
    member Connection : string with get, set
    member ConnectionGroupName : string with get, set
    member ContentLength : int64 with get, set
    member ContentType : string with get, set
    member ContinueDelegate : System.Net.HttpContinueDelegate with get, set
    member CookieContainer : System.Net.CookieContainer with get, set
    member Credentials : System.Net.ICredentials with get, set
    member Date : System.DateTime with get, set
    member EndGetRequestStream : System.IAsyncResult -> System.IO.Stream
    member EndGetRequestStream : System.IAsyncResult * System.Net.TransportContext -> System.IO.Stream
    member EndGetResponse : System.IAsyncResult -> System.Net.WebResponse
    member Expect : string with get, set
    member GetRequestStream : unit -> System.IO.Stream
    member GetRequestStream : System.Net.TransportContext -> System.IO.Stream
    member GetResponse : unit -> System.Net.WebResponse
    member HaveResponse : bool
    member Headers : System.Net.WebHeaderCollection with get, set
    member Host : string with get, set
    member IfModifiedSince : System.DateTime with get, set
    member KeepAlive : bool with get, set
    member MaximumAutomaticRedirections : int with get, set
    member MaximumResponseHeadersLength : int with get, set
    member MediaType : string with get, set
    member Method : string with get, set
    member Pipelined : bool with get, set
    member PreAuthenticate : bool with get, set
    member ProtocolVersion : System.Version with get, set
    member Proxy : System.Net.IWebProxy with get, set
    member ReadWriteTimeout : int with get, set
    member Referer : string with get, set
    member RequestUri : System.Uri
    member SendChunked : bool with get, set
    member ServicePoint : System.Net.ServicePoint
    member SupportsCookieContainer : bool
    member Timeout : int with get, set
    member TransferEncoding : string with get, set
    member UnsafeAuthenticatedConnectionSharing : bool with get, set
    member UseDefaultCredentials : bool with get, set
    member UserAgent : string with get, set
    static member DefaultCachePolicy : System.Net.Cache.RequestCachePolicy with get, set
    static member DefaultMaximumErrorResponseLength : int with get, set
    static member DefaultMaximumResponseHeadersLength : int with get, set
  end

Full name: System.Net.HttpWebRequest

  type: Net.HttpWebRequest
  implements: Runtime.Serialization.ISerializable
  inherits: Net.WebRequest
  inherits: MarshalByRefObject
property Net.HttpWebRequest.AutomaticDecompression: Net.DecompressionMethods
type DecompressionMethods =
  | None = 0
  | GZip = 1
  | Deflate = 2

Full name: System.Net.DecompressionMethods

  type: Net.DecompressionMethods
  inherits: Enum
  inherits: ValueType
field Net.DecompressionMethods.GZip = 1
val response : Net.WebResponse

  type: Net.WebResponse
  implements: Runtime.Serialization.ISerializable
  implements: IDisposable
  inherits: MarshalByRefObject
Net.WebRequest.GetResponse() : Net.WebResponse
val rs : IO.Stream

  type: IO.Stream
  implements: IDisposable
  inherits: MarshalByRefObject
Net.WebResponse.GetResponseStream() : IO.Stream
val reader : IO.StreamReader

  type: IO.StreamReader
  implements: IDisposable
  inherits: IO.TextReader
  inherits: MarshalByRefObject
namespace System.IO
type StreamReader =
  class
    inherit System.IO.TextReader
    new : System.IO.Stream -> System.IO.StreamReader
    new : System.IO.Stream * bool -> System.IO.StreamReader
    new : System.IO.Stream * System.Text.Encoding -> System.IO.StreamReader
    new : System.IO.Stream * System.Text.Encoding * bool -> System.IO.StreamReader
    new : System.IO.Stream * System.Text.Encoding * bool * int -> System.IO.StreamReader
    new : string -> System.IO.StreamReader
    new : string * bool -> System.IO.StreamReader
    new : string * System.Text.Encoding -> System.IO.StreamReader
    new : string * System.Text.Encoding * bool -> System.IO.StreamReader
    new : string * System.Text.Encoding * bool * int -> System.IO.StreamReader
    member BaseStream : System.IO.Stream
    member Close : unit -> unit
    member CurrentEncoding : System.Text.Encoding
    member DiscardBufferedData : unit -> unit
    member EndOfStream : bool
    member Peek : unit -> int
    member Read : unit -> int
    member Read : char [] * int * int -> int
    member ReadLine : unit -> string
    member ReadToEnd : unit -> string
    static val Null : System.IO.StreamReader
  end

Full name: System.IO.StreamReader

  type: IO.StreamReader
  implements: IDisposable
  inherits: IO.TextReader
  inherits: MarshalByRefObject
namespace System.Web
IO.TextReader.ReadToEnd() : string

More information

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