Posted to tcl by Napier at Thu Jun 26 02:05:24 GMT 2014view raw
- namespace eval Web {
-
- variable lastRequest 0
-
- proc Send {args} {
- # Syntax:
- # -method $METHOD -url $URL -params $PARAMS -suffix $URL-Suffix
- # Web::Send -method PATCH -url https://www.url.com -params "Hi Hello" -suffix "devices/hello" -body $json would PATCH:
- # https://www.url.com/devices/hello?auth={AUTH CODE}&hi=hello with the value of $json as the body
- LOG "Beginning Web::Send $args"
- set tempDict $args
- if {$::API::FB_Auth == 0} {LOG "OAuth Required Before Continuing"; #TODO: Build Re-Auth Service}
- if {![dict exists $tempDict -suffix]} {LOG "No Suffix Detected"; dict set tempDict -suffix ""}
- if {![dict exists $tempDict -url]} {LOG "No URL using Default"; dict set tempDict -url $::API::baseURL}
- if {![dict exists $tempDict -method]} {LOG "No Method Defined, Using GET"; dict set tempDict -method "GET"}
- if {[dict exists $tempDict -params]} {LOG "Formatting Query"; dict set tempDict -params "&[::http::formatQuery {*}[dict get $tempDict -params]]"
- } else {LOG "No Params Detected"; dict set tempDict -params ""}
- if {![dict exists $tempDict -port]} {LOG "Custom Port Detected: [dict get $tempDict -port]"; http::register https [dict get $tempDict -port] tls::socket
- } else {http::register https $::API::basePort tls::socket}
- if {![dict exists $tempDict -body]} {LOG "No Body Detected"; dict set tempDict -body ""}
- # set checkRedirect [http::geturl https://developer-api.nest.com/?${::API::FB_Auth}]
- #if {[dict exists [http::meta $checkRedirect] Location]} {
- # dict set tempDict -url [dict get [http::meta $checkRedirect] Location]
- # set qindex [string first "?" [dict get $tempDict -url]]
- # dict set tempDict -url [string range [dict get $tempDict -url] 0 [expr {$qindex-1}]]
- # }
- # http::cleanup $checkRedirect
- if {[dict get $tempDict -method] == "GET"} {set token [http::geturl [dict get $tempDict -url]/[dict get $tempDict -suffix]?${::API::FB_Auth}[dict get $tempDict -params] -command ::Web::httpCallback]
- } else {set token [http::geturl [dict get $tempDict -url]/[dict get $tempDict -suffix]?${::API::FB_Auth}[dict get $tempDict -params] \
- -method [dict get $tempDict -method] -query [dict get $tempDict -body] -command ::Web::httpCallback]
- }
- return
- }
-
- proc httpCallback {token} {
- set response [dict create data [http::data $token] code [http::code $token]]
- LOG "----- RECEIVING DATA -----"
- LOG "$response"
- LOG "--------------------------"
- puts $token
- if {[string match {30[1237]} [::http::ncode $token]]} {LOG "Redirect Required!"; ::Web::startRedirect $token
- } elseif {[::http::ncode $token] == 200} {LOG "Successfull Call Detected"}
- http::cleanup $token
- }
-
- proc startRedirect {token} {
- array set meta [set ${token}(meta)]
- if {![info exist meta(Location)]} {
- LOG "No Redirection Indicator?"; return $token
- }
- array set uri [::uri::split $meta(Location)]
- LOG "URI:"
- LOG "[parray uri]"
- if {$uri(port) == ""} {set uri(port) 443}
- http::register https $uri(port) tls::socket
- unset meta
- if {$uri(host) == ""} {set uri(host) $uri(host)}
- set url [eval ::uri::join [array get uri]]
- set url [string map {":9553" ""} $url]
- LOG "URL: $url"
- set token [http::geturl $url -command onData]
- return $token
- }
-
- }