— Created 2016-03-23 by Takaaki Naganoya
— 2016 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use BridgePlus : script "BridgePlus" –https://www.macosxautomation.com/applescript/apps/BridgePlus.html
set accessToken to retAccessToken() —Access Token
set reqURLStr to "https://slack.com/api/api.test"
set aRes to callRestPOSTAPIAndParseResults(reqURLStr, accessToken) of me
set aRESTres to json of aRes
set aRESCode to responseCode of aRes
–> 200
set aRESHeader to responseHeader of aRes
–> {Content-Type:"application/json; charset=utf-8", Access-Control-Allow-Origin:"*", X-Amz-Cf-Id:"zOmILG860ZDWHHwDA5h7HtLNDqYd8nlQyL-Wf_dKfC-b5jDKkr_p8A==", Content-Security-Policy:"referrer no-referrer;", Content-Encoding:"gzip", Server:"Apache", X-XSS-Protection:"0", Transfer-Encoding:"Identity", Via:"1.1 f8ebfacf2280f8de661be7a1f87ba74e.cloudfront.net (CloudFront)", Date:"Tue, 19 Apr 2016 06:16:37 GMT", Strict-Transport-Security:"max-age=31536000; includeSubDomains; preload", Connection:"keep-alive", X-Content-Type-Options:"nosniff", Vary:"Accept-Encoding", X-Cache:"Miss from cloudfront"}
aRESTres as list of string or string
–> {ok:1}
–POST methodのREST APIを呼ぶ
on callRestPOSTAPIAndParseResults(aURL, anAPIkey)
load framework
set aRequest to current application’s NSMutableURLRequest’s requestWithURL:(current application’s |NSURL|’s URLWithString:aURL)
aRequest’s setHTTPMethod:"POST"
aRequest’s setCachePolicy:(current application’s NSURLRequestReloadIgnoringLocalCacheData)
aRequest’s setHTTPShouldHandleCookies:false
aRequest’s setTimeoutInterval:60
aRequest’s setValue:anAPIkey forHTTPHeaderField:"Authorization"
–aRequest’s setValue:jsonString forHTTPHeaderField:"Dropbox-API-Arg"
–aRequest’s setValue:"application/json" forHTTPHeaderField:"Content-Type"
–aRequest’s setHTTPBody:dataJson
–CALL REST API
set aRes to current application’s NSURLConnection’s sendSynchronousRequest:aRequest returningResponse:(reference) |error|:(missing value)
–Parse Results
set resList to aRes as list
set bRes to contents of (first item of resList)
set resStr to current application’s NSString’s alloc()’s initWithData:bRes encoding:(current application’s NSUTF8StringEncoding)
log resStr
set jsonString to current application’s NSString’s stringWithString:resStr
set jsonData to jsonString’s dataUsingEncoding:(current application’s NSUTF8StringEncoding)
set aJsonDict to current application’s NSJSONSerialization’s JSONObjectWithData:jsonData options:0 |error|:(missing value)
–Get Response Code
set dRes to contents of second item of resList
set resCode to ASify from (dRes’s statusCode())
–Get Response Header
set resHeaders to ASify from (dRes’s allHeaderFields())
return {json:aJsonDict, responseCode:resCode, responseHeader:resHeaders}
end callRestPOSTAPIAndParseResults