Linq2vk / src / VkontakteAPI / Linq2vk / Api.fs

namespace Linq2vk

open System
open System.Text

module Api =

    let BaseUrl = "http://api.vk.com/api.php?"
    let SupportedFormat = "JSON"
    let SupportedVersion = "3.0"

    type Handle = {
        AppId : int64;
        Uid : int64;
        Secret : string;
        Sid : string;
        Expire : DateTime;
    }

    [<AutoOpen>]
    module HelperFunctions =

        let createSignature handle query =
            let sortedQuery = Query.sort query
            sortedQuery
                |> Query.joinWith (fun (k, v) -> sprintf "%s=%s" k v)
                |> (fun args -> sprintf "%d%s%s" (handle.Uid) args (handle.Secret))
                |> DataTools.md5

        let cleanQuery =
            let autogeneratedKeys =
                Set.ofList ["api_id"; "format"; "sig"; "sid"; "v"]
            let shouldRemain k =
                not <| Set.contains k autogeneratedKeys
            Query.filterKeys shouldRemain

    let create appid uid secret sid expire = {
        AppId = appid;
        Uid = uid;
        Secret = secret;
        Sid = sid;
        Expire = DataTools.fromUnixtime expire
    }

    // Query should only contain the significant data related to an API method call
    // api_id, format, sid, sig and v attributes are added automatically
    // see HelperFunctions.cleanQuery for details
    let createQueryString handle query =
        let cleanQ = cleanQuery query
        let signature = createSignature handle cleanQ
        let auxArgs = [
            ("app_id", string handle.AppId);
            ("format", SupportedFormat);
            ("sid", handle.Sid);
            ("v", SupportedVersion);
            ("sig", signature);
        ]
        let resultQ =
            List.fold (fun q (k, v) -> Query.add k v q) cleanQ auxArgs
        Query.toQueryString resultQ
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.