14. public Tuple<string, List<Tuple<string, string>>> GetType(string objectName)
{
Tuple<string, List<Tuple<string, string>>> result = null;
var client = new WebClient();
var yamlAddress = new Configuration().Get("SpotifyYaml");
string yamlText = client.DownloadString("https://" + yamlAddress);
var input = new StringReader(yamlText);
var yaml = new YamlStream();
yaml.Load(input);
var mapping =(YamlMappingNode)yaml.Documents[0].RootNode;
var spotifyObjects = new List<Tuple<string, List<Tuple<string,string>>>>();
foreach (var entry in mapping.Children)
{
if (entry.Key.ToString()== "definitions")
{
var classes = (entry.Value as YamlMappingNode).Children.Keys;
foreach (var n in (entry.Value as YamlMappingNode).Children)
{
List <Tuple<string, string>> propertiesTemp = new List<Tuple<string, string>>();
foreach (var cn in (n.Value as YamlMappingNode).Children)
{
if (cn.Key.ToString() == "properties")
{
var properties = (cn.Value as YamlMappingNode).Children.Keys;
Tuple<string, string> temp = null;
foreach (var gcn in (cn.Value as YamlMappingNode).Children)
{
var tempVal = string.Empty;
if ((gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "type").Count() > 0)
{
tempVal = (gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "type").FirstOrDefault().Value.ToString();
temp = new Tuple<string, string>(gcn.Key.ToString(), tempVal);
propertiesTemp.Add(temp);
}
else if ((gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "$ref").Count() > 0)
{
tempVal = (gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "$ref").FirstOrDefault().Value.ToString();
tempVal = tempVal.Replace("#/definitions/", "");
tempVal = tempVal.Replace("'", "");
temp = new Tuple<string, string>(gcn.Key.ToString(), tempVal);
propertiesTemp.Add(temp);
}
}
break;
}
}
spotifyObjects.Add(new Tuple<string, List<Tuple<string, string>>>(n.Key.ToString(), propertiesTemp));
}
}
}
result = spotifyObjects.Where(r => r.Item1 == objectName).FirstOrDefault();
return result;
}
public Tuple<string, List<Tuple<string, string>>> GetType(string objectName)
var yamlAddress = new Configuration().Get("SpotifyYaml");
string yamlText = client.DownloadString("https://" + yamlAddress);
var yaml = new YamlStream();
yaml.Load(input);
var mapping =(YamlMappingNode)yaml.Documents[0].RootNode;
foreach (var entry in mapping.Children)
{
if (entry.Key.ToString()== "definitions")
{
var classes = (entry.Value as YamlMappingNode).Children.Keys;
foreach (var n in (entry.Value as YamlMappingNode).Children)
{
var propertiesTemp = new List<Tuple<string, string>>();
foreach (var cn in (n.Value as YamlMappingNode).Children)
{
if (cn.Key.ToString() == "properties")
15. public Tuple<string, List<Tuple<string, string>>> GetType(string objectName)
{
Tuple<string, List<Tuple<string, string>>> result = null;
var client = new WebClient();
var yamlAddress = new Configuration().Get("SpotifyYaml");
string yamlText = client.DownloadString("https://" + yamlAddress);
var input = new StringReader(yamlText);
var yaml = new YamlStream();
yaml.Load(input);
var mapping =(YamlMappingNode)yaml.Documents[0].RootNode;
var spotifyObjects = new List<Tuple<string, List<Tuple<string,string>>>>();
foreach (var entry in mapping.Children)
{
if (entry.Key.ToString()== "definitions")
{
var classes = (entry.Value as YamlMappingNode).Children.Keys;
foreach (var n in (entry.Value as YamlMappingNode).Children)
{
List <Tuple<string, string>> propertiesTemp = new List<Tuple<string, string>>();
foreach (var cn in (n.Value as YamlMappingNode).Children)
{
if (cn.Key.ToString() == "properties")
{
var properties = (cn.Value as YamlMappingNode).Children.Keys;
Tuple<string, string> temp = null;
foreach (var gcn in (cn.Value as YamlMappingNode).Children)
{
var tempVal = string.Empty;
if ((gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "type").Count() > 0)
{
tempVal = (gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "type").FirstOrDefault().Value.ToString();
temp = new Tuple<string, string>(gcn.Key.ToString(), tempVal);
propertiesTemp.Add(temp);
}
else if ((gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "$ref").Count() > 0)
{
tempVal = (gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "$ref").FirstOrDefault().Value.ToString();
tempVal = tempVal.Replace("#/definitions/", "");
tempVal = tempVal.Replace("'", "");
temp = new Tuple<string, string>(gcn.Key.ToString(), tempVal);
propertiesTemp.Add(temp);
}
}
break;
}
}
spotifyObjects.Add(new Tuple<string, List<Tuple<string, string>>>(n.Key.ToString(), propertiesTemp));
}
}
}
result = spotifyObjects.Where(r => r.Item1 == objectName).FirstOrDefault();
return result;
}
tempVal = (gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "type").FirstOrDefault().Value.ToString();
temp = new Tuple<string, string>(gcn.Key.ToString(), tempVal);
propertiesTemp.Add(temp);
tempVal = (gcn.Value as YamlMappingNode).Children.Where(c => c.Key.ToString() == "$ref").FirstOrDefault().Value.ToString();
tempVal = tempVal.Replace("#/definitions/", "");
tempVal = tempVal.Replace("'", "");
temp = new Tuple<string, string>(gcn.Key.ToString(), tempVal);
propertiesTemp.Add(temp);
foreach (var gcn in (cn.Value as YamlMappingNode).Children)
16. How to dynamically create a class?
https://stackoverflow.com/questions/3862226/how-to-dynamically-create-a-class
17. Configuration:コンストラクタ
public class Configuration
{
IConfigurationRoot config { get; set; }
public string Get(string key,string ver="Current")
public bool Set(string key,string val, string ver =
"Current")
public Configuration()
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("settings.json");
config = builder.Build();
}
}
18. Configuration:Get
public string Get(string key,string ver="Current")
{
string result = string.Empty;
if (ver == "Current")
{
result = config.GetSection(key).Value;
}
else
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("settings" + ver + ".json");
config = builder.Build();
result = config.GetSection(key).Value;
}
return result;
}
19. Configuration:Set
try
{
if (ver == "Current")
{
config.GetSection(key).Value = val;
}
else
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("settings" + ver + ".json");
config = builder.Build();
config.GetSection(key).Value = val;
}
result = true;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
28. Source
Spotify for developers
https://developer.spotify.com/
Postman | API Development Environment
https://www.getpostman.com/
Open API Directory
https://github.com/APIs-guru/openapi-directory
Swagger
https://swagger.io/
Swagger/OpenAPI を使用する ASP.NET Core Web
API のヘルプ ページ
https://docs.microsoft.com/ja-jp/aspnet/core/tutorials/web-api-help-pages-using-
swagger?view=aspnetcore-3.0
29. Source
YamlDotNet
https://github.com/aaubry/YamlDotNet
How to dynamically create a class?
https://stackoverflow.com/questions/3862226/how-to-dynamically-create-a-class
The Microsoft Graph Toolkit
https://github.com/microsoftgraph/microsoft-graph-toolkit
Getting Started with the Microsoft Graph Toolkit
https://www.youtube.com/watch?v=oZCGb2MMxa0
Sampleソリューション・ソースコード
https://github.com/TetsuroTakao/SpotifyWebAPISample
Web APIはjsonで返ってきますので、受け取ったデータはモデルにマップして操作します。
Web APIはOAuthで認証して使いますので、最初にユーザー情報を取得することになります。
そのため、コードはこんな感じになります。最初に必要なことは、この「SpotifyUser」というクラスを作ることです。
【クリック】
「SpotifyUser」クラスは、先ほどのSpotify Web API Object Modelのページを参照して、このように作ってもいいのですが、タイプリゾルバを作るとコーディングが楽になります。
【クリック】
タイプリゾルバは、Web API側がどのようなインターフェイスを用意しているかを公開する構成にすることで、APIのインターフェイスの更新時でも継続的インテグレーションを実現します。
Spotify Web API Object Modelは、Open APIに準拠しており、Swaggerのyamlを公開しています。
【クリック】
このファイルを使用することでタイプリゾルバを簡単に作成することができます。【クリック】
Web APIのインターフェイスの記述をするためのOpen APIの標準フォーマットがSwaggerで、Swagger UIはインターフェイスを定義したyamlからHTMLを自動生成してくれるのでSwagger Editorでyamlを生成、編集したりSwagger Codegenでコードを自動生成したりすることでWeb APIの提供元は保守、運用作業が楽になります。
これらのツールはjavaを使ってインプリしたりするので、Spotity Web APIの利用者として、Object Modelのタイプリゾルバを作るだけならyamlを読み込む機能を実装するだけで十分です。
また、Swaggerを装備したWeb APIを.Netで構築することもできます。
Web APIを提供する場合は実装することをお勧めします。
Spotify Web APIのObject Modelのタイプリゾルバを作る方法として、yamlを読み込む機能を実装します。