NuGet API v3 パッケージ情報の読み方
昨日に引き続き NuGet API v3 を触ろうの会。テスト勉強しろや。
今回はパッケージの ID とバージョンがわかっているときのパッケージ情報の取得方法についてです。
バックナンバー
とりあえず取得してみる
https://api.nuget.org/v3/index.json によると
- https://api.nuget.org/v3/registration0/{id-lower}/index.json
- https://api.nuget.org/v3/registration0/{id-lower}/{version-lower}.json
で取得できるようです。「-lower」と書いてあるように、必ず小文字にしてください。 Azure Blob Storage 上でホストされているようで大文字小文字を区別します。
index.json
例として CoreTweet パッケージの情報を取得してみましょう。
https://api.nuget.org/v3/registration0/coretweet/index.json
{ "@id": "https://api.nuget.org/v3/registration0/coretweet/index.json", "@type": [ "catalog:CatalogRoot", "PackageRegistration", "catalog:Permalink" ], "commitId": "430b3a0a-bc8a-40a3-9dce-24002ce549e2", "commitTimeStamp": "2015-02-20T00:25:18.9941827Z", "count": 1, "items": [ { "@id": "https://api.nuget.org/v3/registration0/coretweet/index.json#page/0.1.5-alpha/0.4.2.84", "@type": "catalog:CatalogPage", "commitId": "430b3a0a-bc8a-40a3-9dce-24002ce549e2", "commitTimeStamp": "2015-02-20T00:25:18.9941827Z", "count": 20, "items": [ { "@id": "https://api.nuget.org/v3/registration0/coretweet/0.1.5-alpha.json", "@type": "Package", "commitId": "33ef8802-8735-4069-b5f1-cabb95b27d45", "commitTimeStamp": "2015-02-11T01:11:46.1320698Z", "catalogEntry": { "@id": "https://api.nuget.org/v3/catalog0/data/2015.02.07.22.52.30/coretweet.0.1.5-alpha.json", "@type": "PackageDetails", "authors": "CoreTweet Development Team", "dependencyGroups": [ { "@id": "https://api.nuget.org/v3/catalog0/data/2015.02.07.22.52.30/coretweet.0.1.5-alpha.json#dependencygroup", "@type": "PackageDependencyGroup", "dependencies": [ { "@id": "https://api.nuget.org/v3/catalog0/data/2015.02.07.22.52.30/coretweet.0.1.5-alpha.json#dependencygroup/newtonsoft.json", "@type": "PackageDependency", "id": "Newtonsoft.Json", "range": "[4.5.11, )", "registration": "https://api.nuget.org/v3/registration0/newtonsoft.json/index.json" } ] } ], "description": "A .NET Twitter Library supporting API 1.1\nSimple, Powerful, and Well-designed for LINQ\nThis is a pre-alpha release.\nPlease report bugs to https://github.com/lambdalice/CoreTweet/issues", "iconUrl": "https://raw.github.com/lambdalice/CoreTweet/master/icon/core-48x48.png", "id": "CoreTweet", "language": "", "licenseUrl": "http://opensource.org/licenses/mit-license.php", "minClientVersion": "", "projectUrl": "https://github.com/lambdalice/CoreTweet", "published": "1900-01-01T00:00:00Z", "requireLicenseAcceptance": false, "summary": "A .NET Twitter Library supporting API 1.1", "tags": [ "Twitter" ], "title": "CoreTweet", "version": "0.1.5-alpha" }, "packageContent": "https://api.nuget.org/packages/coretweet.0.1.5-alpha.nupkg", "registration": "https://api.nuget.org/v3/registration0/coretweet/index.json" }, /* 残り 19 件は省略 */ ], "parent": "https://api.nuget.org/v3/registration0/coretweet/index.json", "lower": "0.1.5-alpha", "upper": "0.4.2.84" } ], "@context": { "@vocab": "http://schema.nuget.org/schema#", "catalog": "http://schema.nuget.org/catalog#", "xsd": "http://www.w3.org/2001/XMLSchema#", "items": { "@id": "catalog:item", "@container": "@set" }, "commitTimeStamp": { "@id": "catalog:commitTimeStamp", "@type": "xsd:dateTime" }, "commitId": { "@id": "catalog:commitId" }, "count": { "@id": "catalog:count" }, "parent": { "@id": "catalog:parent", "@type": "@id" }, "tags": { "@container": "@set", "@id": "tag" }, "packageTargetFrameworks": { "@container": "@set", "@id": "packageTargetFramework" }, "dependencyGroups": { "@container": "@set", "@id": "dependencyGroup" }, "dependencies": { "@container": "@set", "@id": "dependency" }, "packageContent": { "@type": "@id" }, "published": { "@type": "xsd:dateTime" }, "registration": { "@type": "@id" } } }
最初に items でラップしているのが謎ですが、結構素直な JSON です。フィールドの値を取得するときは本当は JSON-LD の検証を行うべきなのでしょうけど、静的ファイルなので仕様変更することは無いと思います。また @id フィールドの URI はアクセスするとその中身を取得することができます。 catalogEntry のいくつかのフィールドはこの index.json には入ってきていないので、必要な場合はそちらも取得する必要があります。
バージョンを指定して取得
https://api.nuget.org/v3/registration0/{id-lower}/{version-lower}.json の形ですが、さっきの index.json の items/items の要素の @id にちゃんと書き込まれています。
https://api.nuget.org/v3/registration0/coretweet/0.4.2.84.json
{ "@id": "https://api.nuget.org/v3/registration0/coretweet/0.4.2.84.json", "@type": [ "Package", "http://schema.nuget.org/catalog#Permalink" ], "catalogEntry": "https://api.nuget.org/v3/catalog0/data/2015.02.16.09.30.30/coretweet.0.4.2.84.json", "packageContent": "https://api.nuget.org/packages/coretweet.0.4.2.84.nupkg", "published": "2015-02-16T09:29:47.32+00:00", "registration": "https://api.nuget.org/v3/registration0/coretweet/index.json", "@context": { "@vocab": "http://schema.nuget.org/schema#", "xsd": "http://www.w3.org/2001/XMLSchema#", "catalogEntry": { "@type": "@id" }, "registration": { "@type": "@id" }, "packageContent": { "@type": "@id" }, "published": { "@type": "xsd:dateTime" } } }
index.json に対して欠けているフィールドはないし、むしろ catalogEntry がたらい回しされるという素敵仕様になっています。これ意味あるの…。
catalog
では今のたらい回しを取得してみましょう
https://api.nuget.org/v3/catalog0/data/2015.02.16.09.30.30/coretweet.0.4.2.84.json
{ "@id": "https://api.nuget.org/v3/catalog0/data/2015.02.16.09.30.30/coretweet.0.4.2.84.json", "@type": [ "PackageDetails", "catalog:Permalink" ], "authors": "CoreTweet Development Team", "catalog:commitId": "23d74de4-9909-46af-a0b6-6e7e9f39245d", "catalog:commitTimeStamp": "2015-02-16T09:30:30.0782059Z", "copyright": "(c) 2014 lambdalice, karno", "created": "2015-02-16T09:29:47.303Z", "description": "A .NET Twitter Library supporting API 1.1\nSimple, Powerful, and Well-designed for LINQ\nThis is an unstable release.\nPlease report bugs to https://github.com/CoreTweet/CoreTweet/issues", "iconUrl": "https://raw.github.com/CoreTweet/CoreTweet/master/icon/core-48x48.png", "id": "CoreTweet", "isPrerelease": false, "lastEdited": "0001-01-01T00:00:00Z", "licenseUrl": "http://opensource.org/licenses/mit-license.php", "packageHash": "YqdcF/nVZvE3YY8HKnRneq8cHcQa7EHr6T0I8Xo0pONlZmkh2ge1R9YbueZMOg4x33fdDXPQmYu6kC0CgdJ/NA==", "packageHashAlgorithm": "SHA512", "packageSize": 687621, "projectUrl": "https://github.com/CoreTweet/CoreTweet", "published": "2015-02-16T09:29:47.32Z", "requireLicenseAcceptance": false, "summary": "Yet Another .NET Twitter Library", "title": "CoreTweet", "version": "0.4.2.84", "dependencyGroups": [ { "@id": "https://api.nuget.org/v3/catalog0/data/2015.02.16.09.30.30/coretweet.0.4.2.84.json#dependencygroup", "@type": "PackageDependencyGroup", "dependencies": [ { "@id": "https://api.nuget.org/v3/catalog0/data/2015.02.16.09.30.30/coretweet.0.4.2.84.json#dependencygroup/newtonsoft.json", "@type": "PackageDependency", "id": "Newtonsoft.Json", "range": "[4.5.11, )" } ] } ], "tags": [ "Twitter" ], "@context": { "@vocab": "http://schema.nuget.org/schema#", "catalog": "http://schema.nuget.org/catalog#", "xsd": "http://www.w3.org/2001/XMLSchema#", "dependencies": { "@id": "dependency", "@container": "@set" }, "dependencyGroups": { "@id": "dependencyGroup", "@container": "@set" }, "packageEntries": { "@id": "packageEntry", "@container": "@set" }, "supportedFrameworks": { "@id": "supportedFramework", "@container": "@set" }, "tags": { "@id": "tag", "@container": "@set" }, "published": { "@type": "xsd:dateTime" }, "created": { "@type": "xsd:dateTime" }, "lastEdited": { "@type": "xsd:dateTime" }, "catalog:commitTimeStamp": { "@type": "xsd:dateTime" } } }
index.json にはなかった created(published とどう違うんだ…), isPrerelease, packageHash, packageSize というフィールドが追加されています。ふむ。
まとめ
バージョンはともかく https://api.nuget.org/v3/registration0/{id-lower}/index.json を叩けばパッケージ情報は大体取得できます。 JSON-LD とかは気にしなくても普通に解析できるので難しいことはないはずです。ああ、そんなに中身の無い記事になってしまった。