0 people like it.

Quick and Dirty access to Marvel Comics API

Just something I whipped up to check out the Marvel Comics API using JSON Type Provider

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
//#I @"C:\Projects\lib\FSharp.Data"
//#r "FSharp.Data"

open System.Security.Cryptography
open FSharp.Data
open System.Text

//Get your public and private key from http://developer.marvel.com/ 
[<Literal>]
let publicKey = ""
[<Literal>]
let privateKey = ""
[<Literal>]
let characters = "http://gateway.marvel.com/v1/public/characters"

let md5string (value: string) =
    use x = MD5.Create()
    Encoding.UTF8.GetBytes value
    |> x.ComputeHash
    |> Seq.map (fun x -> x.ToString("x2"))
    |> String.concat ""
    
let marvelHash =
    let seed = ref 0
    fun () ->
        incr seed
        let result = sprintf "%i%s%s" !seed privateKey publicKey |> md5string
        !seed, result

//marvelHash() to generate an initial hash

[<Literal>]
let sample = characters + "?ts=1&apikey=" + publicKey + "&hash=<your inital hash here>"

type Characters = JsonProvider<sample>

let result =
    let seed, hash = marvelHash()
    sprintf "%s?ts=%i&apikey=%s&hash=%s" characters seed publicKey hash
    |> Characters.Load

result.Data.Results
|> Array.map (fun x -> x.Name)
namespace System
namespace System.Security
namespace System.Security.Cryptography
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
namespace System.Text
Multiple items
type LiteralAttribute =
  inherit Attribute
  new : unit -> LiteralAttribute

Full name: Microsoft.FSharp.Core.LiteralAttribute

--------------------
new : unit -> LiteralAttribute
val publicKey : string

Full name: Script.publicKey
val privateKey : string

Full name: Script.privateKey
val characters : string

Full name: Script.characters
val md5string : value:string -> string

Full name: Script.md5string
val value : string
Multiple items
val string : value:'T -> string

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

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
val x : MD5
type MD5 =
  inherit HashAlgorithm
  static member Create : unit -> MD5 + 1 overload

Full name: System.Security.Cryptography.MD5
MD5.Create() : MD5
MD5.Create(algName: string) : MD5
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
property Encoding.UTF8: Encoding
Encoding.GetBytes(s: string) : byte []
Encoding.GetBytes(chars: char []) : byte []
Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
Encoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
Encoding.GetBytes(s: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
Encoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
HashAlgorithm.ComputeHash(buffer: byte []) : byte []
HashAlgorithm.ComputeHash(inputStream: System.IO.Stream) : byte []
HashAlgorithm.ComputeHash(buffer: byte [], offset: int, count: int) : byte []
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val x : byte
System.Byte.ToString() : string
System.Byte.ToString(provider: System.IFormatProvider) : string
System.Byte.ToString(format: string) : string
System.Byte.ToString(format: string, provider: System.IFormatProvider) : string
module String

from Microsoft.FSharp.Core
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val marvelHash : (unit -> int * string)

Full name: Script.marvelHash
val seed : int ref
Multiple items
val ref : value:'T -> 'T ref

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

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val incr : cell:int ref -> unit

Full name: Microsoft.FSharp.Core.Operators.incr
val result : string
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val sample : string

Full name: Script.sample
type Characters = obj

Full name: Script.Characters
type JsonProvider

Full name: FSharp.Data.JsonProvider


<summary>Typed representation of a JSON document.</summary>
       <param name='Sample'>Location of a JSON sample file or a string containing a sample JSON document.</param>
       <param name='SampleIsList'>If true, sample should be a list of individual samples for the inference.</param>
       <param name='RootName'>The name to be used to the root type. Defaults to `Root`.</param>
       <param name='Culture'>The culture used for parsing numbers and dates. Defaults to the invariant culture.</param>
       <param name='Encoding'>The encoding used to read the sample. You can specify either the character set name or the codepage number. Defaults to UTF8 for files, and to ISO-8859-1 the for HTTP requests, unless `charset` is specified in the `Content-Type` response header.</param>
       <param name='ResolutionFolder'>A directory that is used when resolving relative file references (at design time and in hosted execution).</param>
       <param name='EmbeddedResource'>When specified, the type provider first attempts to load the sample from the specified resource
          (e.g. 'MyCompany.MyAssembly, resource_name.json'). This is useful when exposing types generated by the type provider.</param>
       <param name='InferTypesFromValues'>If true, turns on additional type inference from values.
          (e.g. type inference infers string values such as "123" as ints and values constrained to 0 and 1 as booleans.)</param>
val result : obj

Full name: Script.result
val seed : int
val hash : string
namespace Microsoft.FSharp.Data
module Array

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.Array.map
val x : obj
Raw view Test code New version

More information

Link:http://fssnip.net/mC
Posted:9 years ago
Author:David Grenier
Tags: typeprovider , json , marvel