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();
        }
Пример #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="responseEncoding"></param>
        /// <param name="idKey"></param>
        /// <param name="id"></param>
        /// <param name="passwordKey"></param>
        /// <param name="password"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public static CookieContainer GetCookieContainer(String url, Encoding responseEncoding, String idKey, String id, String passwordKey, String password
            , Dictionary<String, String> values)
        {
            CookieContainer cc = new CookieContainer();
            HttpClient cl = new HttpClient();
            cl.ResponseEncoding = responseEncoding;
            cl.CookieContainer = cc;

            var cm = new HttpRequestCommand(url);
            cm.ContentType = HttpClient.ApplicationFormUrlEncoded;
            cm.MethodName = HttpMethodName.Post;
            var d = cm.Headers;
            d[idKey] = id;
            d[passwordKey] = password;
            foreach (var key in values.Keys)
            {
                d[key] = values[key];
            }
            var res = cl.GetResponse(cm);

            return cc;
        }
        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;
        }
Пример #8
0
        internal HttpResponse(HttpWebResponse response, HttpClient client)
#if !SILVERLIGHT && !NETFX_CORE && !Pcl
            : this(response, client.ResponseEncodingDetectionMode, client.ResponseEncodingDetectionMaxLineNumber, client.ResponseEncoding, client.ResponseGZipDecompress)
#else
            : this(response, client.ResponseEncodingDetectionMode, client.ResponseEncodingDetectionMaxLineNumber, client.ResponseEncoding, false)