diff --git a/README.md b/README.md index a503366..6e84a52 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,9 @@ To install this module, copy the AlchemyAPI.dll file created from the previous .NET import directory, or set your MONO_PATH environment variable to point to the directory containing the compiled AlchemyAPI.dll file. +TARGET FRAMEWORK + +Native HTTPClient async support is present within .Net Framework 4.5. Backwards compatibility can be enabled by removing AlchemyAPI_Async.cs and setting the Target Framework for the examples and sdk to the desired version. RUNNING EXAMPLES diff --git a/example/author/app.config b/example/author/app.config new file mode 100644 index 0000000..51278a4 --- /dev/null +++ b/example/author/app.config @@ -0,0 +1,3 @@ + + + diff --git a/example/author/author.csproj b/example/author/author.csproj index c8a7fed..db52c37 100644 --- a/example/author/author.csproj +++ b/example/author/author.csproj @@ -1,4 +1,4 @@ - + Debug @@ -9,6 +9,8 @@ Exe author author + v4.5 + true @@ -19,6 +21,7 @@ prompt 4 true + false none @@ -27,6 +30,7 @@ prompt 4 true + false @@ -38,12 +42,6 @@ - - - {799E354F-E323-408C-9733-7F1896342663} - sdk - - api_key.txt @@ -57,8 +55,13 @@ data\microformats.html PreserveNewest + + - + + {799e354f-e323-408c-9733-7f1896342663} + sdk + \ No newline at end of file diff --git a/example/combined/app.config b/example/combined/app.config new file mode 100644 index 0000000..51278a4 --- /dev/null +++ b/example/combined/app.config @@ -0,0 +1,3 @@ + + + diff --git a/example/combined/combined.csproj b/example/combined/combined.csproj index 5a66ac7..9cfec9d 100644 --- a/example/combined/combined.csproj +++ b/example/combined/combined.csproj @@ -1,4 +1,4 @@ - + Debug @@ -9,6 +9,8 @@ Exe combined combined + v4.5 + true @@ -19,6 +21,7 @@ prompt 4 true + false none @@ -27,6 +30,7 @@ prompt 4 true + false @@ -49,5 +53,6 @@ api_key.txt PreserveNewest + \ No newline at end of file diff --git a/example/image/app.config b/example/image/app.config new file mode 100644 index 0000000..51278a4 --- /dev/null +++ b/example/image/app.config @@ -0,0 +1,3 @@ + + + diff --git a/example/image/image.csproj b/example/image/image.csproj index a76203b..66bc97d 100644 --- a/example/image/image.csproj +++ b/example/image/image.csproj @@ -1,4 +1,4 @@ - + Debug @@ -9,6 +9,8 @@ Exe image image + v4.5 + true @@ -19,6 +21,7 @@ prompt 4 true + false none @@ -27,6 +30,7 @@ prompt 4 true + false @@ -43,6 +47,7 @@ api_key.txt PreserveNewest + diff --git a/example/ranked_image_keywords.cs b/example/ranked_image_keywords.cs index ebf77f0..a160747 100644 --- a/example/ranked_image_keywords.cs +++ b/example/ranked_image_keywords.cs @@ -1,7 +1,7 @@ using System; using System.IO; using AlchemyAPI; - +using System.Drawing; public class TestApp { diff --git a/example/ranked_image_keywords/app.config b/example/ranked_image_keywords/app.config new file mode 100644 index 0000000..51278a4 --- /dev/null +++ b/example/ranked_image_keywords/app.config @@ -0,0 +1,3 @@ + + + diff --git a/example/ranked_image_keywords/ranked_image_keywords.csproj b/example/ranked_image_keywords/ranked_image_keywords.csproj index 5ec8d58..ff37cf7 100644 --- a/example/ranked_image_keywords/ranked_image_keywords.csproj +++ b/example/ranked_image_keywords/ranked_image_keywords.csproj @@ -1,4 +1,4 @@ - + Debug @@ -9,6 +9,8 @@ Exe ranked_image_keywords ranked_image_keywords + v4.5 + true @@ -19,6 +21,7 @@ prompt 4 true + false none @@ -27,9 +30,12 @@ prompt 4 true + false + + @@ -42,6 +48,7 @@ api_key.txt + diff --git a/example/taxonomy/app.config b/example/taxonomy/app.config new file mode 100644 index 0000000..51278a4 --- /dev/null +++ b/example/taxonomy/app.config @@ -0,0 +1,3 @@ + + + diff --git a/example/taxonomy/taxo_api_key.txt b/example/taxonomy/taxo_api_key.txt new file mode 100644 index 0000000..5f28270 --- /dev/null +++ b/example/taxonomy/taxo_api_key.txt @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/example/taxonomy/taxonomy.csproj b/example/taxonomy/taxonomy.csproj index 5f88692..8c39145 100644 --- a/example/taxonomy/taxonomy.csproj +++ b/example/taxonomy/taxonomy.csproj @@ -1,4 +1,4 @@ - + Debug @@ -9,6 +9,8 @@ Exe taxonomy taxonomy + v4.5 + true @@ -19,6 +21,7 @@ prompt 4 true + false none @@ -27,6 +30,7 @@ prompt 4 true + false @@ -47,13 +51,10 @@ data\microformats.html PreserveNewest - - taxo_api_key.txt - PreserveNewest - + - + diff --git a/module/AlchemyAPI.cs b/module/AlchemyAPI.cs index a936ec6..06caf78 100644 --- a/module/AlchemyAPI.cs +++ b/module/AlchemyAPI.cs @@ -7,814 +7,812 @@ using System.Text; using System.Web; using System.Xml; +using System.Threading.Tasks; +using System.Net.Http; namespace AlchemyAPI { - public class AlchemyAPI + public partial class AlchemyAPI { - private string _apiKey; - private string _requestUri; - - public AlchemyAPI() - { - _apiKey = ""; - _requestUri = "http://access.alchemyapi.com/calls/"; - } - - public void SetAPIHost(string apiHost) - { - if (apiHost.Length < 2) - { - System.ApplicationException ex = - new System.ApplicationException("Error setting API host."); - - throw ex; - } - - _requestUri = "http://" + apiHost + ".alchemyapi.com/calls/"; - } - - public void SetAPIKey(string apiKey) - { - _apiKey = apiKey; - - if (_apiKey.Length < 5) - { - System.ApplicationException ex = - new System.ApplicationException("Error setting API key."); - - throw ex; - } - } - - public void LoadAPIKey(string filename) - { - StreamReader reader; - - reader = File.OpenText(filename); - - string line = reader.ReadLine(); - - reader.Close(); - - _apiKey = line.Trim(); - - if (_apiKey.Length < 5) - { - System.ApplicationException ex = - new System.ApplicationException("Error loading API key."); - - throw ex; - } - } - - #region GetAuthor - public string URLGetAuthor(string url) - { - CheckURL(url); - - return URLGetAuthor(url, new AlchemyAPI_BaseParams()); - } - - public string URLGetAuthor(string url, AlchemyAPI_BaseParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetAuthor", "url", parameters); - } - - public string HTMLGetAuthor(string html,string url) - { - CheckHTML(html, url); - - return HTMLGetAuthor(html, url, new AlchemyAPI_BaseParams()); - } - - public string HTMLGetAuthor(string html, string url, AlchemyAPI_BaseParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetAuthor", "html", parameters); - } - #endregion - - #region GetRankedNamedEntities - public string URLGetRankedNamedEntities(string url) - { - CheckURL(url); - - return URLGetRankedNamedEntities(url, new AlchemyAPI_EntityParams()); - } - - public string URLGetRankedNamedEntities(string url, AlchemyAPI_EntityParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetRankedNamedEntities", "url", parameters); - } - - public string HTMLGetRankedNamedEntities(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetRankedNamedEntities(html, url, new AlchemyAPI_EntityParams()); - } - - - public string HTMLGetRankedNamedEntities(string html, string url, AlchemyAPI_EntityParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetRankedNamedEntities", "html", parameters); - } - - public string TextGetRankedNamedEntities(string text) - { - CheckText(text); - - return TextGetRankedNamedEntities(text,new AlchemyAPI_EntityParams()); - } - - public string TextGetRankedNamedEntities(string text, AlchemyAPI_EntityParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST("TextGetRankedNamedEntities", "text", parameters); - } - #endregion - - #region GetRankedConcepts - public string URLGetRankedConcepts(string url) - { - CheckURL(url); - - return URLGetRankedConcepts(url, new AlchemyAPI_ConceptParams()); - } - - public string URLGetRankedConcepts(string url, AlchemyAPI_ConceptParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetRankedConcepts", "url", parameters); - } - - public string HTMLGetRankedConcepts(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetRankedConcepts(html, url, new AlchemyAPI_ConceptParams()); - } - - public string HTMLGetRankedConcepts(string html, string url, AlchemyAPI_ConceptParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetRankedConcepts", "html", parameters); - } - - public string TextGetRankedConcepts(string text) - { - CheckText(text); - - return TextGetRankedConcepts(text,new AlchemyAPI_ConceptParams()); - } - - public string TextGetRankedConcepts(string text, AlchemyAPI_ConceptParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST("TextGetRankedConcepts", "text", parameters); - } - #endregion - - #region GetRankedKeywords - public string URLGetRankedKeywords(string url) - { - CheckURL(url); - - return URLGetRankedKeywords(url, new AlchemyAPI_KeywordParams()); - } - - public string URLGetRankedKeywords(string url, AlchemyAPI_KeywordParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetRankedKeywords", "url", parameters); - } - - public string HTMLGetRankedKeywords(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetRankedKeywords(html, url, new AlchemyAPI_KeywordParams()); - } - - public string HTMLGetRankedKeywords(string html, string url, AlchemyAPI_KeywordParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetRankedKeywords", "html", parameters); - } - - public string TextGetRankedKeywords(string text) - { - CheckText(text); - - return TextGetRankedKeywords(text,new AlchemyAPI_KeywordParams()); - } - - public string TextGetRankedKeywords(string text, AlchemyAPI_KeywordParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST("TextGetRankedKeywords", "text", parameters); - } - #endregion - - #region GetLanguage - public string URLGetLanguage(string url) - { - CheckURL(url); - - return URLGetLanguage(url,new AlchemyAPI_LanguageParams()); - } - - public string URLGetLanguage(string url, AlchemyAPI_LanguageParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetLanguage", "url", parameters); - } - - public string HTMLGetLanguage(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetLanguage(html,url,new AlchemyAPI_LanguageParams()); - } - - public string HTMLGetLanguage(string html, string url, AlchemyAPI_LanguageParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetLanguage", "html", parameters); - } - - public string TextGetLanguage(string text) - { - CheckText(text); - - return TextGetLanguage(text, new AlchemyAPI_LanguageParams()); - } - - public string TextGetLanguage(string text, AlchemyAPI_LanguageParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST("TextGetLanguage", "text", parameters); - } - #endregion - - #region GetCategory - public string URLGetCategory(string url) - { - CheckURL(url); - - return URLGetCategory(url, new AlchemyAPI_CategoryParams() ); - } - - public string URLGetCategory(string url, AlchemyAPI_CategoryParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetCategory", "url", parameters); - } - - public string HTMLGetCategory(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetCategory(html, url, new AlchemyAPI_CategoryParams()); - } - - public string HTMLGetCategory(string html, string url, AlchemyAPI_CategoryParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetCategory", "html", parameters); - } - - public string TextGetCategory(string text) - { - CheckText(text); - - return TextGetCategory(text, new AlchemyAPI_CategoryParams()); - } - - public string TextGetCategory(string text, AlchemyAPI_CategoryParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST("TextGetCategory", "text", parameters); - } - #endregion - - #region GetText - public string URLGetText(string url) - { - CheckURL(url); - - return URLGetText(url, new AlchemyAPI_TextParams()); - } - - public string URLGetText(string url, AlchemyAPI_TextParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetText", "url", parameters); - } - - public string HTMLGetText(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetText(html,url, new AlchemyAPI_TextParams()); - } - - public string HTMLGetText(string html, string url,AlchemyAPI_TextParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetText", "html", parameters); - } - #endregion - - #region GetRawText - public string URLGetRawText(string url) - { - CheckURL(url); - - return URLGetRawText(url, new AlchemyAPI_BaseParams()); - } - - public string URLGetRawText(string url, AlchemyAPI_BaseParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetRawText", "url", parameters); - } - - public string HTMLGetRawText(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetRawText(html, url, new AlchemyAPI_BaseParams()); - } - - public string HTMLGetRawText(string html, string url, AlchemyAPI_BaseParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetRawText", "html", parameters); - } - #endregion - - #region GetTitle - public string URLGetTitle(string url) - { - CheckURL(url); - - return URLGetTitle(url, new AlchemyAPI_BaseParams()); - } - - public string URLGetTitle(string url, AlchemyAPI_BaseParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetTitle", "url", parameters); - } - - public string HTMLGetTitle(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetTitle(html, url, new AlchemyAPI_BaseParams()); - } - - public string HTMLGetTitle(string html, string url, AlchemyAPI_BaseParams parameters) - - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetTitle", "html", parameters); - } - #endregion - - #region GetFeedLinks - public string URLGetFeedLinks(string url) - { - CheckURL(url); - - return URLGetFeedLinks(url, new AlchemyAPI_BaseParams()); - } - - public string URLGetFeedLinks(string url, AlchemyAPI_BaseParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetFeedLinks", "url", parameters); - } - - public string HTMLGetFeedLinks(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetFeedLinks(html,url, new AlchemyAPI_BaseParams()); - } - - public string HTMLGetFeedLinks(string html, string url, AlchemyAPI_BaseParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetFeedLinks", "html", parameters); - } - #endregion - - #region GetMicroformats - public string URLGetMicroformats(string url) - { - CheckURL(url); - - return URLGetMicroformats(url, new AlchemyAPI_BaseParams()); - } - - public string URLGetMicroformats(string url, AlchemyAPI_BaseParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetMicroformatData", "url", parameters); - } - - public string HTMLGetMicroformats(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetMicroformats(html,url, new AlchemyAPI_BaseParams()); - } - - public string HTMLGetMicroformats(string html, string url, AlchemyAPI_BaseParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetMicroformatData", "html", parameters); - } - #endregion - - #region GetConstraintQuery - public string URLGetConstraintQuery(string url, string query) - { - CheckURL(url); - if (query.Length < 2) - { - System.ApplicationException ex = - new System.ApplicationException("Invalid constraint query specified."); - - throw ex; - } - - AlchemyAPI_ConstraintQueryParams cqParams = new AlchemyAPI_ConstraintQueryParams(); - cqParams.setCQuery(query); - - return URLGetConstraintQuery(url,cqParams); - } - - public string URLGetConstraintQuery(string url, AlchemyAPI_ConstraintQueryParams parameters) - { - CheckURL(url); - if (parameters.getCQuery().Length < 2) - { - System.ApplicationException ex = - new System.ApplicationException("Invalid constraint query specified."); - - throw ex; - } - - parameters.setUrl(url); - - return POST("URLGetConstraintQuery", "url", parameters); - } - - public string HTMLGetConstraintQuery(string html, string url, string query) - { - CheckHTML(html, url); - if (query.Length < 2) - { - System.ApplicationException ex = - new System.ApplicationException("Invalid constraint query specified."); - - throw ex; - } - - AlchemyAPI_ConstraintQueryParams cqParams = new AlchemyAPI_ConstraintQueryParams(); - cqParams.setCQuery(query); - - return HTMLGetConstraintQuery(html, url, cqParams); - } - - public string HTMLGetConstraintQuery(string html, string url, AlchemyAPI_ConstraintQueryParams parameters) - { - CheckHTML(html, url); - if (parameters.getCQuery().Length < 2) - { - System.ApplicationException ex = - new System.ApplicationException("Invalid constraint query specified."); - - throw ex; - } - - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetConstraintQuery", "html", parameters); - } - #endregion - - #region GetTextSentiment - public string URLGetTextSentiment(string url) - { - CheckURL(url); - - return URLGetTextSentiment(url, new AlchemyAPI_BaseParams()); - } - - public string URLGetTextSentiment(string url, AlchemyAPI_BaseParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetTextSentiment", "url", parameters); - } - - public string HTMLGetTextSentiment(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetTextSentiment(html, url, new AlchemyAPI_BaseParams()); - } - - public string HTMLGetTextSentiment(string html, string url, AlchemyAPI_BaseParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetTextSentiment", "html", parameters); - } - - public string TextGetTextSentiment(string text) - { - CheckText(text); - - return TextGetTextSentiment(text,new AlchemyAPI_BaseParams()); - } - - public string TextGetTextSentiment(string text, AlchemyAPI_BaseParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST("TextGetTextSentiment", "text", parameters); - } - #endregion - - #region GetTargetedSentiment - public string URLGetTargetedSentiment(string url, string target) - { - CheckURL(url); - CheckText(target); - - return URLGetTargetedSentiment(url, target, new AlchemyAPI_TargetedSentimentParams()); - } - - public string URLGetTargetedSentiment(string url, string target, AlchemyAPI_TargetedSentimentParams parameters) - { - CheckURL(url); - CheckText(target); - - parameters.setUrl(url); - parameters.setTarget(target); - - return GET("URLGetTargetedSentiment", "url", parameters); - } - - public string HTMLGetTargetedSentiment(string html, string url, string target) - { - CheckHTML(html, url); - CheckText(target); - - return HTMLGetTargetedSentiment(html, url, target, new AlchemyAPI_TargetedSentimentParams()); - } - - public string HTMLGetTargetedSentiment(string html, string url, string target, AlchemyAPI_TargetedSentimentParams parameters) - { - - CheckHTML(html, url); - CheckText(target); - - parameters.setHtml(html); - parameters.setUrl(url); - parameters.setTarget(target); - - return POST("HTMLGetTargetedSentiment", "html", parameters); - } - - public string TextGetTargetedSentiment(string text, string target) - { - CheckText(text); - CheckText(target); - - return TextGetTargetedSentiment(text, target, new AlchemyAPI_TargetedSentimentParams()); - } - - public string TextGetTargetedSentiment(string text, string target, AlchemyAPI_TargetedSentimentParams parameters) - { - CheckText(text); - CheckText(target); - - parameters.setText(text); - parameters.setTarget(target); - - return POST("TextGetTargetedSentiment", "text", parameters); - } - #endregion - - #region GetRelations - public string URLGetRelations(string url) - { - CheckURL(url); - - return URLGetRelations(url, new AlchemyAPI_RelationParams()); - } - - public string URLGetRelations(string url, AlchemyAPI_RelationParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET("URLGetRelations", "url", parameters); - } - - public string HTMLGetRelations(string html, string url) - { - CheckHTML(html, url); - - return HTMLGetRelations(html, url, new AlchemyAPI_RelationParams()); - } - - public string HTMLGetRelations(string html, string url, AlchemyAPI_RelationParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST("HTMLGetRelations", "html", parameters); - } - - public string TextGetRelations(string text) - { - CheckText(text); - - return TextGetRelations(text,new AlchemyAPI_RelationParams()); - } - - public string TextGetRelations(string text, AlchemyAPI_RelationParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST("TextGetRelations", "text", parameters); - } - #endregion - - #region GetCombinedData - public string URLGetCombinedData(string url, AlchemyAPI_CombinedDataParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET ("URLGetCombinedData", "url", parameters); - } - public string TextGetCombinedData(string text, AlchemyAPI_CombinedDataParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST ("TextGetCombinedData", "text", parameters); - } - #endregion - - #region GetRankedTaxonomy - public string URLGetRankedTaxonomy (string url) - { - return URLGetRankedTaxonomy(url, new AlchemyAPI_TaxonomyParams()); - } - public string URLGetRankedTaxonomy (string url, AlchemyAPI_TaxonomyParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET ("URLGetRankedTaxonomy", "url", parameters); - } - public string HTMLGetRankedTaxonomy (string html, string url) - { - return HTMLGetRankedTaxonomy(html, url, new AlchemyAPI_TaxonomyParams()); - } - public string HTMLGetRankedTaxonomy (string html, string url, AlchemyAPI_TaxonomyParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST ("HTMLGetRankedTaxonomy", "html", parameters); - } - public string TextGetRankedTaxonomy (string text) - { - return TextGetRankedTaxonomy(text, new AlchemyAPI_TaxonomyParams()); - } - public string TextGetRankedTaxonomy (string text, AlchemyAPI_TaxonomyParams parameters) - { - CheckText(text); - parameters.setText(text); - - return POST ("TextGetRankedTaxonomy", "text", parameters); - } - #endregion - - #region GetImage - public string HTMLGetImage (string html, string url) - { - return HTMLGetImage(html, url, new AlchemyAPI_ImageParams()); - } - public string HTMLGetImage (string html, string url, AlchemyAPI_ImageParams parameters) - { - CheckHTML(html, url); - parameters.setHtml(html); - parameters.setUrl(url); - - return POST ("HTMLGetImage", "html", parameters); - } - public string URLGetImage (string url) - { - return URLGetImage (url, new AlchemyAPI_ImageParams()); - } - public string URLGetImage (string url, AlchemyAPI_ImageParams parameters) - { - CheckURL(url); - parameters.setUrl(url); - - return GET ("URLGetImage", "url", parameters); - } - #endregion - + private string _apiKey; + private string _requestUri; + + public AlchemyAPI() + { + _apiKey = ""; + _requestUri = "http://access.alchemyapi.com/calls/"; + } + + public void SetAPIHost(string apiHost) + { + if (apiHost.Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Error setting API host."); + + throw ex; + } + + _requestUri = "http://" + apiHost + ".alchemyapi.com/calls/"; + } + + public void SetAPIKey(string apiKey) + { + _apiKey = apiKey; + + if (_apiKey.Length < 5) + { + System.ApplicationException ex = + new System.ApplicationException("Error setting API key."); + + throw ex; + } + } + + public void LoadAPIKey(string filename) + { + using (StreamReader reader = File.OpenText(filename)) + { + string line = reader.ReadLine(); + + _apiKey = line.Trim(); + + if (_apiKey.Length < 5) + { + System.ApplicationException ex = + new System.ApplicationException("Error loading API key."); + + throw ex; + } + } + } + + #region GetAuthor + public string URLGetAuthor(string url) + { + CheckURL(url); + + return URLGetAuthor(url, new AlchemyAPI_BaseParams()); + } + + public string URLGetAuthor(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetAuthor", "url", parameters); + } + + public string HTMLGetAuthor(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetAuthor(html, url, new AlchemyAPI_BaseParams()); + } + + public string HTMLGetAuthor(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetAuthor", "html", parameters); + } + #endregion + + #region GetRankedNamedEntities + public string URLGetRankedNamedEntities(string url) + { + CheckURL(url); + + return URLGetRankedNamedEntities(url, new AlchemyAPI_EntityParams()); + } + + public string URLGetRankedNamedEntities(string url, AlchemyAPI_EntityParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetRankedNamedEntities", "url", parameters); + } + + public string HTMLGetRankedNamedEntitiesAsync(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetRankedNamedEntities(html, url, new AlchemyAPI_EntityParams()); + } + + + public string HTMLGetRankedNamedEntities(string html, string url, AlchemyAPI_EntityParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetRankedNamedEntities", "html", parameters); + } + + public string TextGetRankedNamedEntities(string text) + { + CheckText(text); + + return TextGetRankedNamedEntities(text, new AlchemyAPI_EntityParams()); + } + + public string TextGetRankedNamedEntities(string text, AlchemyAPI_EntityParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetRankedNamedEntities", "text", parameters); + } + #endregion + + #region GetRankedConcepts + public string URLGetRankedConcepts(string url) + { + CheckURL(url); + + return URLGetRankedConcepts(url, new AlchemyAPI_ConceptParams()); + } + + public string URLGetRankedConcepts(string url, AlchemyAPI_ConceptParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetRankedConcepts", "url", parameters); + } + + public string HTMLGetRankedConcepts(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetRankedConcepts(html, url, new AlchemyAPI_ConceptParams()); + } + + public string HTMLGetRankedConcepts(string html, string url, AlchemyAPI_ConceptParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetRankedConcepts", "html", parameters); + } + + public string TextGetRankedConcepts(string text) + { + CheckText(text); + + return TextGetRankedConcepts(text, new AlchemyAPI_ConceptParams()); + } + + public string TextGetRankedConcepts(string text, AlchemyAPI_ConceptParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetRankedConcepts", "text", parameters); + } + #endregion + + #region GetRankedKeywords + public string URLGetRankedKeywords(string url) + { + CheckURL(url); + + return URLGetRankedKeywords(url, new AlchemyAPI_KeywordParams()); + } + + public string URLGetRankedKeywords(string url, AlchemyAPI_KeywordParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetRankedKeywords", "url", parameters); + } + + public string HTMLGetRankedKeywords(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetRankedKeywords(html, url, new AlchemyAPI_KeywordParams()); + } + + public string HTMLGetRankedKeywords(string html, string url, AlchemyAPI_KeywordParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetRankedKeywords", "html", parameters); + } + + public string TextGetRankedKeywords(string text) + { + CheckText(text); + + return TextGetRankedKeywords(text, new AlchemyAPI_KeywordParams()); + } + + public string TextGetRankedKeywords(string text, AlchemyAPI_KeywordParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetRankedKeywords", "text", parameters); + } + #endregion + + #region GetLanguage + public string URLGetLanguage(string url) + { + CheckURL(url); + + return URLGetLanguage(url, new AlchemyAPI_LanguageParams()); + } + + public string URLGetLanguage(string url, AlchemyAPI_LanguageParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetLanguage", "url", parameters); + } + + public string HTMLGetLanguage(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetLanguage(html, url, new AlchemyAPI_LanguageParams()); + } + + public string HTMLGetLanguage(string html, string url, AlchemyAPI_LanguageParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetLanguage", "html", parameters); + } + + public string TextGetLanguage(string text) + { + CheckText(text); + + return TextGetLanguage(text, new AlchemyAPI_LanguageParams()); + } + + public string TextGetLanguage(string text, AlchemyAPI_LanguageParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetLanguage", "text", parameters); + } + #endregion + + #region GetCategory + public string URLGetCategory(string url) + { + CheckURL(url); + + return URLGetCategory(url, new AlchemyAPI_CategoryParams()); + } + + public string URLGetCategory(string url, AlchemyAPI_CategoryParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetCategory", "url", parameters); + } + + public string HTMLGetCategory(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetCategory(html, url, new AlchemyAPI_CategoryParams()); + } + + public string HTMLGetCategory(string html, string url, AlchemyAPI_CategoryParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetCategory", "html", parameters); + } + + public string TextGetCategory(string text) + { + CheckText(text); + + return TextGetCategory(text, new AlchemyAPI_CategoryParams()); + } + + public string TextGetCategory(string text, AlchemyAPI_CategoryParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetCategory", "text", parameters); + } + #endregion + + #region GetText + public string URLGetText(string url) + { + CheckURL(url); + + return URLGetText(url, new AlchemyAPI_TextParams()); + } + + public string URLGetText(string url, AlchemyAPI_TextParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetText", "url", parameters); + } + + public string HTMLGetText(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetText(html, url, new AlchemyAPI_TextParams()); + } + + public string HTMLGetText(string html, string url, AlchemyAPI_TextParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetText", "html", parameters); + } + #endregion + + #region GetRawText + public string URLGetRawText(string url) + { + CheckURL(url); + + return URLGetRawText(url, new AlchemyAPI_BaseParams()); + } + + public string URLGetRawText(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetRawText", "url", parameters); + } + + public string HTMLGetRawText(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetRawText(html, url, new AlchemyAPI_BaseParams()); + } + + public string HTMLGetRawText(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetRawText", "html", parameters); + } + #endregion + + #region GetTitle + public string URLGetTitle(string url) + { + CheckURL(url); + + return URLGetTitle(url, new AlchemyAPI_BaseParams()); + } + + public string URLGetTitle(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetTitle", "url", parameters); + } + + public string HTMLGetTitle(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetTitle(html, url, new AlchemyAPI_BaseParams()); + } + + public string HTMLGetTitle(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetTitle", "html", parameters); + } + #endregion + + #region GetFeedLinks + public string URLGetFeedLinks(string url) + { + CheckURL(url); + + return URLGetFeedLinks(url, new AlchemyAPI_BaseParams()); + } + + public string URLGetFeedLinks(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetFeedLinks", "url", parameters); + } + + public string HTMLGetFeedLinks(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetFeedLinks(html, url, new AlchemyAPI_BaseParams()); + } + + public string HTMLGetFeedLinks(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetFeedLinks", "html", parameters); + } + #endregion + + #region GetMicroformats + public string URLGetMicroformats(string url) + { + CheckURL(url); + + return URLGetMicroformats(url, new AlchemyAPI_BaseParams()); + } + + public string URLGetMicroformats(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetMicroformatData", "url", parameters); + } + + public string HTMLGetMicroformats(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetMicroformats(html, url, new AlchemyAPI_BaseParams()); + } + + public string HTMLGetMicroformats(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetMicroformatData", "html", parameters); + } + #endregion + + #region GetConstraintQuery + public string URLGetConstraintQuery(string url, string query) + { + CheckURL(url); + if (query.Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Invalid constraint query specified."); + + throw ex; + } + + AlchemyAPI_ConstraintQueryParams cqParams = new AlchemyAPI_ConstraintQueryParams(); + cqParams.setCQuery(query); + + return URLGetConstraintQuery(url, cqParams); + } + + public string URLGetConstraintQuery(string url, AlchemyAPI_ConstraintQueryParams parameters) + { + CheckURL(url); + if (parameters.getCQuery().Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Invalid constraint query specified."); + + throw ex; + } + + parameters.setUrl(url); + + return POST("URLGetConstraintQuery", "url", parameters); + } + + public string HTMLGetConstraintQuery(string html, string url, string query) + { + CheckHTML(html, url); + if (query.Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Invalid constraint query specified."); + + throw ex; + } + + AlchemyAPI_ConstraintQueryParams cqParams = new AlchemyAPI_ConstraintQueryParams(); + cqParams.setCQuery(query); + + return HTMLGetConstraintQuery(html, url, cqParams); + } + + public string HTMLGetConstraintQuery(string html, string url, AlchemyAPI_ConstraintQueryParams parameters) + { + CheckHTML(html, url); + if (parameters.getCQuery().Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Invalid constraint query specified."); + + throw ex; + } + + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetConstraintQuery", "html", parameters); + } + #endregion + + #region GetTextSentiment + public string URLGetTextSentiment(string url) + { + CheckURL(url); + + return URLGetTextSentiment(url, new AlchemyAPI_BaseParams()); + } + + public string URLGetTextSentiment(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetTextSentiment", "url", parameters); + } + + public string HTMLGetTextSentiment(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetTextSentiment(html, url, new AlchemyAPI_BaseParams()); + } + + public string HTMLGetTextSentiment(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetTextSentiment", "html", parameters); + } + + public string TextGetTextSentiment(string text) + { + CheckText(text); + + return TextGetTextSentiment(text, new AlchemyAPI_BaseParams()); + } + + public string TextGetTextSentiment(string text, AlchemyAPI_BaseParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetTextSentiment", "text", parameters); + } + #endregion + + #region GetTargetedSentiment + public string URLGetTargetedSentiment(string url, string target) + { + CheckURL(url); + CheckText(target); + + return URLGetTargetedSentiment(url, target, new AlchemyAPI_TargetedSentimentParams()); + } + + public string URLGetTargetedSentiment(string url, string target, AlchemyAPI_TargetedSentimentParams parameters) + { + CheckURL(url); + CheckText(target); + + parameters.setUrl(url); + parameters.setTarget(target); + + return GET("URLGetTargetedSentiment", "url", parameters); + } + + public string HTMLGetTargetedSentiment(string html, string url, string target) + { + CheckHTML(html, url); + CheckText(target); + + return HTMLGetTargetedSentiment(html, url, target, new AlchemyAPI_TargetedSentimentParams()); + } + + public string HTMLGetTargetedSentiment(string html, string url, string target, AlchemyAPI_TargetedSentimentParams parameters) + { + + CheckHTML(html, url); + CheckText(target); + + parameters.setHtml(html); + parameters.setUrl(url); + parameters.setTarget(target); + + return POST("HTMLGetTargetedSentiment", "html", parameters); + } + + public string TextGetTargetedSentiment(string text, string target) + { + CheckText(text); + CheckText(target); + + return TextGetTargetedSentiment(text, target, new AlchemyAPI_TargetedSentimentParams()); + } + + public string TextGetTargetedSentiment(string text, string target, AlchemyAPI_TargetedSentimentParams parameters) + { + CheckText(text); + CheckText(target); + + parameters.setText(text); + parameters.setTarget(target); + + return POST("TextGetTargetedSentiment", "text", parameters); + } + #endregion + + #region GetRelations + public string URLGetRelations(string url) + { + CheckURL(url); + + return URLGetRelations(url, new AlchemyAPI_RelationParams()); + } + + public string URLGetRelations(string url, AlchemyAPI_RelationParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetRelations", "url", parameters); + } + + public string HTMLGetRelations(string html, string url) + { + CheckHTML(html, url); + + return HTMLGetRelations(html, url, new AlchemyAPI_RelationParams()); + } + + public string HTMLGetRelations(string html, string url, AlchemyAPI_RelationParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetRelations", "html", parameters); + } + + public string TextGetRelations(string text) + { + CheckText(text); + + return TextGetRelations(text, new AlchemyAPI_RelationParams()); + } + + public string TextGetRelations(string text, AlchemyAPI_RelationParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetRelations", "text", parameters); + } + #endregion + + #region GetCombinedData + public string URLGetCombinedData(string url, AlchemyAPI_CombinedDataParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetCombinedData", "url", parameters); + } + public string TextGetCombinedData(string text, AlchemyAPI_CombinedDataParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetCombinedData", "text", parameters); + } + #endregion + + #region GetRankedTaxonomy + public string URLGetRankedTaxonomy(string url) + { + return URLGetRankedTaxonomy(url, new AlchemyAPI_TaxonomyParams()); + } + public string URLGetRankedTaxonomy(string url, AlchemyAPI_TaxonomyParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetRankedTaxonomy", "url", parameters); + } + public string HTMLGetRankedTaxonomy(string html, string url) + { + return HTMLGetRankedTaxonomy(html, url, new AlchemyAPI_TaxonomyParams()); + } + public string HTMLGetRankedTaxonomy(string html, string url, AlchemyAPI_TaxonomyParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetRankedTaxonomy", "html", parameters); + } + public string TextGetRankedTaxonomy(string text) + { + return TextGetRankedTaxonomy(text, new AlchemyAPI_TaxonomyParams()); + } + public string TextGetRankedTaxonomy(string text, AlchemyAPI_TaxonomyParams parameters) + { + CheckText(text); + parameters.setText(text); + + return POST("TextGetRankedTaxonomy", "text", parameters); + } + #endregion + + #region GetImage + public string HTMLGetImage(string html, string url) + { + return HTMLGetImage(html, url, new AlchemyAPI_ImageParams()); + } + public string HTMLGetImage(string html, string url, AlchemyAPI_ImageParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return POST("HTMLGetImage", "html", parameters); + } + public string URLGetImage(string url) + { + return URLGetImage(url, new AlchemyAPI_ImageParams()); + } + public string URLGetImage(string url, AlchemyAPI_ImageParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return GET("URLGetImage", "url", parameters); + } + #endregion + #region GetRankedImageKeywords public string URLGetRankedImageKeywords(string url) { @@ -837,12 +835,12 @@ public string ImageGetRankedImageKeywords(AlchemyAPI_RankedImageKeywords paramet } #endregion - private void CheckHTML(string html, string url) + private void CheckHTML(string html, string url) { if (html.Length < 10) { System.ApplicationException ex = - new System.ApplicationException ("Enter a HTML document to analyze."); + new System.ApplicationException("Enter a HTML document to analyze."); throw ex; } @@ -850,48 +848,49 @@ private void CheckHTML(string html, string url) if (url.Length < 10) { System.ApplicationException ex = - new System.ApplicationException ("Enter a web URL to analyze."); + new System.ApplicationException("Enter a web URL to analyze."); throw ex; } } - private void CheckText(string text) + private void CheckText(string text) { if (text.Length < 5) { System.ApplicationException ex = - new System.ApplicationException ("Enter some text to analyze."); + new System.ApplicationException("Enter some text to analyze."); throw ex; } } - private void CheckURL(string url) + private void CheckURL(string url) { if (url.Length < 10) { System.ApplicationException ex = - new System.ApplicationException ("Enter a web URL to analyze."); + new System.ApplicationException("Enter a web URL to analyze."); throw ex; } } - private string GET(string callName, string callPrefix, AlchemyAPI_BaseParams parameters) + private string GET(string callName, string callPrefix, AlchemyAPI_BaseParams parameters) { // callMethod, callPrefix, ... params - StringBuilder uri = new StringBuilder (); + StringBuilder uri = new StringBuilder(); uri.Append(_requestUri).Append(callPrefix).Append("/").Append(callName); uri.Append("?apikey=").Append(_apiKey).Append(parameters.getParameterString()); parameters.resetBaseParams(); - Uri address = new Uri (uri.ToString()); + Uri address = new Uri(uri.ToString()); + HttpWebRequest wreq = WebRequest.Create(address) as HttpWebRequest; wreq.Proxy = null; byte[] postData = parameters.GetPostData(); - + if (postData == null) { wreq.Method = "GET"; @@ -904,20 +903,19 @@ private string GET(string callName, string callPrefix, AlchemyAPI_BaseParams par ps.Write(postData, 0, postData.Length); } } - return DoRequest(wreq, parameters.getOutputMode()); } - private string POST(string callName, string callPrefix, AlchemyAPI_BaseParams parameters) + private string POST(string callName, string callPrefix, AlchemyAPI_BaseParams parameters) { // callMethod, callPrefix, ... params - Uri address = new Uri (_requestUri + callPrefix + "/" + callName); + Uri address = new Uri(_requestUri + callPrefix + "/" + callName); HttpWebRequest wreq = WebRequest.Create(address) as HttpWebRequest; wreq.Proxy = null; wreq.Method = "POST"; wreq.ContentType = "application/x-www-form-urlencoded"; - - StringBuilder d = new StringBuilder (); + + StringBuilder d = new StringBuilder(); d.Append("apikey=").Append(_apiKey).Append(parameters.getParameterString()); parameters.resetBaseParams(); @@ -933,75 +931,73 @@ private string POST(string callName, string callPrefix, AlchemyAPI_BaseParams pa return DoRequest(wreq, parameters.getOutputMode()); } - private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode outputMode) + private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode outputMode) { + string xml; using (HttpWebResponse wres = wreq.GetResponse() as HttpWebResponse) { - StreamReader r = new StreamReader (wres.GetResponseStream()); + using (StreamReader r = new StreamReader(wres.GetResponseStream())) + { + xml = r.ReadToEnd(); + } + } + if (string.IsNullOrEmpty(xml)) + throw new XmlException("The API request returned back an empty response. Please verify that the url is correct."); - string xml = r.ReadToEnd(); + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(xml); - if (string.IsNullOrEmpty(xml)) - throw new XmlException ("The API request returned back an empty response. Please verify that the url is correct."); - - XmlDocument xmlDoc = new XmlDocument (); - xmlDoc.LoadXml(xml); + XmlElement root = xmlDoc.DocumentElement; - XmlElement root = xmlDoc.DocumentElement; + if (AlchemyAPI_BaseParams.OutputMode.XML == outputMode) + { + XmlNode status = root.SelectSingleNode("/results/status"); - if (AlchemyAPI_BaseParams.OutputMode.XML == outputMode) + if (status.InnerText != "OK") { - XmlNode status = root.SelectSingleNode("/results/status"); + string errorMessage = "Error making API call."; - if (status.InnerText != "OK") + try + { + XmlNode statusInfo = root.SelectSingleNode("/results/statusInfo"); + errorMessage = statusInfo.InnerText; + } + catch { - string errorMessage = "Error making API call."; - - try - { - XmlNode statusInfo = root.SelectSingleNode("/results/statusInfo"); - errorMessage = statusInfo.InnerText; - } - catch - { - errorMessage = "An error occurred: Unable to access XmlNode /results/statusInfo"; - } - System.ApplicationException ex = new System.ApplicationException (errorMessage); - - throw ex; + errorMessage = "An error occurred: Unable to access XmlNode /results/statusInfo"; } + System.ApplicationException ex = new System.ApplicationException(errorMessage); + + throw ex; } - else if (AlchemyAPI_BaseParams.OutputMode.RDF == outputMode) + } + else if (AlchemyAPI_BaseParams.OutputMode.RDF == outputMode) + { + XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable); + nm.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + nm.AddNamespace("aapi", "http://rdf.alchemyapi.com/rdf/v1/s/aapi-schema#"); + XmlNode status = root.SelectSingleNode("/rdf:RDF/rdf:Description/aapi:ResultStatus", nm); + + if (status.InnerText != "OK") { - XmlNamespaceManager nm = new XmlNamespaceManager (xmlDoc.NameTable); - nm.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); - nm.AddNamespace("aapi", "http://rdf.alchemyapi.com/rdf/v1/s/aapi-schema#"); - XmlNode status = root.SelectSingleNode("/rdf:RDF/rdf:Description/aapi:ResultStatus", nm); + string errorMessage = "Error making API call."; - if (status.InnerText != "OK") + try { - string errorMessage = "Error making API call."; - - try - { - XmlNode statusInfo = root.SelectSingleNode("/results/statusInfo"); - errorMessage = statusInfo.InnerText; - } - catch - { - errorMessage = "An error occurred: Unable to access XmlNode /results/statusInfo"; - } - System.ApplicationException ex = new System.ApplicationException (errorMessage); - - throw ex; + XmlNode statusInfo = root.SelectSingleNode("/results/statusInfo"); + errorMessage = statusInfo.InnerText; } - } + catch + { + errorMessage = "An error occurred: Unable to access XmlNode /results/statusInfo"; + } + System.ApplicationException ex = new System.ApplicationException(errorMessage); - return xml; - + throw ex; + } } + return xml; } } - } diff --git a/module/AlchemyAPI_Async.cs b/module/AlchemyAPI_Async.cs new file mode 100644 index 0000000..430ae55 --- /dev/null +++ b/module/AlchemyAPI_Async.cs @@ -0,0 +1,915 @@ +using System; +using System.Collections; +using System.Configuration; +using System.Data; +using System.IO; +using System.Net; +using System.Text; +using System.Web; +using System.Xml; +using System.Threading.Tasks; +using System.Net.Http; + +namespace AlchemyAPI +{ + public partial class AlchemyAPI + { + #region GetAuthor + async public Task URLGetAuthorAsync(string url) + { + CheckURL(url); + + return await URLGetAuthorAsync(url, new AlchemyAPI_BaseParams()); + } + + async public Task URLGetAuthorAsync(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetAuthor", "url", parameters); + } + + async public Task HTMLGetAuthorAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetAuthorAsync(html, url, new AlchemyAPI_BaseParams()); + } + + async public Task HTMLGetAuthorAsync(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetAuthor", "html", parameters); + } + #endregion + + #region GetRankedNamedEntities + async public Task URLGetRankedNamedEntitiesAsync(string url) + { + CheckURL(url); + + return await URLGetRankedNamedEntitiesAsync(url, new AlchemyAPI_EntityParams()); + } + + async public Task URLGetRankedNamedEntitiesAsync(string url, AlchemyAPI_EntityParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetRankedNamedEntities", "url", parameters); + } + + async public Task HTMLGetRankedNamedEntities(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetRankedNamedEntitiesAsync(html, url, new AlchemyAPI_EntityParams()); + } + + + async public Task HTMLGetRankedNamedEntitiesAsync(string html, string url, AlchemyAPI_EntityParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetRankedNamedEntities", "html", parameters); + } + async public Task TextGetRankedNamedEntitiesAsync(string text, AlchemyAPI_EntityParams parameters) + { + CheckText(text); + parameters.setText(text); + + string res = await POSTAsync("TextGetRankedNamedEntities", "text", parameters); + return res; + } + #endregion + + #region GetRankedConcepts + async public Task URLGetRankedConceptsAsync(string url) + { + CheckURL(url); + + return await URLGetRankedConceptsAsync(url, new AlchemyAPI_ConceptParams()); + } + + async public Task URLGetRankedConceptsAsync(string url, AlchemyAPI_ConceptParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetRankedConcepts", "url", parameters); + } + + async public Task HTMLGetRankedConceptsAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetRankedConceptsAsync(html, url, new AlchemyAPI_ConceptParams()); + } + + async public Task HTMLGetRankedConceptsAsync(string html, string url, AlchemyAPI_ConceptParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetRankedConcepts", "html", parameters); + } + + async public Task TextGetRankedConceptsAsync(string text) + { + CheckText(text); + + return await TextGetRankedConceptsAsync(text, new AlchemyAPI_ConceptParams()); + } + + async public Task TextGetRankedConceptsAsync(string text, AlchemyAPI_ConceptParams parameters) + { + CheckText(text); + parameters.setText(text); + + return await POSTAsync("TextGetRankedConcepts", "text", parameters); + } + #endregion + + #region GetRankedKeywords + async public Task URLGetRankedKeywordsAsync(string url) + { + CheckURL(url); + + return await URLGetRankedKeywordsAsync(url, new AlchemyAPI_KeywordParams()); + } + + async public Task URLGetRankedKeywordsAsync(string url, AlchemyAPI_KeywordParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetRankedKeywords", "url", parameters); + } + + async public Task HTMLGetRankedKeywordsAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetRankedKeywordsAsync(html, url, new AlchemyAPI_KeywordParams()); + } + + async public Task HTMLGetRankedKeywordsAsync(string html, string url, AlchemyAPI_KeywordParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetRankedKeywords", "html", parameters); + } + + async public Task TextGetRankedKeywordsAsync(string text) + { + CheckText(text); + + return await TextGetRankedKeywordsAsync(text, new AlchemyAPI_KeywordParams()); + } + + async public Task TextGetRankedKeywordsAsync(string text, AlchemyAPI_KeywordParams parameters) + { + CheckText(text); + parameters.setText(text); + + return await POSTAsync("TextGetRankedKeywords", "text", parameters); + } + #endregion + + #region GetLanguage + async public Task URLGetLanguageAsync(string url) + { + CheckURL(url); + + return await URLGetLanguageAsync(url, new AlchemyAPI_LanguageParams()); + } + + async public Task URLGetLanguageAsync(string url, AlchemyAPI_LanguageParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetLanguage", "url", parameters); + } + + async public Task HTMLGetLanguageAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetLanguageAsync(html, url, new AlchemyAPI_LanguageParams()); + } + + async public Task HTMLGetLanguageAsync(string html, string url, AlchemyAPI_LanguageParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetLanguage", "html", parameters); + } + + async public Task TextGetLanguageAsync(string text) + { + CheckText(text); + + return await TextGetLanguageAsync(text, new AlchemyAPI_LanguageParams()); + } + + async public Task TextGetLanguageAsync(string text, AlchemyAPI_LanguageParams parameters) + { + CheckText(text); + parameters.setText(text); + + return await POSTAsync("TextGetLanguage", "text", parameters); + } + #endregion + + #region GetCategory + async public Task URLGetCategoryAsync(string url) + { + CheckURL(url); + + return await URLGetCategoryAsync(url, new AlchemyAPI_CategoryParams()); + } + + async public Task URLGetCategoryAsync(string url, AlchemyAPI_CategoryParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetCategory", "url", parameters); + } + + async public Task HTMLGetCategoryAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetCategoryAsync(html, url, new AlchemyAPI_CategoryParams()); + } + + async public Task HTMLGetCategoryAsync(string html, string url, AlchemyAPI_CategoryParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetCategory", "html", parameters); + } + + async public Task TextGetCategoryAsync(string text) + { + CheckText(text); + + return await TextGetCategoryAsync(text, new AlchemyAPI_CategoryParams()); + } + + async public Task TextGetCategoryAsync(string text, AlchemyAPI_CategoryParams parameters) + { + CheckText(text); + parameters.setText(text); + + return await POSTAsync("TextGetCategory", "text", parameters); + } + #endregion + + #region GetText + async public Task URLGetTextAsync(string url) + { + CheckURL(url); + + return await URLGetTextAsync(url, new AlchemyAPI_TextParams()); + } + + async public Task URLGetTextAsync(string url, AlchemyAPI_TextParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetText", "url", parameters); + } + + async public Task HTMLGetTextAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetTextAsync(html, url, new AlchemyAPI_TextParams()); + } + + async public Task HTMLGetTextAsync(string html, string url, AlchemyAPI_TextParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetText", "html", parameters); + } + #endregion + + #region GetRawText + async public Task URLGetRawTextAsync(string url) + { + CheckURL(url); + + return await URLGetRawTextAsync(url, new AlchemyAPI_BaseParams()); + } + + async public Task URLGetRawTextAsync(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetRawText", "url", parameters); + } + + async public Task HTMLGetRawTextAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetRawTextAsync(html, url, new AlchemyAPI_BaseParams()); + } + + async public Task HTMLGetRawTextAsync(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetRawText", "html", parameters); + } + #endregion + + #region GetTitle + async public Task URLGetTitleAsync(string url) + { + CheckURL(url); + + return await URLGetTitleAsync(url, new AlchemyAPI_BaseParams()); + } + + async public Task URLGetTitleAsync(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetTitle", "url", parameters); + } + + async public Task HTMLGetTitleAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetTitleAsync(html, url, new AlchemyAPI_BaseParams()); + } + + async public Task HTMLGetTitleAsync(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetTitle", "html", parameters); + } + #endregion + + #region GetFeedLinks + async public Task URLGetFeedLinksAsync(string url) + { + CheckURL(url); + + return await URLGetFeedLinksAsync(url, new AlchemyAPI_BaseParams()); + } + + async public Task URLGetFeedLinksAsync(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetFeedLinks", "url", parameters); + } + + async public Task HTMLGetFeedLinksAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetFeedLinksAsync(html, url, new AlchemyAPI_BaseParams()); + } + + async public Task HTMLGetFeedLinksAsync(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetFeedLinks", "html", parameters); + } + #endregion + + #region GetMicroformats + async public Task URLGetMicroformatsAsync(string url) + { + CheckURL(url); + + return await URLGetMicroformatsAsync(url, new AlchemyAPI_BaseParams()); + } + + async public Task URLGetMicroformatsAsync(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetMicroformatData", "url", parameters); + } + + async public Task HTMLGetMicroformatsAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetMicroformatsAsync(html, url, new AlchemyAPI_BaseParams()); + } + + async public Task HTMLGetMicroformatsAsync(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetMicroformatData", "html", parameters); + } + #endregion + + #region GetConstraintQuery + async public Task URLGetConstraintQueryAsync(string url, string query) + { + CheckURL(url); + if (query.Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Invalid constraint query specified."); + + throw ex; + } + + AlchemyAPI_ConstraintQueryParams cqParams = new AlchemyAPI_ConstraintQueryParams(); + cqParams.setCQuery(query); + + return await URLGetConstraintQueryAsync(url, cqParams); + } + + async public Task URLGetConstraintQueryAsync(string url, AlchemyAPI_ConstraintQueryParams parameters) + { + CheckURL(url); + if (parameters.getCQuery().Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Invalid constraint query specified."); + + throw ex; + } + + parameters.setUrl(url); + + return await POSTAsync("URLGetConstraintQuery", "url", parameters); + } + + async public Task HTMLGetConstraintQueryAsync(string html, string url, string query) + { + CheckHTML(html, url); + if (query.Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Invalid constraint query specified."); + + throw ex; + } + + AlchemyAPI_ConstraintQueryParams cqParams = new AlchemyAPI_ConstraintQueryParams(); + cqParams.setCQuery(query); + + return await HTMLGetConstraintQueryAsync(html, url, cqParams); + } + + async public Task HTMLGetConstraintQueryAsync(string html, string url, AlchemyAPI_ConstraintQueryParams parameters) + { + CheckHTML(html, url); + if (parameters.getCQuery().Length < 2) + { + System.ApplicationException ex = + new System.ApplicationException("Invalid constraint query specified."); + + throw ex; + } + + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetConstraintQuery", "html", parameters); + } + #endregion + + #region GetTextSentiment + async public Task URLGetTextSentimentAsync(string url) + { + CheckURL(url); + + return await URLGetTextSentimentAsync(url, new AlchemyAPI_BaseParams()); + } + + async public Task URLGetTextSentimentAsync(string url, AlchemyAPI_BaseParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetTextSentiment", "url", parameters); + } + + async public Task HTMLGetTextSentimentAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetTextSentimentAsync(html, url, new AlchemyAPI_BaseParams()); + } + + async public Task HTMLGetTextSentimentAsync(string html, string url, AlchemyAPI_BaseParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetTextSentiment", "html", parameters); + } + + async public Task TextGetTextSentimentAsync(string text) + { + CheckText(text); + + return await TextGetTextSentimentAsync(text, new AlchemyAPI_BaseParams()); + } + + async public Task TextGetTextSentimentAsync(string text, AlchemyAPI_BaseParams parameters) + { + CheckText(text); + parameters.setText(text); + + return await POSTAsync("TextGetTextSentiment", "text", parameters); + } + #endregion + + #region GetTargetedSentiment + async public Task URLGetTargetedSentimentAsync(string url, string target) + { + CheckURL(url); + CheckText(target); + + return await URLGetTargetedSentimentAsync(url, target, new AlchemyAPI_TargetedSentimentParams()); + } + + async public Task URLGetTargetedSentimentAsync(string url, string target, AlchemyAPI_TargetedSentimentParams parameters) + { + CheckURL(url); + CheckText(target); + + parameters.setUrl(url); + parameters.setTarget(target); + + return await GETAsync("URLGetTargetedSentiment", "url", parameters); + } + + async public Task HTMLGetTargetedSentimentAsync(string html, string url, string target) + { + CheckHTML(html, url); + CheckText(target); + + return await HTMLGetTargetedSentimentAsync(html, url, target, new AlchemyAPI_TargetedSentimentParams()); + } + + async public Task HTMLGetTargetedSentimentAsync(string html, string url, string target, AlchemyAPI_TargetedSentimentParams parameters) + { + + CheckHTML(html, url); + CheckText(target); + + parameters.setHtml(html); + parameters.setUrl(url); + parameters.setTarget(target); + + return await POSTAsync("HTMLGetTargetedSentiment", "html", parameters); + } + + async public Task TextGetTargetedSentimentAsync(string text, string target) + { + CheckText(text); + CheckText(target); + + return await TextGetTargetedSentimentAsync(text, target, new AlchemyAPI_TargetedSentimentParams()); + } + + async public Task TextGetTargetedSentimentAsync(string text, string target, AlchemyAPI_TargetedSentimentParams parameters) + { + CheckText(text); + CheckText(target); + + parameters.setText(text); + parameters.setTarget(target); + + return await POSTAsync("TextGetTargetedSentiment", "text", parameters); + } + #endregion + + #region GetRelations + async public Task URLGetRelationsAsync(string url) + { + CheckURL(url); + + return await URLGetRelationsAsync(url, new AlchemyAPI_RelationParams()); + } + + async public Task URLGetRelationsAsync(string url, AlchemyAPI_RelationParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetRelations", "url", parameters); + } + + async public Task HTMLGetRelationsAsync(string html, string url) + { + CheckHTML(html, url); + + return await HTMLGetRelationsAsync(html, url, new AlchemyAPI_RelationParams()); + } + + async public Task HTMLGetRelationsAsync(string html, string url, AlchemyAPI_RelationParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetRelations", "html", parameters); + } + + async public Task TextGetRelationsAsync(string text) + { + CheckText(text); + + return await TextGetRelationsAsync(text, new AlchemyAPI_RelationParams()); + } + + async public Task TextGetRelationsAsync(string text, AlchemyAPI_RelationParams parameters) + { + CheckText(text); + parameters.setText(text); + + return await POSTAsync("TextGetRelations", "text", parameters); + } + #endregion + + #region GetCombinedData + async public Task URLGetCombinedDataAsync(string url, AlchemyAPI_CombinedDataParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetCombinedData", "url", parameters); + } + async public Task TextGetCombinedDataAsync(string text, AlchemyAPI_CombinedDataParams parameters) + { + CheckText(text); + parameters.setText(text); + + return await POSTAsync("TextGetCombinedData", "text", parameters); + } + #endregion + + #region GetRankedTaxonomy + async public Task URLGetRankedTaxonomyAsync(string url) + { + return await URLGetRankedTaxonomyAsync(url, new AlchemyAPI_TaxonomyParams()); + } + async public Task URLGetRankedTaxonomyAsync(string url, AlchemyAPI_TaxonomyParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetRankedTaxonomy", "url", parameters); + } + async public Task HTMLGetRankedTaxonomyAsync(string html, string url) + { + return await HTMLGetRankedTaxonomyAsync(html, url, new AlchemyAPI_TaxonomyParams()); + } + async public Task HTMLGetRankedTaxonomyAsync(string html, string url, AlchemyAPI_TaxonomyParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetRankedTaxonomy", "html", parameters); + } + async public Task TextGetRankedTaxonomyAsync(string text) + { + return await TextGetRankedTaxonomyAsync(text, new AlchemyAPI_TaxonomyParams()); + } + async public Task TextGetRankedTaxonomyAsync(string text, AlchemyAPI_TaxonomyParams parameters) + { + CheckText(text); + parameters.setText(text); + + return await POSTAsync("TextGetRankedTaxonomy", "text", parameters); + } + #endregion + + #region GetImage + async public Task HTMLGetImageAsync(string html, string url) + { + return await HTMLGetImageAsync(html, url, new AlchemyAPI_ImageParams()); + } + async public Task HTMLGetImageAsync(string html, string url, AlchemyAPI_ImageParams parameters) + { + CheckHTML(html, url); + parameters.setHtml(html); + parameters.setUrl(url); + + return await POSTAsync("HTMLGetImage", "html", parameters); + } + async public Task URLGetImageAsync(string url) + { + return await URLGetImageAsync(url, new AlchemyAPI_ImageParams()); + } + async public Task URLGetImageAsync(string url, AlchemyAPI_ImageParams parameters) + { + CheckURL(url); + parameters.setUrl(url); + + return await GETAsync("URLGetImage", "url", parameters); + } + #endregion + + #region GetRankedImageKeywords + async public Task URLGetRankedImageKeywordsAsync(string url) + { + AlchemyAPI_RankedImageKeywords pms = new AlchemyAPI_RankedImageKeywords + { + ImageURL = url + }; + + return await URLGetRankedImageKeywordsAsync(pms); + } + async public Task URLGetRankedImageKeywordsAsync(AlchemyAPI_RankedImageKeywords parameters) + { + CheckURL(parameters.ImageURL); + + return await GETAsync("URLGetRankedImageKeywords", "url", parameters); + } + async public Task ImageGetRankedImageKeywordsAsync(AlchemyAPI_RankedImageKeywords parameters) + { + return await GETAsync("ImageGetRankedImageKeywords", "image", parameters); + } + #endregion + + #region GetPostDo + async private Task GETAsync(string callName, string callPrefix, AlchemyAPI_BaseParams parameters) + { + StringBuilder uri = new StringBuilder(); + uri.Append(_requestUri).Append(callPrefix).Append("/").Append(callName); + uri.Append("?apikey=").Append(_apiKey).Append(parameters.getParameterString()); + + parameters.resetBaseParams(); + + Uri address = new Uri(uri.ToString()); + //In progress VVVVVV + using (HttpClient hClient = new HttpClient()) + { + hClient.Timeout = System.TimeSpan.FromMilliseconds(8000); + StringBuilder d = new StringBuilder(); + d.Append("apikey=").Append(_apiKey).Append(parameters.getParameterString()); + var s = new StringContent(d.ToString()); + + using (HttpResponseMessage results = await hClient.GetAsync(address).ConfigureAwait(false)) + { + String ret = await DoRequestAsync(results, parameters.getOutputMode()); + return ret; + } + } + //In progress ^^^^^^ + /* + HttpWebRequest wreq = WebRequest.Create(address) as HttpWebRequest; + wreq.Proxy = null; + + byte[] postData = parameters.GetPostData(); + + if (postData == null) + { + wreq.Method = "GET"; + } + else + { + wreq.Method = "POST"; + using (var ps = wreq.GetRequestStream()) + { + ps.Write(postData, 0, postData.Length); + } + } + return DoRequest(wreq, parameters.getOutputMode());*/ + } + + private async Task POSTAsync(string callName, string callPrefix, AlchemyAPI_BaseParams parameters) + { + Uri address = new Uri(_requestUri + callPrefix + "/" + callName); + + using (HttpClient hClient = new HttpClient()) + { + hClient.Timeout = System.TimeSpan.FromMilliseconds(8000); + StringBuilder d = new StringBuilder(); + d.Append("apikey=").Append(_apiKey).Append(parameters.getParameterString()); + var s = new StringContent(d.ToString()); + + using (HttpResponseMessage results = await hClient.PostAsync(address, s).ConfigureAwait(false)) + { + String ret = await DoRequestAsync(results, parameters.getOutputMode()); + return ret; + } + } + } + + /// + /// + /// + /// + /// + /// XML + async private Task DoRequestAsync(HttpResponseMessage response, AlchemyAPI_BaseParams.OutputMode outputMode) + { + string xml; + using (StreamReader r = new StreamReader(await response.Content.ReadAsStreamAsync())) + { + xml = await r.ReadToEndAsync(); + } + if (string.IsNullOrEmpty(xml)) + throw new XmlException("The API request returned back an empty response. Please verify that the url is correct."); + + XmlDocument xmlDoc = new XmlDocument(); + xmlDoc.LoadXml(xml); + + XmlElement root = xmlDoc.DocumentElement; + + if (AlchemyAPI_BaseParams.OutputMode.XML == outputMode) + { + XmlNode status = root.SelectSingleNode("/results/status"); + + if (status.InnerText != "OK") + { + string errorMessage = "Error making API call."; + + try + { + XmlNode statusInfo = root.SelectSingleNode("/results/statusInfo"); + errorMessage = statusInfo.InnerText; + } + catch + { + errorMessage = "An error occurred: Unable to access XmlNode /results/statusInfo"; + } + System.ApplicationException ex = new System.ApplicationException(errorMessage); + + throw ex; + } + } + else if (AlchemyAPI_BaseParams.OutputMode.RDF == outputMode) + { + XmlNamespaceManager nm = new XmlNamespaceManager(xmlDoc.NameTable); + nm.AddNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + nm.AddNamespace("aapi", "http://rdf.alchemyapi.com/rdf/v1/s/aapi-schema#"); + XmlNode status = root.SelectSingleNode("/rdf:RDF/rdf:Description/aapi:ResultStatus", nm); + + if (status.InnerText != "OK") + { + string errorMessage = "Error making API call."; + + try + { + XmlNode statusInfo = root.SelectSingleNode("/results/statusInfo"); + errorMessage = statusInfo.InnerText; + } + catch + { + errorMessage = "An error occurred: Unable to access XmlNode /results/statusInfo"; + } + System.ApplicationException ex = new System.ApplicationException(errorMessage); + + throw ex; + } + } + return xml; + } + #endregion + + } +} diff --git a/module/sdk/sdk.csproj b/module/sdk/sdk.csproj index 52348cb..99db3ae 100644 --- a/module/sdk/sdk.csproj +++ b/module/sdk/sdk.csproj @@ -1,4 +1,4 @@ - + Debug @@ -9,6 +9,8 @@ Library sdk sdk + v4.5 + true @@ -19,6 +21,7 @@ prompt 4 false + false none @@ -27,15 +30,22 @@ prompt 4 false + false + + + + + AlchemyAPI.cs + AlchemyAPI.cs diff --git a/monoDevelop/AlchemyAPI_CS_SDK/AlchemyAPI_CS_SDK.sln b/monoDevelop/AlchemyAPI_CS_SDK/AlchemyAPI_CS_SDK.sln index 8990ed3..941527c 100644 --- a/monoDevelop/AlchemyAPI_CS_SDK/AlchemyAPI_CS_SDK.sln +++ b/monoDevelop/AlchemyAPI_CS_SDK/AlchemyAPI_CS_SDK.sln @@ -1,76 +1,81 @@ - -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sdk", "..\..\module\sdk\sdk.csproj", "{799E354F-E323-408C-9733-7F1896342663}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{7D111981-ED8F-4E06-B135-A72B4278C6D4}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "author", "..\..\example\author\author.csproj", "{E61EB529-E86A-4273-940F-FA4A99378E92}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "combined", "..\..\example\combined\combined.csproj", "{DC4C1286-3B69-4826-AA74-AAC87B34E4ED}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "taxonomy", "..\..\example\taxonomy\taxonomy.csproj", "{FD0F315B-C5F0-442F-B86C-E02886749B92}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "image", "..\..\example\image\image.csproj", "{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ranked_image_keywords", "..\..\example\ranked_image_keywords\ranked_image_keywords.csproj", "{181E3CCB-A081-4CA5-963D-CA41B9886683}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {181E3CCB-A081-4CA5-963D-CA41B9886683}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {181E3CCB-A081-4CA5-963D-CA41B9886683}.Debug|Any CPU.Build.0 = Debug|Any CPU - {181E3CCB-A081-4CA5-963D-CA41B9886683}.Release|Any CPU.ActiveCfg = Release|Any CPU - {181E3CCB-A081-4CA5-963D-CA41B9886683}.Release|Any CPU.Build.0 = Release|Any CPU - {799E354F-E323-408C-9733-7F1896342663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {799E354F-E323-408C-9733-7F1896342663}.Debug|Any CPU.Build.0 = Debug|Any CPU - {799E354F-E323-408C-9733-7F1896342663}.Release|Any CPU.ActiveCfg = Release|Any CPU - {799E354F-E323-408C-9733-7F1896342663}.Release|Any CPU.Build.0 = Release|Any CPU - {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Release|Any CPU.Build.0 = Release|Any CPU - {DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Debug|Any CPU.Build.0 = Debug|Any CPU - {DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Release|Any CPU.ActiveCfg = Release|Any CPU - {DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Release|Any CPU.Build.0 = Release|Any CPU - {E61EB529-E86A-4273-940F-FA4A99378E92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E61EB529-E86A-4273-940F-FA4A99378E92}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E61EB529-E86A-4273-940F-FA4A99378E92}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E61EB529-E86A-4273-940F-FA4A99378E92}.Release|Any CPU.Build.0 = Release|Any CPU - {FD0F315B-C5F0-442F-B86C-E02886749B92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {FD0F315B-C5F0-442F-B86C-E02886749B92}.Debug|Any CPU.Build.0 = Debug|Any CPU - {FD0F315B-C5F0-442F-B86C-E02886749B92}.Release|Any CPU.ActiveCfg = Release|Any CPU - {FD0F315B-C5F0-442F-B86C-E02886749B92}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(NestedProjects) = preSolution - {E61EB529-E86A-4273-940F-FA4A99378E92} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} - {DC4C1286-3B69-4826-AA74-AAC87B34E4ED} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} - {FD0F315B-C5F0-442F-B86C-E02886749B92} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} - {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} - {181E3CCB-A081-4CA5-963D-CA41B9886683} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} - EndGlobalSection - GlobalSection(MonoDevelopProperties) = preSolution - StartupItem = ..\..\example\ranked_image_keywords\ranked_image_keywords.csproj - Policies = $0 - $0.TextStylePolicy = $1 - $1.FileWidth = 120 - $1.inheritsSet = VisualStudio - $1.inheritsScope = text/plain - $1.scope = text/x-csharp - $0.CSharpFormattingPolicy = $2 - $2.StatementBraceStyle = NextLine - $2.ElseNewLinePlacement = NewLine - $2.CatchNewLinePlacement = NewLine - $2.FinallyNewLinePlacement = NewLine - $2.BeforeMethodDeclarationParentheses = False - $2.BeforeMethodCallParentheses = False - $2.BeforeConstructorDeclarationParentheses = False - $2.inheritsSet = Mono - $2.inheritsScope = text/x-csharp - $2.scope = text/x-csharp - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 2013 +VisualStudioVersion = 12.0.31101.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "sdk", "..\..\module\sdk\sdk.csproj", "{799E354F-E323-408C-9733-7F1896342663}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Examples", "Examples", "{7D111981-ED8F-4E06-B135-A72B4278C6D4}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "author", "..\..\example\author\author.csproj", "{E61EB529-E86A-4273-940F-FA4A99378E92}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "combined", "..\..\example\combined\combined.csproj", "{DC4C1286-3B69-4826-AA74-AAC87B34E4ED}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "taxonomy", "..\..\example\taxonomy\taxonomy.csproj", "{FD0F315B-C5F0-442F-B86C-E02886749B92}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "image", "..\..\example\image\image.csproj", "{B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ranked_image_keywords", "..\..\example\ranked_image_keywords\ranked_image_keywords.csproj", "{181E3CCB-A081-4CA5-963D-CA41B9886683}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {799E354F-E323-408C-9733-7F1896342663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {799E354F-E323-408C-9733-7F1896342663}.Debug|Any CPU.Build.0 = Debug|Any CPU + {799E354F-E323-408C-9733-7F1896342663}.Release|Any CPU.ActiveCfg = Release|Any CPU + {799E354F-E323-408C-9733-7F1896342663}.Release|Any CPU.Build.0 = Release|Any CPU + {E61EB529-E86A-4273-940F-FA4A99378E92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E61EB529-E86A-4273-940F-FA4A99378E92}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E61EB529-E86A-4273-940F-FA4A99378E92}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E61EB529-E86A-4273-940F-FA4A99378E92}.Release|Any CPU.Build.0 = Release|Any CPU + {DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC4C1286-3B69-4826-AA74-AAC87B34E4ED}.Release|Any CPU.Build.0 = Release|Any CPU + {FD0F315B-C5F0-442F-B86C-E02886749B92}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FD0F315B-C5F0-442F-B86C-E02886749B92}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FD0F315B-C5F0-442F-B86C-E02886749B92}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FD0F315B-C5F0-442F-B86C-E02886749B92}.Release|Any CPU.Build.0 = Release|Any CPU + {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A}.Release|Any CPU.Build.0 = Release|Any CPU + {181E3CCB-A081-4CA5-963D-CA41B9886683}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {181E3CCB-A081-4CA5-963D-CA41B9886683}.Debug|Any CPU.Build.0 = Debug|Any CPU + {181E3CCB-A081-4CA5-963D-CA41B9886683}.Release|Any CPU.ActiveCfg = Release|Any CPU + {181E3CCB-A081-4CA5-963D-CA41B9886683}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {E61EB529-E86A-4273-940F-FA4A99378E92} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} + {DC4C1286-3B69-4826-AA74-AAC87B34E4ED} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} + {FD0F315B-C5F0-442F-B86C-E02886749B92} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} + {B117A1D0-AAAD-4B03-8B20-A8AEDC2EE01A} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} + {181E3CCB-A081-4CA5-963D-CA41B9886683} = {7D111981-ED8F-4E06-B135-A72B4278C6D4} + EndGlobalSection + GlobalSection(MonoDevelopProperties) = preSolution + StartupItem = ..\..\example\ranked_image_keywords\ranked_image_keywords.csproj + Policies = $0 + $0.TextStylePolicy = $1 + $1.FileWidth = 120 + $1.inheritsSet = VisualStudio + $1.inheritsScope = text/plain + $1.scope = text/x-csharp + $0.CSharpFormattingPolicy = $2 + $2.StatementBraceStyle = NextLine + $2.ElseNewLinePlacement = NewLine + $2.CatchNewLinePlacement = NewLine + $2.FinallyNewLinePlacement = NewLine + $2.BeforeMethodDeclarationParentheses = False + $2.BeforeMethodCallParentheses = False + $2.BeforeConstructorDeclarationParentheses = False + $2.inheritsSet = Mono + $2.inheritsScope = text/x-csharp + $2.scope = text/x-csharp + EndGlobalSection +EndGlobal