Posted to tcl by Napier at Thu Jun 26 02:05:24 GMT 2014view raw

  1. namespace eval Web {
  2.  
  3. variable lastRequest 0
  4.  
  5. proc Send {args} {
  6. # Syntax:
  7. # -method $METHOD -url $URL -params $PARAMS -suffix $URL-Suffix
  8. # Web::Send -method PATCH -url https://www.url.com -params "Hi Hello" -suffix "devices/hello" -body $json would PATCH:
  9. # https://www.url.com/devices/hello?auth={AUTH CODE}&hi=hello with the value of $json as the body
  10. LOG "Beginning Web::Send $args"
  11. set tempDict $args
  12. if {$::API::FB_Auth == 0} {LOG "OAuth Required Before Continuing"; #TODO: Build Re-Auth Service}
  13. if {![dict exists $tempDict -suffix]} {LOG "No Suffix Detected"; dict set tempDict -suffix ""}
  14. if {![dict exists $tempDict -url]} {LOG "No URL using Default"; dict set tempDict -url $::API::baseURL}
  15. if {![dict exists $tempDict -method]} {LOG "No Method Defined, Using GET"; dict set tempDict -method "GET"}
  16. if {[dict exists $tempDict -params]} {LOG "Formatting Query"; dict set tempDict -params "&[::http::formatQuery {*}[dict get $tempDict -params]]"
  17. } else {LOG "No Params Detected"; dict set tempDict -params ""}
  18. if {![dict exists $tempDict -port]} {LOG "Custom Port Detected: [dict get $tempDict -port]"; http::register https [dict get $tempDict -port] tls::socket
  19. } else {http::register https $::API::basePort tls::socket}
  20. if {![dict exists $tempDict -body]} {LOG "No Body Detected"; dict set tempDict -body ""}
  21. # set checkRedirect [http::geturl https://developer-api.nest.com/?${::API::FB_Auth}]
  22. #if {[dict exists [http::meta $checkRedirect] Location]} {
  23. # dict set tempDict -url [dict get [http::meta $checkRedirect] Location]
  24. # set qindex [string first "?" [dict get $tempDict -url]]
  25. # dict set tempDict -url [string range [dict get $tempDict -url] 0 [expr {$qindex-1}]]
  26. # }
  27. # http::cleanup $checkRedirect
  28. 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]
  29. } else {set token [http::geturl [dict get $tempDict -url]/[dict get $tempDict -suffix]?${::API::FB_Auth}[dict get $tempDict -params] \
  30. -method [dict get $tempDict -method] -query [dict get $tempDict -body] -command ::Web::httpCallback]
  31. }
  32. return
  33. }
  34.  
  35. proc httpCallback {token} {
  36. set response [dict create data [http::data $token] code [http::code $token]]
  37. LOG "----- RECEIVING DATA -----"
  38. LOG "$response"
  39. LOG "--------------------------"
  40. puts $token
  41. if {[string match {30[1237]} [::http::ncode $token]]} {LOG "Redirect Required!"; ::Web::startRedirect $token
  42. } elseif {[::http::ncode $token] == 200} {LOG "Successfull Call Detected"}
  43. http::cleanup $token
  44. }
  45.  
  46. proc startRedirect {token} {
  47. array set meta [set ${token}(meta)]
  48. if {![info exist meta(Location)]} {
  49. LOG "No Redirection Indicator?"; return $token
  50. }
  51. array set uri [::uri::split $meta(Location)]
  52. LOG "URI:"
  53. LOG "[parray uri]"
  54. if {$uri(port) == ""} {set uri(port) 443}
  55. http::register https $uri(port) tls::socket
  56. unset meta
  57. if {$uri(host) == ""} {set uri(host) $uri(host)}
  58. set url [eval ::uri::join [array get uri]]
  59. set url [string map {":9553" ""} $url]
  60. LOG "URL: $url"
  61. set token [http::geturl $url -command onData]
  62. return $token
  63. }
  64.  
  65. }