ã¹ã¿ãŒãã¢ããã®äººçã¯å€åã«æºã¡ãŠããããããžã§ã¯ãã®éåœã決å®ã«äŸåããã¿ãŒãã³ã°ãã€ã³ãã§ãã ã³ãŒãããŒã¹ãæãå€æ§ãªæ±ºå®ã®å ·äœåãæäŸã§ããªãå Žåãããªãã¯è² ããŸãã ãµãŒããŒã³ãŒãã¯ãããžãã¹ã®ããŒãºã«çæéã§é©å¿ããã®ã«ååãªæè»æ§ãåããŠããããããžã§ã¯ãã«ãªããã©ããã決å®ããŸãã æåããã¹ã¿ãŒãã¢ããã¯ãåã«çŽ æŽãããã¢ã€ãã¢ãæäŸããã ãã§ã¯ãªãã質ã®é«ãæ¹æ³ã§ãããå®è£ ããããšãã§ããã¹ã¿ãŒãã¢ããã§ãã ã¹ã¿ãŒãã¢ããã®æåã¯ã補åãiOSã¢ããªã±ãŒã·ã§ã³ããµãŒãã¹ãAPIã®ãããã§ãããã«é¢ä¿ãªãã補åã®æåã«äŸåããŸãã éå»3幎éãç§ã¯WebãµãŒãã¹ã䜿çšããŠiOSïŒäž»ã«ã¹ã¿ãŒãã¢ããåãïŒã®ããŸããŸãªã¢ããªã±ãŒã·ã§ã³ã«åãçµãã§ããŸããããã®ããã°ã§ã¯ãèç©ãããç¥èããŸãšããŠåéããRESTful APIãéçºããéã«åŸãã¹ãæé«ã®ãã¯ããã¯ã玹ä»ããããšããŸããã åªããRESTful APIã¯ãç°¡åãã€ç°¡åã«å€æŽã§ããAPIã§ãã
察象èªè
ãã®æçš¿ã¯ãäžãé«åºŠãªRESTful APIã®éçºã«é¢ããç¥èããæã¡ã®æ¹ã察象ãšããŠããŸãã Java / Ruby / Scalaãªã©ã®ãµãŒããŒèšèªã§ã®ãªããžã§ã¯ãæåïŒãŸãã¯æ©èœïŒããã°ã©ãã³ã°ã®åºæ¬çãªç¥èãå¿ èŠã§ãã ïŒPHPãŸãã¯Programmable Hyperlinked Pastaã¯æå³çã«ç¡èŠããŸããïŒã
ã泚æ ããã ããã§ãèè ã¯ãPHPãProgrammable Hyperlinked PastaïŒProgrammable Hyperlink NoodlesïŒãšããŠè§£èªãããããã°ã©ãã³ã°èšèªã®æŽå²ã«é¢ããååè«ã®èšäºãžã®ãªã³ã¯ãæäŸããŸããã PHPã«å¯Ÿããèè ã®æ 床ã®ç¹åŸŽã¯äœã§ããããïŒ
èšäºã®æ§é ãšæ§æ
ãã®èšäºã¯éåžžã«è©³çŽ°ã§ã2ã€ã®éšåã§æ§æãããŠããŸãã æåã¯RESTã®åºæ¬ã説æãã2çªç®ã¯APIã®ããŸããŸãªããŒãžã§ã³ã®ããã¥ã¡ã³ããšãµããŒãã説æããŸãã åå¿è åãã®ååãããåãã®åŸåã ããªããããã§ããããšã¯ééããªãã®ã§ãããã§ãDocumenting APIãã®ç« ã«çŽæ¥ãžã£ã³ãããããã®ãªã³ã¯ã瀺ããŸãã ãå€ãã®bukafãniasilil ...ãã®ã«ããŽãªããã®ãã®æçš¿ãšæãããå Žåã¯ãããããããããéå§ããå¿ èŠããããŸãã
RESTfulã®åå
ãµãŒããŒãRESTã®ååã«æºæ ããŠããå ŽåããµãŒããŒã¯RESTfulãšèŠãªãããšãã§ããŸãã äž»ã«ã¢ãã€ã«ããã€ã¹ã§äœ¿çšãããAPIãéçºããå Žåã3ã€ã®æãéèŠãªååãç解ããŠããã«åŸãããšã¯éåžžã«åœ¹ç«ã¡ãŸãã ãŸããAPIã®éçºäžã ãã§ãªããå°æ¥ã®ãµããŒããšéçºãå«ãŸããŸãã ããã§ã¯å§ããŸãããã
ç¡åœç±
æåã®ååã¯ãåœå®¶ã®ç¬ç«ã§ãã ç°¡åã«èšãã°ãRESTfulãµãŒããŒã¯ãã¯ã©ã€ã¢ã³ãã®çŸåšã®ã³ã³ããã¹ãæ å ±ãç£èŠãä¿åãããã«äœ¿çšãã¹ãã§ã¯ãããŸããã äžæ¹ãã¯ã©ã€ã¢ã³ãã¯ãã®ã¿ã¹ã¯ãåŒãåããå¿ èŠããããŸãã ã€ãŸããAPIã䜿çšããŠã¢ãã€ã«ããã€ã¹ã®ç¶æ ããµãŒããŒã«èšæ¶ãããªãã§ãã ããã
New Facebookãšããã¹ã¿ãŒãã¢ããããããšæ³åããŠã¿ãŸãããã éçºè ãééããç¯ãå¯èœæ§ã®ããè¯ãäŸã¯ãã¢ãã€ã«ããã€ã¹ãã¹ããªãŒã å ã®æåŸã®èªã¿åãé ç®ãèšå®ã§ããããã«ããAPIåŒã³åºããæäŸããããšã§ãïŒFacebookãã£ãŒããšåŒã³ãŸãããïŒã éåžžãã£ãŒããè¿ãAPIåŒã³åºãïŒåŒã³åºã/ãã£ãŒãããŸãããïŒã¯ãèšå®ããããã®ãããæ°ããèŠçŽ ãè¿ãããã«ãªããŸããã è³¢ãã§ããã ã¯ã©ã€ã¢ã³ããšãµãŒããŒéã®ããŒã¿äº€æããæé©åãããŸãããïŒ ããããéããŸãã
ãã®å Žåã«åé¡ã«ãªãå¯èœæ§ãããã®ã¯ããŠãŒã¶ãŒã2ã€ãŸãã¯3ã€ã®ããã€ã¹ãããµãŒãã¹ã䜿çšããå Žåããã®ãã¡ã®1ã€ãæåŸã«èªã¿åã£ãã¢ã€ãã ãã€ã³ã¹ããŒã«ãããšãæ®ãã¯ä»ã®ããã€ã¹ã§èªã¿åã£ããã£ãŒãã¢ã€ãã ãããŒãã§ããªããªãããšã§ã以åã
ç¶æ ã®ç¬ç«æ§ãšã¯ãç¹å®ã®APIåŒã³åºãã«ãã£ãŠè¿ãããããŒã¿ãã以åã«è¡ãããåŒã³åºãã«äŸåããªãããšãæå³ããŸãã
ãã®åŒã³åºããæé©åããæ£ããæ¹æ³ã¯ããã£ãŒããè¿ãAPIåŒã³åºãã«åŒæ°ãšããŠæåŸã«èªã¿åãããããŒããšã³ããªãäœæãããæéãæž¡ãããšã§ãïŒ/ feedïŒLastFeed = 20120228ïŒã If-Modified-Since HTTPããããŒã䜿çšããå¥ã®ããããæ£ãããã¡ãœããããããŸãã ããããç§ãã¡ã¯ãŸã ãã®æ¹åã«ã¯æ·±ãå ¥ããŸããã ããã«ã€ããŠã¯ç¬¬2éšã§èª¬æããŸãã
äžæ¹ãã¯ã©ã€ã¢ã³ãã¯ããµãŒããŒã«ã¢ã¯ã»ã¹ãããšãã«ãµãŒããŒã§çæããããã©ã¡ãŒã¿ãŒãèšæ¶ãïŒå¿ èŠïŒãå¿ èŠã«å¿ããŠåŸç¶ã®APIåŒã³åºãã«äœ¿çšããããšãã§ããŸãã
ãã£ãã·ã¥å¯èœãªéå±€åã¢ãŒããã¯ãã£
2çªç®ã®ååã¯ããµãŒããŒå¿çãäžå®æéãã£ãã·ã¥ãããµãŒããŒãžã®æ°ããèŠæ±ãªãã§åå©çšã§ãããšããæ å ±ãã¯ã©ã€ã¢ã³ãã«æäŸããããšã§ãã ãã®ã¯ã©ã€ã¢ã³ãã¯ãã¢ãã€ã«ããã€ã¹èªäœãŸãã¯äžéãããã·ãµãŒããŒã®ããããã§ãã ãã£ãã·ã³ã°ã«ã€ããŠã¯ã第2éšã§è©³ãã説æããŸãã
ã¯ã©ã€ã¢ã³ã-ãµãŒããŒã®ããŒãã£ã·ã§ã³åå²ãšåäžã®ã€ã³ã¿ãŒãã§ãŒã¹
RESTfulãµãŒããŒã¯ããã®å®è£ ã®è©³çŽ°ãã§ããã ãã¯ã©ã€ã¢ã³ãããé ãå¿ èŠããããŸãã ã¯ã©ã€ã¢ã³ãã¯ãã©ã®DBMSããµãŒããŒã§äœ¿çšãããŠãããããŸãã¯çŸåšãªã¯ãšã¹ãããã®ä»ã®é¡äŒŒã®ãã®ãåŠçããŠãããµãŒããŒã®æ°ãç¥ããªãã¯ãã§ãã ãããžã§ã¯ããæ¥éã«äººæ°ãåãå§ããå Žåãæ©èœãé©åã«åé¢ããããšã¯ã¹ã±ãŒã«ã¢ããã«ãšã£ãŠéèŠã§ãã
ãããã¯ãããããRESTfulãµãŒããŒã®éçºäžã«åŸãã¹ã3ã€ã®æãéèŠãªååã§ãã 以äžã«ãéèŠæ§ã®äœã3ã€ã®ååã«ã€ããŠèª¬æããŸããããããã¯ãã¹ãŠãããã§è©±ããŠããããšã«çŽæ¥é¢é£ããŠããŸãã
RESTãªã¯ãšã¹ããš4ã€ã®HTTPã¡ãœãã
ã²ãã
æçš¿
眮ã
åé€
ããã£ãã·ã¥å¯èœæ§ããšGETãªã¯ãšã¹ãã®åå
èŠããŠããã¹ãäž»ãªããšã¯ãGETãä»ããŠè¡ãããåŒã³åºãããµãŒããŒã®ç¶æ ãå€æŽããªãããšã§ãã ããã¯ããªã¯ãšã¹ããä»»æã®äžéãããã·ã§ãã£ãã·ã¥ã§ããããšãæå³ããŸãïŒè² è·åæžïŒã ãããã£ãŠããµãŒããŒéçºè ã¯ãããŒã¿ããŒã¹å ã®ããŒã¿ãå€æŽããGETã¡ãœãããå ¬éããªãã§ãã ããã ããã¯ãRESTfulã®å²åŠãç¹ã«äžèšã®2çªç®ã®ãã€ã³ãã«éåããŠããŸãã GETåŒã³åºãã§access.logã«ãšã³ããªãæ®ãããããæåŸã«ãã°ã€ã³ããããªã©ã®ããŒã¿ãæŽæ°ãããããããšã¯ã§ããŸããã ããŒã¿ããŒã¹å ã®ããŒã¿ãå€æŽããå Žåã¯ãPOST / PUTã¡ãœããã§ããå¿ èŠããããŸãã
POSTãšPUTã®åãè°è«
HTTP 1.1ä»æ§ã¯ãPUTãã¹ãçã§ããããšã瀺ããŠããŸãã ããã¯ãã¯ã©ã€ã¢ã³ãã1ã€ã®URIã§å€ãã®PUTãªã¯ãšã¹ããå®è¡ã§ããããšãæå³ããããã«ããéè€ã¬ã³ãŒãã®äœæã¯çºçããŸããã ä»£å ¥æŒç®ã¯ãã¹ãçæŒç®ã®è¯ãäŸã§ã
String userId = this.request["USER_ID"];
ãã®æäœã2åãŸãã¯3åå®è¡ãããŠãã害ã¯ãããŸããïŒäœåãªããã»ããµãµã€ã¯ã«ãé€ãïŒã äžæ¹ãPOSTã¯içã§ã¯ãããŸããã ããã¯å°ãå¢å ããŸãã å®è¡ãããŠããã¢ã¯ã·ã§ã³ãã¹ãçã§ãããã©ãããèæ ®ããŠãPOSTãŸãã¯PUTã䜿çšããå¿ èŠããããŸãã ããã°ã©ããŒã®èšèªã§ã¯ãã¯ã©ã€ã¢ã³ããäœæããããªããžã§ã¯ãã®URLãç¥ã£ãŠããå ŽåãPUTã䜿çšããŸãã ã¯ã©ã€ã¢ã³ããç®çã®ãªããžã§ã¯ããäœæããã¡ãœãã/ã¯ã©ã¹ã®URLãç¥ã£ãŠããå Žåã¯ãPOSTã䜿çšããŸãã
PUT www.example.com/post/1234
ã¯ã©ã€ã¢ã³ããURIãç¥ã£ãŠããå Žåã¯ãPUTã䜿çšããŸããURIèªäœã¯ãªã¯ãšã¹ãã®çµæã§ããå¯èœæ§ããããŸãã ã¯ã©ã€ã¢ã³ãããã®PUTã¡ãœãããäœåºŠãåŒã³åºãããšããŠãã害ãéè€ãšã³ããªã¯äœæãããŸããã
POST www.example.com/createpost
ãµãŒããŒèªäœãäžæã®èå¥åãäœæãããããã¯ã©ã€ã¢ã³ãã«è¿ãå Žåã¯ãPOSTã䜿çšããŸãã ãã®ãªã¯ãšã¹ããåããã©ã¡ãŒã¿ã§åŸã§ç¹°ãè¿ããããšãéè€ããã¬ã³ãŒããäœæãããŸãã
ãã®è°è«ã®è©³çŽ°ã
DELETEã¡ãœãã
DELETEã¯å®å šã«äžæã§ãã PUTã®ãããªã¹ãçã§ãããã¬ã³ãŒããååšããå Žåã¯åé€ããããã«äœ¿çšããå¿ èŠããããŸãã
RESTã®åç
RESTfulãµãŒããŒããã®å¿çã¯ãXMLãŸãã¯JSON圢åŒãšããŠäœ¿çšã§ããŸãã å人çã«ã¯ãåãå¿çãXML圢åŒã§éä¿¡ããå Žåãããç°¡æœã§ããããã¯ãŒã¯çµç±ã§éä¿¡ãããããŒã¿ãå°ãªããããJSONã奜ã¿ãŸãã éãã¯æ°çŸãããã€ãçšåºŠã§ããã3Gã®é床ãšã¢ãã€ã«ããã€ã¹ãšã®äº€æã®äžå®å®æ§ãèæ ®ãããšããããã®æ°çŸãããã€ããéããçãå¯èœæ§ããããŸãã
èªèšŒ
èªèšŒã¯httpsã§è¡ãããã¯ã©ã€ã¢ã³ãã¯æå·åããã圢åŒã§ãã¹ã¯ãŒããéä¿¡ããå¿ èŠããããŸãã Objective-Cã§NSStringããã·ã¥ã®sha1ãååŸããããã»ã¹ã¯éåžžã«æ確ã§åçŽã§ãããäžèšã®ã³ãŒãã¯ãããæ確ã«ç€ºããŠããŸãã
- (NSString *) sha1 { const char *cstr = [self cStringUsingEncoding:NSUTF8StringEncoding]; NSData *data = [NSData dataWithBytes:cstr length:self.length]; uint8_t digest[CC_SHA1_DIGEST_LENGTH]; CC_SHA1(data.bytes, data.length, digest); NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2]; for(int i = 0; i <; CC_SHA1_DIGEST_LENGTH; i++) [output appendFormat:@"%02x", digest[i]]; return output; }
ãµãŒããŒã¯ãåä¿¡ãããã¹ã¯ãŒãããã·ã¥ãããŒã¿ããŒã¹ã«ä¿åãããŠããããã·ã¥ãšæ¯èŒããå¿ èŠããããŸãã ãããã«ãããã¯ã©ã€ã¢ã³ããããµãŒããŒã«ãã¹ã¯ãŒããå¹³æã§æž¡ããªãã§ãã ããã ãã®ã«ãŒã«ã«ã¯äŸå€ã¯ãããŸããïŒ ãã¹ã¯ãŒããéãããŸãŸã«ããŠããããšããŠãŒã¶ãŒã«ãããæ¥ã¯ãã¹ã¿ãŒãã¢ããã®æçµæ¥ãããããŸããã äžåºŠå€±ããããšãä¿¡é Œãå埩ããããšã¯ã§ããŸããã
RFC 2617ã¯ãHTTPãµãŒããŒã§ã®èªèšŒã®2ã€ã®æ¹æ³ã«ã€ããŠèª¬æããŠããŸãã æåã¯åºæ¬ã¢ã¯ã»ã¹ã2çªç®ã¯ãã€ãžã§ã¹ãã§ãã ã¢ãã€ã«ã¯ã©ã€ã¢ã³ãã®å Žåãããã2ã€ã®æ¹æ³ã®ãããããé©åã§ãããã»ãšãã©ã®ãµãŒããŒïŒããã³ã¯ã©ã€ã¢ã³ããïŒèšèªã«ã¯ããã®ãããªèªèšŒã¹ããŒã ãå®è£ ããããã®çµã¿èŸŒã¿ã¡ã«ããºã ããããŸãã
APIãå ¬éããäºå®ãããå Žåã¯ãoAuthãŸãã¯ããåªããoAuth 2.0ãæ€èšããå¿ èŠããããŸãã oAuthã䜿çšãããšããŠãŒã¶ãŒã¯ããŒïŒãã°ã€ã³/ãã¹ã¯ãŒãïŒã亀æããããšãªããã¢ããªã±ãŒã·ã§ã³ã§äœæãããã³ã³ãã³ããä»ã®ãªãœãŒã¹ã«å ¬éã§ããŸãã ãŸããoAuthã䜿çšãããšããŠãŒã¶ãŒã¯æ£ç¢ºã«äœ¿çšã§ãããã®ãšããµãŒãããŒãã£ã®ãªãœãŒã¹ã«ä»äžããæš©éãå¶åŸ¡ã§ããŸãã
Facebook Graph APIã¯ãçŸæç¹ã§æãéçºãããæ®åããŠããoAuthã®å®è£ ã§ãã FacebookãŠãŒã¶ãŒã¯ãoAuthã䜿çšããŠãä»ã®å人æ å ±ãèå¥æ å ±ïŒãŠãŒã¶ãŒå/ãã¹ã¯ãŒãïŒãå ¬éããããšãªãããµãŒãããŒãã£ã®ã¢ããªã±ãŒã·ã§ã³ãšåçãå ±æã§ããŸãã ãŠãŒã¶ãŒã¯ããã¹ã¯ãŒããå€æŽããããšãªããäžèŠãªã¢ããªã±ãŒã·ã§ã³ãžã®ã¢ã¯ã»ã¹ãå¶éããããšãã§ããŸãã
ãããŸã§ãRESTã®åºæ¬ã«ã€ããŠèª¬æããŸããã 次ã«ãèšäºã®æ¬è³ªã«ç®ãåããŸãã 次ã®ç« ã§ã¯ãææžåãæ°ããã¡ãœããã®äœæãAPIã®å€ãããŒãžã§ã³ã®ãµããŒãã®å®äºæã«äœ¿çšããå®çšçãªæ¹æ³ã«ã€ããŠèª¬æããŸã...
APIããã¥ã¡ã³ã
ãµãŒããŒéçºè ãèšè¿°ã§ããææªã®ããã¥ã¡ã³ãã¯ããã©ã¡ãŒã¿ãŒã®èª¬æãšè¿ãããããŒã¿ãå«ãAPIåŒã³åºãã®é·ãå調ãªãªã¹ãã§ãã ãã®ã¢ãããŒãã®äž»ãªåé¡ã¯ããµãŒããŒã®å€æŽãšããããžã§ã¯ãã®éçºäžã«è¿ãããããŒã¿ã®åœ¢åŒã®å€æŽãæªå€¢ã«ãªãããšã§ãã ãã®ç¹ã«é¢ããŠãã¯ã©ã€ã¢ã³ããœãããŠã§ã¢éçºè ãããªããããããç解ã§ããããã«ãããã€ãã®ææ¡ãè¡ããŸãã æéãçµã€ã«ã€ããŠãããã¯ãµãŒããŒãœãããŠã§ã¢éçºè ãšããŠã®éçºã«ã圹ç«ã¡ãŸãã
ããã¥ã¡ã³ã
ã¢ããªã±ãŒã·ã§ã³ãåäœããã¡ã€ã³ã®é«ã¬ãã«ã®ããŒã¿æ§é ïŒã¢ãã«ïŒã«ã€ããŠèããããšããå§ãããŸãã 次ã«ããããã®ã³ã³ããŒãã³ãã«å¯ŸããŠå®è¡ã§ããã¢ã¯ã·ã§ã³ã«ã€ããŠèããŸãã foursquare APIããã¥ã¡ã³ãã¯ãç¬èªã®APIãäœæããåã«åŠã¶è¯ãäŸã§ãã ãããã«ã¯ãå ŽæããŠãŒã¶ãŒãªã©ã®é«ã¬ãã«ã®ãªããžã§ã¯ãã®ã»ããããããŸãã ãŸãããããã®ãªããžã§ã¯ãã«å¯ŸããŠå®è¡ã§ããäžé£ã®ã¢ã¯ã·ã§ã³ããããŸãã 補åã®é«ã¬ãã«ã®ãªããžã§ã¯ããšãããã«å¯Ÿããã¢ã¯ã·ã§ã³ãç¥ã£ãŠããã®ã§ãAPIåŒã³åºãã®æ§é ãäœæããããšã¯ããç°¡åã§ç解ãããããªããŸãã ããšãã°ãæ°ããå Žæãè¿œå ããã«ã¯ã/ venues / addãªã©ã®ã¡ãœãããåŒã³åºãã®ãè«ççã§ã
ãã¹ãŠã®é«ã¬ãã«ãªããžã§ã¯ããææžåããŸãã 次ã«ãåçŽãªããŒã¿åã§ã¯ãªãããããã®é«ã¬ãã«ã®ãªããžã§ã¯ãã䜿çšããŠãèŠæ±ãšå¿çãææžåããŸãã ããã®åŒã³åºãã¯3ã€ã®æååãã£ãŒã«ããè¿ããŸããæåã®åŒã³åºãã«ã¯idã2çªç®ã®ååã3çªç®ã®èª¬æãå«ãŸããŸãããšæžã蟌ã¿ãŸãã
ã¯ãšãªãã©ã¡ãŒã¿ã®ææžå
ãŠãŒã¶ãŒãFacebokããŒã¯ã³ã䜿çšããŠãã°ã€ã³ã§ããAPIããããšããŸãã ãã®ã¡ãœããã/ loginãšããŠåŒã³åºããŸãã
Request /login Headers Authorization: Token XXXXX User-Agent: MyGreatApp/1.0 Accept: application/json Accept-Encoding: compress, gzip Parameters Encoding type â application/x-www-form-urlencoded token â âFacebook Auth Tokenâ (mandatory) profileInfo = âjson string containing public profile information from Facebookâ (optional)
ããã§ãprofileinfoã¯é«ã¬ãã«ã®ãªããžã§ã¯ãã§ãã ãã®ãªããžã§ã¯ãã®å éšæ§é ã¯ãã§ã«ææžåãããŠããããããã®ãããªç°¡åãªèšåã§ååã§ãã ãµãŒããŒãåãAcceptãAccept-Encodingãããã³Encodingã¿ã€ããã©ã¡ãŒã¿ãŒã䜿çšããŠããå Žåããã¹ãŠã®ã»ã¯ã·ã§ã³ã§ç¹°ãè¿ãã®ã§ã¯ãªãããããããã€ã§ãåå¥ã«ææžåã§ããŸãã
å¿çãªãã·ã§ã³ã®ææžå
APIåŒã³åºããžã®å¿çãããªããžã§ã¯ãã®é«ã¬ãã«ã¢ãã«ã«åºã¥ããŠææžåããå¿ èŠããããŸãã åãfoursquareã®äŸãåŒçšããŠã/ venue /ïŒvenueidïŒã¡ãœãããåŒã³åºããšãã€ãã³ãã®å Žæã説æããããŒã¿æ§é ïŒã¢ãã«ïŒãè¿ãããŸãã
ãªããžã§ã¯ãïŒã¢ãã«ïŒã®æ§é ã䜿çšããŠAPIãææžåãããšãã¢ã€ãã¢ã®äº€æãææžåããŸãã¯ãªã¯ãšã¹ããžã®å¿çã§è¿ãããå 容ã«é¢ããä»ã®éçºè ãžã®éç¥ã容æã«ãªããŸãã ãã®ç« ã®æãéèŠãªçµæã¯ããµãŒããŒåŽã®éçºè ããã³ã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ïŒiOS / Android / Windows Phone /äœæïŒã®éçºè ãšããŠç· çµããå¥çŽãšããŠããã¥ã¡ã³ããèªèããå¿ èŠãããããšã§ãã
APIã®æ°ããããŒãžã§ã³ãäœæããå€ãããŒãžã§ã³ã®ãµããŒããåæ¢ããçç±
Web 2.0ã®æ代ã®ã¢ãã€ã«ã¢ããªã±ãŒã·ã§ã³ã®åºçŸä»¥åã¯ãAPIã®ç°ãªãããŒãžã§ã³ãäœæããããšã¯åé¡ã§ã¯ãããŸããã§ããã ã¯ã©ã€ã¢ã³ãïŒJavaScript / AJAXããã³ããšã³ãïŒãšãµãŒããŒã®äž¡æ¹ãåæã«ãããã€ãããŸããã æ¶è²»è ïŒé¡§å®¢ïŒã¯ãåžžã«ææ°ããŒãžã§ã³ã®ã¯ã©ã€ã¢ã³ããœãããŠã§ã¢ã䜿çšããŠã·ã¹ãã ã«ã¢ã¯ã»ã¹ããŠããŸãã ã¯ã©ã€ã¢ã³ããšãµãŒããŒã®äž¡æ¹ã®éšåãéçºããŠããã®ã¯ããªãã ããªã®ã§ãAPIã®äœ¿çšæ¹æ³ãå®å šã«å¶åŸ¡ã§ãããã®å€æŽã¯åžžã«ã¯ã©ã€ã¢ã³ãéšåã«å³åº§ã«é©çšãããŸãã æ®å¿µãªãããããã¯ç°ãªããã©ãããã©ãŒã çšã«äœæãããã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ã§ã¯äžå¯èœã§ãã ãã¹ãŠãããŸããããšä¿¡ããŠAPIããŒãžã§ã³2ããããã€ã§ããŸãããããã¯å€ãããŒãžã§ã³ã䜿çšããiOSã¢ããªã±ãŒã·ã§ã³ã®åäœäžèœã«ã€ãªãããŸãã App Storeã«æŽæ°ãããããŒãžã§ã³ãæçš¿ããã«ããããããããã®ãããªã¢ããªã±ãŒã·ã§ã³ã䜿çšããŠãããŠãŒã¶ãŒããŸã ããå¯èœæ§ãããããã§ãã äžéšã®äŒæ¥ã¯ãããã·ã¥éç¥ã䜿çšããŠæŽæ°ãä¿ãããšã«é Œã£ãŠããŸãã ãããããããå¯äžã®ããšã¯ããã®ãããªã¯ã©ã€ã¢ã³ãã®æ倱ã§ãã 100以äžã®ã¢ããªãã¢ããããŒããåŸ ã£ãŠããå€ãã®iPhoneãèŠãŸããã ããªãã圌ãã®äžäººã«ãªãå¯èœæ§ã¯éåžžã«é«ãã§ãã APIãããŒãžã§ã³ã«åå²ããå¿ èŠã«å¿ããŠãã®äžéšã®ãµããŒããåæ¢ããæºåãåžžã«æŽããŠããå¿ èŠããããŸãã ãã ããAPIã®åããŒãžã§ã³ãå°ãªããšã3ãæéç¶æããŠãã ããã
ããŒãžã§ãã³ã°
ãµãŒããŒã³ãŒããç°ãªããã©ã«ããŒã«ãããã€ããåŒã³åºãã«ç°ãªãURLã䜿çšããŠããAPIãããŒãžã§ã³ã«æ£åžžã«åå²ããããã§ã¯ãããŸããã
ãããã£ãŠã example.com / api / v1ã¯ã¢ããªã±ãŒã·ã§ã³ã®ããŒãžã§ã³1.0ã§äœ¿çšãããæãæ°ãããŠã¯ãŒã«ãªããŒãžã§ã³2.0ã¯example.com/api/v2ã䜿çšããŸã
æŽæ°ãè¡ãå Žåãã»ãšãã©ã®å Žåãå éšããŒã¿æ§é ãšã¢ãã«ã«å€æŽãå ããŸãã ããã«ã¯ãããŒã¿ããŒã¹ã®å€æŽïŒåã®è¿œå ãŸãã¯åé€ïŒãå«ãŸããŸãã ç解ãæ·±ããããã«ããæ°ããFacebookãã«/ feedãšããAPIåŒã³åºããããããFeedããªããžã§ã¯ããè¿ããšããŸãã çŸåšãããŒãžã§ã³1ã§ã¯ãããªãã³ããªããžã§ã¯ãã«ãŠãŒã¶ãŒã¢ãã¿ãŒURLïŒavatarURLïŒããŠãŒã¶ãŒåïŒpersonNameïŒãæçš¿ããã¹ãïŒfeedEntryTextïŒãæçš¿äœææéïŒtimeStampïŒãå«ãŸããŠããŸãã åŸã®ããŒãžã§ã³2ã§ã¯ãåºåäž»ãååã®èª¬æããã£ãŒãã«æçš¿ã§ããããã«ããæ°ããæ©èœã玹ä»ããŸãã ããã§ãããªãã³ããªããžã§ã¯ãã«æ°ãããã£ãŒã«ããsourceNameããå«ãŸããããã«ãªããŸãããããã¯ããªãã³ã衚瀺ããããšãã«ãŠãŒã¶ãŒåãšéè€ããŸãã ãããã£ãŠãã¢ããªã±ãŒã·ã§ã³ã¯ãpersonNameãã§ã¯ãªããsourceNameãã衚瀺ããå¿ èŠããããŸãã ãsourceNameããæå®ãããŠããå Žåãã¢ããªã±ãŒã·ã§ã³ã¯ãpersonNameãã衚瀺ããå¿ èŠããªããªãããããsourceNameããããå Žåã¯ãpersonNameããéä¿¡ããªãããšã«ããŸãã ããã¯ãã¢ããªã±ãŒã·ã§ã³ã®å€ãããŒãžã§ã³ã§ããããŒãžã§ã³1ãæŽæ°ããããµãŒããŒã«ã¢ã¯ã»ã¹ãããŸã§ããã¹ãŠæ£åžžã«èŠããŸãã ãpersonNameããæ¬ èœããŠãããããã¿ã€ãã«ãªãã§ãã£ãŒãããåºåãšã³ããªã衚瀺ãããŸãã ãã®ãããªåé¡ã解決ãããæèœãªãæ¹æ³ã¯ããpersonNameããšãsourceNameãã®äž¡æ¹ãéä¿¡ããããšã§ãã ããããå人ãã¡ã人çã¯å¿ ãããããã»ã©åçŽã§ã¯ãããŸããã éçºè ã¯ããªããžã§ã¯ãã®åããŒã¿ã¢ãã«ã§ãããŸã§ã«è¡ããããã¹ãŠã®åäžã®å€æŽã远跡ããããšã¯ã§ããŸããã å幎åŸã«ã¯ã³ãŒãã«äœããè¿œå ãããçç±ãšæ¹æ³ãã»ãšãã©å¿ããŠããŸããããããã¯å€æŽãè¡ãã®ã«ããŸãå¹æçãªæ¹æ³ã§ã¯ãããŸããã
Web 2.0ã«æ»ããšãããã¯ãŸã£ããåé¡ã§ã¯ãããŸããã§ããã JavaScriptã¯ã©ã€ã¢ã³ãã¯ãAPIãžã®å€æŽããµããŒãããããã«ããã«ä¿®æ£ãããŸããã ãã ããã€ã³ã¹ããŒã«ãããiOSã¢ããªã±ãŒã·ã§ã³ã¯ãã¯ãããªãã«äŸåããŠããŸããã çŸåšããããã®æŽæ°ã¯ãŠãŒã¶ãŒã®ç¹æš©ã§ãã
ãã®çš®ã®ããªãããŒãªç¶æ³ã«ã¯ãšã¬ã¬ã³ããªãœãªã¥ãŒã·ã§ã³ããããŸãã
URLããŒãžã§ã³ç®¡çãã©ãã€ã
æåã®è§£æ±ºçã¯URLåé¢ã§ãã
api.example.com/v1/feedsã¯iOSã¢ããªã±ãŒã·ã§ã³ã®ããŒãžã§ã³1ã§äœ¿çšãããŸããã
api.example.com/v2/feedsã¯ããŒãžã§ã³2ã§äœ¿çšãããŸãã
ããã¯è¯ãããã«èãããŸãããè¿ãããããŒã¿ã®åœ¢åŒãå€æŽããããã³ã«ãµãŒããŒã³ãŒãã®ã³ããŒãäœæãç¶ããããšã¯ã§ããŸããã ãã®ã¢ãããŒãã¯ãAPIãã°ããŒãã«ã«å€æŽãããå Žåã«ã®ã¿äœ¿çšããããšããå§ãããŸãã
ã¢ãã«ã«ããããŒãžã§ã³ç®¡çãã©ãã€ã
äžèšã§ã¯ãããŒã¿æ§é ïŒã¢ãã«ïŒãææžåããæ¹æ³ã瀺ããŸããã ãã®ããã¥ã¡ã³ãã¯ããµãŒããŒããŒããšã¯ã©ã€ã¢ã³ãããŒãã®éçºè éã®å¥çŽãšããŠæ€èšããŠãã ããã ããŒãžã§ã³ãå€æŽããã«ã¢ãã«ãå€æŽããªãã§ãã ããã ã€ãŸããåã®ã±ãŒã¹ã§ã¯ãFeed1ãšFeed2ã®2ã€ã®ã¢ãã«ãããã¯ãã§ãã
Feed2ã«ã¯sourceNameãã£ãŒã«ãããããsourceNameãèšå®ãããŠããå ŽåãpersonNameã§ã¯ãªãsourceNameãè¿ããŸãã Feed1ã®åäœã¯ãããã¥ã¡ã³ãã§æå®ãããŠãããã®ãšåãã§ãã ã³ã³ãããŒã©ãŒã®ã¢ã«ãŽãªãºã ã¯æ¬¡ã®ããã«ãªããŸãã
Factoryã¡ãœãããã¿ãŒã³ã«åŸã£ãŠãã¯ã©ã¹ãã€ã³ã¹ã¿ã³ã¹åããããžãã¯ãå¥ã®ã¯ã©ã¹ã«ç§»åããå¿ èŠããããŸãã 察å¿ããã³ã³ãããŒã©ãŒã³ãŒãã¯æ¬¡ã®ããã«ãªããŸãã
Feed myFeedObject = Feed.createFeedObject("1.0"); myFeedObject.populateWithDBObject(FeedDao* feedDaoObject);
䜿çšãããAPIã®ããŒãžã§ã³ã®æ±ºå®ã¯ããªã¯ãšã¹ãããã¹ãã®UserAgentãã£ãŒã«ãã«åŸã£ãŠã³ã³ãããŒã©ãŒã«ãã£ãŠè¡ãããŸãã
è¿œå ïŒ
UserAgentæååã®ããŒãžã§ã³çªå·ã䜿çšãã代ããã«ãAcceptããããŒã®ããŒãžã§ã³çªå·ã䜿çšããããšããå§ãããŸãã éä¿¡ãã代ããã«
Accept: application/json
éä¿¡ããå¿ èŠããããŸã
Accept: application/myservice.1.0+json
ãããã£ãŠãRESTãµãŒããŒãžã®ãªã¯ãšã¹ãããšã«APIã®ããŒãžã§ã³ãæå®ã§ããŸãã ãã®ãã³ããæäŸããŠãããããã«ãŒãã¥ãŒã¹ãªãŒããŒã«æè¬ããŸãã
ã³ã³ãããŒã©ãŒã¯ããã£ãŒããã¡ã¯ããªã¡ãœããã䜿çšããŠãããŒãžã§ã³ã«é¢ããã¯ã©ã€ã¢ã³ãã®èŠæ±ïŒãã¹ãŠã®èŠæ±ã«ã¯AppName / 1.0ã®ãããªUserAgentãã£ãŒã«ããå«ãŸããŸãïŒããã®æ å ±ã«åºã¥ããŠãæ£ãããã£ãŒããªããžã§ã¯ãïŒãã£ãŒãïŒãäœæããŸãã ãã®æ¹æ³ã§ãµãŒããŒãèšèšãããšãå€æŽã¯ç°¡åã«ãªããŸãã å€æŽãæ¢åã®å¥çŽã«éåããããšã¯ãããŸããã æ°ããããŒã¿æ§é ïŒã¢ãã«ïŒãäœæãããã¡ã¯ããªã¡ãœãããå€æŽããŠãæ°ããããŒãžã§ã³ã®æ°ããã¢ãã«ã®ã€ã³ã¹ã¿ã³ã¹ãäœæããã ãã§ãã
ãã®ã¢ãããŒãã䜿çšãããšãããŒãžã§ã³1ããã³2ã®ã¢ããªã±ãŒã·ã§ã³ã¯åããµãŒããŒã§åŒãç¶ãåäœã§ããŸãã ã³ã³ãããŒã©ã¯ãå€ãã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³çšã«ããŒãžã§ã³1ãªããžã§ã¯ããäœæããæ°ããã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³çšã«ããŒãžã§ã³2ãªããžã§ã¯ããäœæã§ããŸãã
ãµããŒãçµäº
ã¢ãã«ãä»ããŠAPIãããŒãžã§ã³ã«åå²ããããã®äžèšã®ãã©ãã€ã ã«ãããAPIã®ãµããŒããçµäºããã®ãã¯ããã«ç°¡åã«ãªããŸãã ããã¯ãAPIãå ¬éããæåŸã®æ®µéã§éåžžã«éèŠã§ãã APIã®ã°ããŒãã«æŽæ°ãè¡ãå Žåãããžãã¹ããžãã¯ã®å€æŽã«å¿ããŠã¢ãã«å ã®ãã¹ãŠã®ãã¡ã¯ããªã¡ãœãããç£æ»ããŸãã
APIã®ããŒãžã§ã³3ã®ãªãªãŒã¹äžã«ããŒãžã§ã³1ã®ãµããŒããåæ¢ããããšã«ããå Žåã察å¿ããã¢ãã«ãåé€ãããã¡ã¯ããªã¡ãœããã§ã€ã³ã¹ã¿ã³ã¹ãäœæããè¡ãåé€ããã ãã§ãã æ°ããããŒãžã§ã³ãäœæããå€ãããŒãžã§ã³ã®ãµããŒããåæ¢ãããšãå¿ ããããžã§ã¯ãã«ä»éããããžãã¹ã決å®ããéèŠãªæ±ºå®ããµããŒãããããšãã©ãã»ã©æè»ãã瀺ããŸãã æ¥æ¿ãªå€åãšã¿ãŒã³ã®ã§ããªãããžãã¹ã¯éåœã¥ããããŠããŸãã éåžžãéèŠãªå€æŽãå ããããšãã§ããªãã®ã¯ããããžã§ã¯ãã®æè¡çãªæ¬ é¥ã«ãããã®ã§ãã æå®ãããææ³ã¯ããã®ãããªåé¡ã解決ã§ããŸãã
ãã£ãã·ã³ã°
ããã©ãŒãã³ã¹ã«é¢ããŠæ³šæãã¹ããã1ã€ã®éèŠãªç¹ã¯ããã£ãã·ã¥ã§ãã ãããã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ã®ã¿ã¹ã¯ã ãšæãããå Žåã¯ãæ éã«èããŠãã ããã ãã®èšäºã®ããŒã2ã§ã¯ãhttp 1.1ã䜿çšããŠãã£ãã·ã¥ãç·šæããæ¹æ³ã説æããŸãã
APIã®ãšã©ãŒåŠçãšåœéå
ãšã©ãŒãçºçããå Žåã«ãšã©ãŒã®åå ãã¯ã©ã€ã¢ã³ãã«äŒããããšã¯ãæ£ããããŒã¿ãéä¿¡ããããšãšåããããéèŠã§ãã ãã®èšäºã®ããŒã3ã§ã¯ããšã©ãŒåŠçãšåœéåã«ã€ããŠèª¬æããŸãã ç§ã¯äœãçŽæããŸãããã©ããªå Žåã§ããæžãã®ã«æéãããããŸãã
翻蚳è ããïŒ
ç§èªèº«ã¯iOSã®éçºè ã§ã¯ãªããWebãµãŒãã¹ãéçºããŠããŸããããã®åéã§ã®ç§ã®ã¬ãã«ã¯ããåå¿è ã«ãªããŸãããšèšããŸãã ãããããã®ãããã¯ã¯ç§ã«ãšã£ãŠèå³æ·±ããã®ã§ãããç§ã¯ãã®èšäºã奜ãã ã£ãã®ã§ã翻蚳ããããšã«ããŸããã
第äºéš