Skip to content

Instantly share code, notes, and snippets.

@sowderca
Created April 3, 2019 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sowderca/89595f69043af196da080a9bc4ea818c to your computer and use it in GitHub Desktop.
Save sowderca/89595f69043af196da080a9bc4ea818c to your computer and use it in GitHub Desktop.
connectwise
s (51 sloc) 2.49 KB
RawBlameHistory
use AppleScript version "2.5"
use scripting additions
use framework "Foundation"
property |NSURL| : a reference to current application's |NSURL|
property NSString : a reference to current application's NSString
property NSURLSession : a reference to current application's NSURLSession
property NSJSONSerialization : a reference to current application's NSJSONSerialization
property NSMutableURLRequest : a reference to current application's NSMutableURLRequest
property NSUTF8StringEncoding : a reference to current application's NSUTF8StringEncoding
property NSURLSessionConfiguration : a reference to current application's NSURLSessionConfiguration
property retData : missing value
property retCode : 0
property retHeaders : 0
property APIKey : (system attribute "CW_MANAGE_API_KEY") as string
on fetchTicket(ticketID as number)
set hostURL to "https://connect.vc3.com/v4_6_release/apis/3.0/service/tickets/" as string
set reqURLStr to hostURL & ticketID as string
set retData to missing value
set retCode to 0
set retHeaders to {}
set aURL to |NSURL|'s URLWithString:reqURLStr
set aRequest to NSMutableURLRequest's requestWithURL:aURL
aRequest's setHTTPMethod:"GET"
aRequest's setValue:"gzip" forHTTPHeaderField:"Content-Encoding"
aRequest's setValue:"application/json; charset=UTF-8" forHTTPHeaderField:"Content-Type"
aRequest's setValue:APIKey forHTTPHeaderField:"Authorization"
set aConfig to NSURLSessionConfiguration's defaultSessionConfiguration()
set aSession to NSURLSession's sessionWithConfiguration:aConfig delegate:(me) delegateQueue:(missing value)
set aTask to aSession's dataTaskWithRequest:aRequest
set hitF to false
aTask's resume()
repeat (1000 * 10) times
if retData is not equal to missing value then
set hitF to true
exit repeat
end if
delay ("0.001" as real)
end repeat
if hitF = false then error "REST API Timeout Error"
return {retData:retData, retCode:retCode, retHeaders:retHeaders}
end fetchTicket
on URLSession:tmpSession dataTask:tmpTask didReceiveData:tmpData
set aRes to tmpTask's response()
set retCode to aRes's statusCode()
set retHeaders to aRes's allHeaderFields()
set resStr to NSString's alloc()'s initWithData:tmpData encoding:(NSUTF8StringEncoding)
set jsonString to NSString's stringWithString:(resStr)
set jsonData to jsonString's dataUsingEncoding:(NSUTF8StringEncoding)
set aJsonDict to NSJSONSerialization's JSONObjectWithData:jsonData options:0 |error|:(missing value)
set retData to aJsonDict as list
end URLSession:dataTask:didReceiveData:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment