public void GenerateSourceCode(String folderPath) { var d = new Dictionary<TwitterApiEndpointInfo, SourceCode>(); var documentUrls = new List<String>(); var cl = new HttpClient(); var html = cl.GetBodyText("https://dev.twitter.com/rest/public"); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); var nodes = doc.DocumentNode.SelectNodes("//a[starts-with(@href,'/rest/reference/get/') or starts-with(@href,'/rest/reference/post/')]"); var apiInfoList = new List<TwitterApiEndpointInfo>(); foreach (var a in nodes) { var documentUrl = WebUtility.UrlDecode(a.Attributes["href"].Value); //This endpoint has been DEPRECATED.So we skip this endpoint if (documentUrl == "/rest/reference/post/statuses/update_with_media") { continue; } //documentUrl = "/rest/reference/get/lists/members";//Use when debug... if (documentUrls.Contains(documentUrl) == true) { continue; } documentUrls.Add(documentUrl); var apiInfo = CreateApiInfo(documentUrl); //statuses/mentions_timeline //statuses/update_with_media //application/rate_limit_status //user/profile_banner //friendships/no_retweets/ids //geo/reverse_geocode var sc = GenerateApiEntityClasses("https://dev.twitter.com" + apiInfo.DocumentUrl, apiInfo); d.Add(apiInfo, sc); apiInfoList.AddIfNotExist(apiInfo); Console.WriteLine(apiInfo.DocumentUrl); } this.EnsureDirectory(folderPath); var sc1 = GenerateApiEndpointClasses(apiInfoList); this.CreateSourceCodeFile(Path.Combine(folderPath, "ApiEndpoints.Api.cs"), sc1); foreach (var apiInfo in d.Keys) { var sc = d[apiInfo]; var fileName = String.Format("{0}.{1}.cs", apiInfo.Name1, apiInfo.Name2); this.EnsureDirectory(Path.Combine(folderPath, apiInfo.Name1)); var filePath = Path.Combine(folderPath, apiInfo.Name1, fileName); this.CreateSourceCodeFile(filePath, sc); Console.WriteLine(fileName); } Console.WriteLine("Completed...Please press enter key"); Console.ReadLine(); }
/// <summary> /// /// </summary> /// <param name="authorizeInfo"></param> /// <param name="oauthVerifier"></param> /// <returns></returns> public AccessTokenInfo GetAccessToken(AuthorizeInfo authorizeInfo, String oauthVerifier) { var ai = authorizeInfo; var cm = new GetRequestTokenCommand(this.ConsumerKey, this.ConsumerSecret , ai.RequestToken, ai.RequestTokenSecret, HttpMethodName.Post); var si = OAuthClient.GenerateSignature(new Uri(this.AccessTokenUrl), cm, OAuthSignatureTypes.HMACSHA1); HttpRequestCommand command = new HttpRequestCommand(String.Format("{0}?{1}&oauth_verifier={2}&oauth_signature={3}" , this.AccessTokenUrl, si.NormalizedRequestParameters, oauthVerifier, si.Signature), HttpMethodName.Post); HttpClient cl = new HttpClient(); String s = cl.GetBodyText(command); return(AccessTokenInfo.Create(s, "oauth_token", "oauth_token_secret")); }
/// <summary> /// /// </summary> /// <param name="requestToken"></param> /// <param name="requestTokenSecret"></param> /// <returns></returns> public AccessTokenInfo GetAccessToken(String requestToken, String requestTokenSecret) { String url = this.AccessTokenUrl; GetRequestTokenCommand cm = new GetRequestTokenCommand(this.ConsumerKey, this.ConsumerSecret , requestToken, requestTokenSecret, HttpMethodName.Post); SignatureInfo si = OAuthClient.GenerateSignature(new Uri(url), cm, OAuthSignatureTypes.HMACSHA1); HttpRequestCommand command = new HttpRequestCommand(String.Format("{0}?{1}&oauth_signature={2}", url, si.NormalizedRequestParameters, si.Signature), HttpMethodName.Post); HttpClient cl = new HttpClient(); String result = cl.GetBodyText(command); return(AccessTokenInfo.Create(result, "oauth_token", "oauth_token_secret")); }
/// <summary> /// /// </summary> /// <returns></returns> public AuthorizeInfo GetAuthorizeInfo() { String url = this.RequestTokenUrl; var cm = new GetRequestTokenCommand(this.ConsumerKey, this.ConsumerSecret, "", "", HttpMethodName.Post); SignatureInfo si = OAuthClient.GenerateSignature(new Uri(url), cm, OAuthSignatureTypes.HMACSHA1); HttpClient cl = new HttpClient(); HttpRequestCommand command = new HttpRequestCommand(String.Format("{0}?{1}&oauth_signature={2}", url, si.NormalizedRequestParameters, si.Signature), HttpMethodName.Post); String result = cl.GetBodyText(command); //正規表現でoauth_token,oauth_token_secret取得 AuthorizeInfo ai = new AuthorizeInfo(); ai.AuthorizeUrl = String.Format("{0}?{1}", this.AuthorizeUrl, result); ai.RequestToken = this.GetMatchValue(RegexList.OAuthToken, result); ai.RequestTokenSecret = this.GetMatchValue(RegexList.OAuthTokenSecret, result); return(ai); }
public void GenerateSourceCode(String folderPath) { var d = new Dictionary<VimeoApiEndpointInfo, SourceCode>(); var documentUrls = new List<String>(); var cl = new HttpClient(); var html = cl.GetBodyText("https://developer.vimeo.com/api/endpoints/"); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); var playgroundUrls = new List<string>(); var nodes = doc.DocumentNode.SelectNodes("//div[@class='list js-endpoint_list']//div[@class='item js-endpoint_name txt_md']//a"); foreach (var node in nodes) { var href = "https://developer.vimeo.com" + node.Attributes["href"].Value; Console.WriteLine(href); playgroundUrls.AddRange(this.GetPlaygroundUrls(href)); } playgroundUrls = playgroundUrls.Distinct().ToList(); playgroundUrls.Add("https://developer.vimeo.com/api/playground/users/"); playgroundUrls.Add("https://developer.vimeo.com/api/playground/users/{user_id}"); playgroundUrls.Add("https://developer.vimeo.com/api/playground/channels/"); playgroundUrls.Add("https://developer.vimeo.com/api/playground/channels/{channel_id}"); playgroundUrls.Add("https://developer.vimeo.com/api/playground/channels/{channel_id}/videos"); playgroundUrls.Add("https://developer.vimeo.com/api/playground/users/{user_id}/videos"); playgroundUrls.Add("https://developer.vimeo.com/api/playground/videos/"); playgroundUrls.Add("https://developer.vimeo.com/api/playground/videos/{video_id}"); var apiInfoList = new List<VimeoApiEndpointInfo>(); foreach (var url in playgroundUrls) { foreach (var apiInfo in CreateApiInfoList(url)) { if (apiInfo.HttpMethodName != "Get") { continue; } if (apiInfoList.Exists(el => el.Name1 == apiInfo.Name1 && el.Name2 == apiInfo.Name2) == true) { continue; } var sc = GenerateApiEntityClasses(apiInfo.ApiPlaygroundUrl, apiInfo); d.Add(apiInfo, sc); apiInfoList.AddIfNotExist(apiInfo); Console.WriteLine(String.Format("{0}: {1}.{2}" , apiInfo.ApiPath, apiInfo.Name1, apiInfo.Name2)); } } this.EnsureDirectory(folderPath); foreach (var apiInfo in d.Keys) { var sc = d[apiInfo]; var fileName = String.Format("{0}.{1}.cs", apiInfo.Name1, apiInfo.Name2); this.EnsureDirectory(Path.Combine(folderPath, apiInfo.Name1)); var filePath = Path.Combine(folderPath, apiInfo.Name1, fileName); this.CreateSourceCodeFile(filePath, sc); Console.WriteLine(fileName); } var sc1 = GenerateApiEndpointClasses(apiInfoList); this.CreateSourceCodeFile(Path.Combine(folderPath, "ApiEndpoints.Api.cs"), sc1); Console.WriteLine("Completed...Please press enter key"); Console.ReadLine(); }
private List<String> GetPlaygroundUrls(String url) { var l = new List<String>(); var cl = new HttpClient(); var html = cl.GetBodyText(url); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); var nodes = doc.DocumentNode.SelectNodes("//a[starts-with(@href, '/api/playground')]"); foreach (var node in nodes) { if (node.Attributes["href"].Value == "/api/playground/") { continue; } l.Add("https://developer.vimeo.com" + node.Attributes["href"].Value); } return l; }
private SourceCode GenerateApiEntityClasses(String url, VimeoApiEndpointInfo apiInfo) { var cl = new HttpClient(); var html = cl.GetBodyText(url); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); SourceCode sc = new SourceCode(); sc.UsingNamespaces.Add("System"); sc.UsingNamespaces.Add("System.Collections.Generic"); sc.UsingNamespaces.Add("Newtonsoft.Json"); Namespace ns = new Namespace("HigLabo.Net.Vimeo.Api_3_2"); sc.Namespaces.Add(ns); AddEntityClasses(doc, ns, apiInfo); return sc; }
private List<VimeoApiEndpointInfo> CreateApiInfoList(String apiPlaygroundUrl) { var l = new List<VimeoApiEndpointInfo>(); HttpClient cl = new HttpClient(); var html = cl.GetBodyText(apiPlaygroundUrl); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); var nodes = doc.DocumentNode.SelectNodes("//div[@id='playground_table']//ul[@class='switch_menu js-switch_menu']//a[@class='http_method']"); if (nodes == null) { nodes = doc.DocumentNode.SelectNodes("//div[@id='playground_table']//div[@class='switch_current js-switch_current ']//span[@class='http_method']"); } foreach (var item in nodes) { var apiInfo = new VimeoApiEndpointInfo(); apiInfo.ApiPlaygroundUrl = apiPlaygroundUrl; apiInfo.ApiPath = apiPlaygroundUrl.Replace("https://developer.vimeo.com/api/playground/", ""); apiInfo.HttpMethodName = ToPascalCase(item.InnerText.Trim().ToLower()); var apiNames = apiInfo.ApiPath; var ss = apiNames.Split('/'); apiInfo.Name1 = ss[0]; if (ss.Length == 1) { apiInfo.Name2 = ToPascalCase(apiInfo.HttpMethodName); } if (ss.Length == 2) { apiInfo.Name2 = ss[1]; } else { for (int i = 1; i < ss.Length; i++) { apiInfo.Name2 += ss[i]; if (i < ss.Length - 1) { apiInfo.Name2 += "_"; } } } apiInfo.Name2 = apiInfo.Name2.Replace("_id", "ID").Replace("{", "").Replace("}", ""); if (apiInfo.Name2.IsNullOrEmpty() == true) { apiInfo.Name2 = apiInfo.HttpMethodName; } else { apiInfo.Name2 = apiInfo.Name2 + "_" + apiInfo.HttpMethodName; } apiInfo.Name1 = ToPascalCase(apiInfo.Name1); apiInfo.Name2 = ToPascalCase(apiInfo.Name2); l.Add(apiInfo); } return l; }
private String CallApi(VimeoApiEndpointInfo apiInfo, Dictionary<String, String> parameters) { var cl = new HttpClient(); var methodName = apiInfo.HttpMethodName.ToEnum<HttpMethodName>().Value; var qs = new QueryStringConverter(); var url = String.Format("https://api.vimeo.com/{0}?{1}", apiInfo.ApiPath, qs.Write(parameters)); foreach (var key in _IDParameterValues.Keys) { url = url.Replace("{" + key + "}", _IDParameterValues[key]); } var cm = new HttpRequestCommand(url); cm.MethodName = methodName; if (cm.MethodName != HttpMethodName.Get) { cm.SetBodyStream(new HttpBodyFormUrlEncodedData(parameters)); } var json = cl.GetBodyText(cm); return json; }