REST API呼び出しに欠かせない部品をアップデートしました。macOS 10.15でクラッシュしないように書き換えています。
–> GET method REST API v4.4
–> GET method REST API v4.3
–> GET method REST API v4.1
–> GET method REST API v4
前バージョンではmacOS 10.15上でクラッシュするかわりにmacOS 10.14上で高速という特徴がありました。ただ、macOS 10.15上でクラッシュしないように調整を行っていくと、処理速度については別段速くもなく(キャッシュがヒットしていない雰囲気)、よりいっそうキャッシュについて調べる必要が出てきています。
一応、キャッシュの作り方については調べてみたものの、
キャッシュが効いている雰囲気がまったくありません。
キャッシュの内容を確認してみると、からっぽですね(^ー^; もう少し(時間のあるときに)キャッシュの連携などを調べておくといいのかもしれません。最低限、macOS 10.15でクラッシュしなくなったので、NSURLConnectionを呼び出してリジェクトを喰らわないように準備しておくという感じでしょうか。
AppleScript名:GET method REST API v4.4.1_wikipedia.scptd |
— Created 2019-05-02 by Takaaki Naganoya — Modified 2020-04-02 by Takaaki Naganoya — 2020 Piyomaru Software use AppleScript version "2.5" use scripting additions use framework "Foundation" use framework "AppKit" 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 NSMutableData : a reference to current application’s NSMutableData 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 drecF : false property aSession : missing value property aCache : missing value on run set retData to missing value set drecF to false set aSession to missing value set aQueryKeyTitle to "AppleScript" set aRes to getWikiText(aQueryKeyTitle) of me return aRes end run on getWikiText(aQueryKeyTitle) set reqURLStr to "https://en.wikipedia.org/w/api.php" set aRec to {action:"parse", page:aQueryKeyTitle, |prop|:"wikitext", format:"json"} set aURL to retURLwithParams(reqURLStr, aRec) of me set aRes to callRestGETAPIAndParseResults(aURL, 5) of me if aRes = missing value then return "Error" set bRes to (aRes’s valueForKeyPath:"parse.wikitext.*") as string return bRes end getWikiText –GET methodのREST APIを呼ぶ on callRestGETAPIAndParseResults(reqURLStr as string, timeoutSec as integer) set (my retData) to NSMutableData’s alloc()’s init() set (my retCode) to 0 set (my retHeaders) to {} set (my drecF) to false –if my aCache = missing value then set cachePath to (POSIX path of (path to library folder from user domain)) & "/Caches/AppleScriptURLCache" set my aCache to current application’s NSURLCache’s alloc()’s initWithMemoryCapacity:512000 diskCapacity:1024 * 1024 * 5 diskPath:cachePath –end if current application’s NSURLCache’s setSharedURLCache:(my aCache) set aURL to |NSURL|’s URLWithString:reqURLStr set aRequest to NSMutableURLRequest’s requestWithURL:aURL aRequest’s setHTTPMethod:"GET" aRequest’s setTimeoutInterval:timeoutSec aRequest’s setValue:"gzip" forHTTPHeaderField:"Content-Encoding" aRequest’s setValue:"AppleScript/Cocoa" forHTTPHeaderField:"User-Agent" aRequest’s setValue:"application/json; charset=UTF-8" forHTTPHeaderField:"Content-Type" set v2 to system attribute "sys2" –> 14 if v2 < 15 then –macOS 10.14まで set aConfig to NSURLSessionConfiguration’s defaultSessionConfiguration() else –macOS 10.15以降。10.15でdefaultSessionConfigurationを使うとクラッシュしやすい??? set identifier to "BackgroundSessionConfiguration" set aConfig to NSURLSessionConfiguration’s backgroundSessionConfiguration:(identifier) end if aConfig’s setRequestCachePolicy:(current application’s NSURLRequestReturnCacheDataElseLoad) aConfig’s setURLCache:(my aCache) –aConfig’s setTimeoutIntervalForRequest:0.5 –aConfig’s setTimeoutIntervalForResource:timeoutSec –aConfig’s setWaitsForConnectivity:false –どちらでも速度差がない set my aSession to NSURLSession’s sessionWithConfiguration:aConfig delegate:(me) delegateQueue:(current application’s NSOperationQueue’s mainQueue()) –set my aSession to NSURLSession’s sessionWithConfiguration:aConfig delegate:(me) delegateQueue:(missing value) set aTask to aSession’s dataTaskWithRequest:aRequest aTask’s resume() –Start URL Session repeat (1000 * timeoutSec) times if (my drecF) is not equal to false then exit repeat end if delay "0.001" as real end repeat –aSession’s invalidateAndCancel() –delegateの無効化 my aSession’s finishTasksAndInvalidate() set my aSession to missing value return my parseSessionResults() end callRestGETAPIAndParseResults on URLSession:tmpSession dataTask:tmpTask didReceiveData:tmpData (my retData)’s appendData:tmpData end URLSession:dataTask:didReceiveData: on URLSession:tmpSession dataTask:tmpTask willCacheResponse:cacheRes completionHandler:aHandler set (my drecF) to true end URLSession:dataTask:willCacheResponse:completionHandler: on URLSession:tmpSession task:tmpTask didCompleteWithError:tmpError if tmpError = missing value then set (my drecF) to true else error "Donwload Failed" end if end URLSession:task:didCompleteWithError: on parseSessionResults() set resStr to NSString’s alloc()’s initWithData:(my retData) 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) return aJsonDict end parseSessionResults on retURLwithParams(aBaseURL, aRec) set aDic to current application’s NSMutableDictionary’s dictionaryWithDictionary:aRec set aKeyList to (aDic’s allKeys()) as list set aValList to (aDic’s allValues()) as list set aLen to length of aKeyList set qList to {} repeat with i from 1 to aLen set aName to contents of item i of aKeyList set aVal to contents of item i of aValList set the end of qList to (current application’s NSURLQueryItem’s queryItemWithName:aName value:aVal) end repeat set aComp to current application’s NSURLComponents’s alloc()’s initWithString:aBaseURL aComp’s setQueryItems:qList set aURL to (aComp’s |URL|()’s absoluteString()) as text return aURL end retURLwithParams |
More from my site
(Visited 129 times, 1 visits today)
NSURLSessionでREST API呼び出しv4.4.2a – AppleScriptの穴 says:
[…] NSURLSessionでREST API呼び出しv4.4.1 […]