Web上のREST APIを、NSURLSessionを用いて呼び出すAppleScriptの改良版です。
status code、Response Header、処理結果をそれぞれ返すように改良してみました。これで、実際のプログラムに組み込むことができるようになりました。
Response Headerの各フィールドはスペースやハイフンを含んでいたりするので、CocoaのNSDictionaryからAppleScriptのrecordに変換すると値が取り出せなくなってしまいます。
そのため、NSDictionaryのまま返すようにしています。
AppleScript名:GET method REST API v4.1 |
— Created 2018-06-16 by Takaaki Naganoya — 2018 Piyomaru Software 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 set reqURLStr to "http://jsonplaceholder.typicode.com/posts" set aRESTres to callRestGETAPIAndParseResults(reqURLStr, 10) of me set aRESTcode to retCode of aRESTres –> 200 set aRESTheader to retHeaders of aRESTres –> (NSDictionary) {Content-Type:"application/json; charset=utf-8", Pragma:"no-cache", X-Powered-By:"Express", Set-Cookie:"__cfduid=dc7a11359ba7f9518366108f4c2e2d7fb1529215907; expires=Mon, 17-Jun-19 06:11:47 GMT; path=/; domain=.typicode.com; HttpOnly", Server:"cloudflare", Via:"1.1 vegur", Content-Encoding:"gzip", Expires:"Sun, 17 Jun 2018 15:21:12 GMT", CF-Cache-Status:"HIT", Transfer-Encoding:"Identity", Cache-Control:"public, max-age=14400", Date:"Sun, 17 Jun 2018 11:21:12 GMT", Access-Control-Allow-Credentials:"true", Connection:"keep-alive", CF-RAY:"42c5219eb6f2a5cc-NRT", Etag:"W/"6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM"", Vary:"Origin, Accept-Encoding", X-Content-Type-Options:"nosniff"} set aRESTres to retData of aRESTres –> (* {{body:"quia et suscipit suscipit recusandae consequuntur expedita et cum reprehenderit molestiae ut ut quas totam nostrum rerum est autem sunt rem eveniet architecto", id:1, title:"sunt aut facere repellat provident occaecati excepturi optio reprehenderit", userId:1},….} *) on callRestGETAPIAndParseResults(reqURLStr as string, timeoutSec as integer) 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" 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() –Start URL Session repeat (1000 * timeoutSec) 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 callRestGETAPIAndParseResults 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 of string or string –as anything end URLSession:dataTask:didReceiveData: |
More from my site
(Visited 78 times, 1 visits today)