Posted to tcl by Napier at Thu Jun 26 02:04:31 GMT 2014view raw

  1. proc Send {args} {
  2. # Syntax:
  3. # -method $METHOD -url $URL -params $PARAMS -suffix $URL-Suffix
  4. # Web::Send -method PATCH -url https://www.url.com -params "Hi Hello" -suffix "devices/hello" -body $json would PATCH:
  5. # https://www.url.com/devices/hello?auth={AUTH CODE}&hi=hello with the value of $json as the body
  6. LOG "Beginning Web::Send $args"
  7. set tempDict $args
  8. if {$::API::FB_Auth == 0} {LOG "OAuth Required Before Continuing"; #TODO: Build Re-Auth Service}
  9. if {![dict exists $tempDict -suffix]} {LOG "No Suffix Detected"; dict set tempDict -suffix ""}
  10. if {![dict exists $tempDict -url]} {LOG "No URL using Default"; dict set tempDict -url $::API::baseURL}
  11. if {![dict exists $tempDict -method]} {LOG "No Method Defined, Using GET"; dict set tempDict -method "GET"}
  12. if {[dict exists $tempDict -params]} {LOG "Formatting Query"; dict set tempDict -params "&[::http::formatQuery {*}[dict get $tempDict -params]]"
  13. } else {LOG "No Params Detected"; dict set tempDict -params ""}
  14. if {![dict exists $tempDict -port]} {LOG "Custom Port Detected: [dict get $tempDict -port]"; http::register https [dict get $tempDict -port] tls::socket
  15. } else {http::register https $::API::basePort tls::socket}
  16. if {![dict exists $tempDict -body]} {LOG "No Body Detected"; dict set tempDict -body ""}
  17. # set checkRedirect [http::geturl https://developer-api.nest.com/?${::API::FB_Auth}]
  18. #if {[dict exists [http::meta $checkRedirect] Location]} {
  19. # dict set tempDict -url [dict get [http::meta $checkRedirect] Location]
  20. # set qindex [string first "?" [dict get $tempDict -url]]
  21. # dict set tempDict -url [string range [dict get $tempDict -url] 0 [expr {$qindex-1}]]
  22. # }
  23. # http::cleanup $checkRedirect
  24. 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]
  25. } else {set token [http::geturl [dict get $tempDict -url]/[dict get $tempDict -suffix]?${::API::FB_Auth}[dict get $tempDict -params] \
  26. -method [dict get $tempDict -method] -query [dict get $tempDict -body] -command ::Web::httpCallback]
  27. }
  28. return
  29. }