From 5751b82f023e073f97138454e68a20dfd844e4e2 Mon Sep 17 00:00:00 2001 From: pranabyte <519296+pranabyte@users.noreply.github.com> Date: Tue, 15 Jul 2014 10:45:23 -0600 Subject: [PATCH 1/8] added reference for System.Drawing.dll, also returning more descriptive error messages from API --- example/build.sh | 2 +- module/AlchemyAPI.cs | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/example/build.sh b/example/build.sh index cc9f672..2a1c9a8 100755 --- a/example/build.sh +++ b/example/build.sh @@ -32,5 +32,5 @@ dmcs taxonomy.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dl dmcs image.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll -dmcs ranked_image_keywords.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll +dmcs ranked_image_keywords.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll -r:System.Drawing.dll diff --git a/module/AlchemyAPI.cs b/module/AlchemyAPI.cs index 60144dc..4b7a25d 100644 --- a/module/AlchemyAPI.cs +++ b/module/AlchemyAPI.cs @@ -955,8 +955,18 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o if (status.InnerText != "OK") { - System.ApplicationException ex = - new System.ApplicationException ("Error making API call."); + string errorMessage = "Error making API call."; + + try + { + XmlNode statusInfo = root.SelectSingleNode("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/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; } @@ -970,8 +980,18 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o if (status.InnerText != "OK") { - System.ApplicationException ex = - new System.ApplicationException ("Error making API call."); + string errorMessage = "Error making API call."; + + try + { + XmlNode statusInfo = root.SelectSingleNode("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/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; } From 88a898c27fa2973b2a06fa3bf3dee2200d75de0d Mon Sep 17 00:00:00 2001 From: pranabyte <519296+pranabyte@users.noreply.github.com> Date: Fri, 25 Jul 2014 10:40:51 -0600 Subject: [PATCH 2/8] Updated README.md to make the first steps more clear. --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4f65104..dbb01da 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +Note: THIS IS NOT PRODUCTON CODE. This SDK is example code to get you started making programmatic calls to AlchemyAPI. + ================================ AlchemyAPI .NET SDK: version 0.10 ================================ @@ -12,10 +14,10 @@ MONO INSTALLATION -To compile this module, enter the "module" directory and run the "build.sh" +To compile this modul eand generate the AlchemyAPI.dll file, enter the "module" directory and run the "build.sh" script. -To install this module, copy the AlchemyAPI.dll file into your desired +To install this module, copy the AlchemyAPI.dll file created from the previous step into your desired .NET import directory, or set your MONO_PATH environment variable to point to the directory containing the compiled AlchemyAPI.dll file. @@ -70,10 +72,10 @@ Windows .NET INSTALLATION -To compile this module, enter the "module" directory and run the "build" +To compile this module and generate the AlchemyAPI.dll file, enter the "module" directory and run the "build" script. -To install this module, copy the AlchemyAPI.dll file into your desired +To install this module, copy the AlchemyAPI.dll file generated from the previous step into your desired .NET import directory. For purposes of running examples, the dll is copied into the example folder. From 0585f601d2395de01daa534c2d5b04eb84e4a2a6 Mon Sep 17 00:00:00 2001 From: pranabyte <519296+pranabyte@users.noreply.github.com> Date: Fri, 25 Jul 2014 10:43:55 -0600 Subject: [PATCH 3/8] adding blank api_key.txt to example folder --- example/api_key.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 example/api_key.txt diff --git a/example/api_key.txt b/example/api_key.txt new file mode 100644 index 0000000..e69de29 From 9cef003e7f3f1029c0c5f949f9813d442c48efe4 Mon Sep 17 00:00:00 2001 From: pranabyte <519296+pranabyte@users.noreply.github.com> Date: Fri, 25 Jul 2014 10:48:25 -0600 Subject: [PATCH 4/8] fixed spelling mistake in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dbb01da..fb6ccfb 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ MONO INSTALLATION -To compile this modul eand generate the AlchemyAPI.dll file, enter the "module" directory and run the "build.sh" +To compile this module and generate the AlchemyAPI.dll file, enter the "module" directory and run the "build.sh" script. To install this module, copy the AlchemyAPI.dll file created from the previous step into your desired From dde29d0a44dba26b1e22e458311084ae76758bfb Mon Sep 17 00:00:00 2001 From: pranabyte <519296+pranabyte@users.noreply.github.com> Date: Fri, 25 Jul 2014 11:18:19 -0600 Subject: [PATCH 5/8] changed Orchestr8 to AlchemyAPI in README.md removed deprecated GlobalProxySelection object from AlchemyAPI.cs class. --- README.md | 4 ++-- module/AlchemyAPI.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index fb6ccfb..adc6fb6 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ and other tasks. All code samples are within the "example" directory. To run these code samples you must first edit the (example/api_key.txt) file, -adding your assigned Orchestr8 API key. +adding your assigned AlchemyAPI API key. Compiling Code Samples: @@ -88,7 +88,7 @@ and other tasks. All code samples are within the "example" directory. To run these code samples you must first edit the (example/api_key.txt) file, -adding your assigned Orchestr8 API key. +adding your assigned AlchemyAPI API key. Compiling Code Samples: diff --git a/module/AlchemyAPI.cs b/module/AlchemyAPI.cs index 4b7a25d..a936ec6 100644 --- a/module/AlchemyAPI.cs +++ b/module/AlchemyAPI.cs @@ -888,7 +888,7 @@ private string GET(string callName, string callPrefix, AlchemyAPI_BaseParams par Uri address = new Uri (uri.ToString()); HttpWebRequest wreq = WebRequest.Create(address) as HttpWebRequest; - wreq.Proxy = GlobalProxySelection.GetEmptyWebProxy(); + wreq.Proxy = null; byte[] postData = parameters.GetPostData(); @@ -913,7 +913,7 @@ private string POST(string callName, string callPrefix, AlchemyAPI_BaseParams pa Uri address = new Uri (_requestUri + callPrefix + "/" + callName); HttpWebRequest wreq = WebRequest.Create(address) as HttpWebRequest; - wreq.Proxy = GlobalProxySelection.GetEmptyWebProxy(); + wreq.Proxy = null; wreq.Method = "POST"; wreq.ContentType = "application/x-www-form-urlencoded"; From 5082e5b1626dd4bfe2232f0a1db3481d3dee3be3 Mon Sep 17 00:00:00 2001 From: pranabyte <519296+pranabyte@users.noreply.github.com> Date: Thu, 16 Oct 2014 10:34:49 -0600 Subject: [PATCH 6/8] Updated build script to build.bat script --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index adc6fb6..a503366 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ Windows .NET INSTALLATION -To compile this module and generate the AlchemyAPI.dll file, enter the "module" directory and run the "build" +To compile this module and generate the AlchemyAPI.dll file, enter the "module" directory and run the "build.bat" script. To install this module, copy the AlchemyAPI.dll file generated from the previous step into your desired @@ -92,7 +92,7 @@ adding your assigned AlchemyAPI API key. Compiling Code Samples: - 1. Run "build" (Compiles code using .NET) + 1. Run "build.bat" (Compiles code using .NET) Running Code Samples: From ce7346819260f4dc2c4eb9c4cae518f21efb5ca2 Mon Sep 17 00:00:00 2001 From: pranabyte <519296+pranabyte@users.noreply.github.com> Date: Thu, 16 Oct 2014 10:38:31 -0600 Subject: [PATCH 7/8] added reference to ranked_image_keywords.cs in example/build.bat and module/build.bat --- example/build.bat | 2 ++ module/build.bat | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/example/build.bat b/example/build.bat index abdb262..21bb492 100644 --- a/example/build.bat +++ b/example/build.bat @@ -30,3 +30,5 @@ csc combined.cs -r:System.Data.dll -r:System.Web.dll -r:AlchemyAPI.dll csc taxonomy.cs -r:System.Data.dll -r:System.Web.dll -r:AlchemyAPI.dll csc image.cs -r:System.Data.dll -r:System.Web.dll -r:AlchemyAPI.dll + +csc ranked_image_keywords.cs -r:System.Data.dll -r:System.Web.dll -r:AlchemyAPI.dll -r:System.Drawing.dll diff --git a/module/build.bat b/module/build.bat index 1a1767d..38a3eca 100644 --- a/module/build.bat +++ b/module/build.bat @@ -1,4 +1,4 @@ -csc /target:library AlchemyAPI.cs AlchemyAPI_BaseParams.cs AlchemyAPI_CategoryParams.cs AlchemyAPI_ConceptParams.cs AlchemyAPI_ConstraintQueryParams.cs AlchemyAPI_EntityParams.cs AlchemyAPI_KeywordParams.cs AlchemyAPI_LanguageParams.cs AlchemyAPI_TextParams.cs AlchemyAPI_RelationParams.cs AlchemyAPI_TargetedSentimentParams.cs AlchemyAPI_CombinedDataParams.cs AlchemyAPI_TaxonomyParams.cs AlchemyAPI_ImageParams.cs -r:System.Data.dll -r:System.Web.dll +csc /target:library AlchemyAPI.cs AlchemyAPI_BaseParams.cs AlchemyAPI_CategoryParams.cs AlchemyAPI_ConceptParams.cs AlchemyAPI_ConstraintQueryParams.cs AlchemyAPI_EntityParams.cs AlchemyAPI_KeywordParams.cs AlchemyAPI_LanguageParams.cs AlchemyAPI_TextParams.cs AlchemyAPI_RelationParams.cs AlchemyAPI_TargetedSentimentParams.cs AlchemyAPI_CombinedDataParams.cs AlchemyAPI_TaxonomyParams.cs AlchemyAPI_ImageParams.cs AlchemyAPI_RankedImageKeywords.cs -r:System.Data.dll -r:System.Web.dll -r:System.Drawing.dll copy "AlchemyAPI.dll" "..\example\" From e74b0bf4ba6cc3ddf77b652a8b5bfc75c4320698 Mon Sep 17 00:00:00 2001 From: pranabyte <519296+pranabyte@users.noreply.github.com> Date: Wed, 4 Feb 2015 10:09:04 -0700 Subject: [PATCH 8/8] Handle non-XML responses gracefully from API server and add example code to get keywords in JSON format. --- example/build.sh | 2 ++ example/keywords_json.cs | 43 ++++++++++++++++++++++++++++++++++++++++ module/AlchemyAPI.cs | 17 ++++++++++------ 3 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 example/keywords_json.cs diff --git a/example/build.sh b/example/build.sh index 2a1c9a8..ad13c71 100755 --- a/example/build.sh +++ b/example/build.sh @@ -6,6 +6,8 @@ dmcs concepts.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dl dmcs keywords.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll +dmcs keywords_json.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll + dmcs categories.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll dmcs feed_links.cs -r:System.Data.dll -r:System.Web.dll -r:../module/AlchemyAPI.dll diff --git a/example/keywords_json.cs b/example/keywords_json.cs new file mode 100644 index 0000000..aa9fd30 --- /dev/null +++ b/example/keywords_json.cs @@ -0,0 +1,43 @@ + +using System; +using System.IO; +using AlchemyAPI; + + +public class TestApp +{ + static public void Main () + { + // Create an AlchemyAPI object. + AlchemyAPI.AlchemyAPI alchemyObj = new AlchemyAPI.AlchemyAPI(); + AlchemyAPI_KeywordParams alchemyParams = new AlchemyAPI_KeywordParams(); + alchemyParams.setOutputMode(AlchemyAPI_BaseParams.OutputMode.JSON); + + + // Load an API key from disk. + alchemyObj.LoadAPIKey("api_key.txt"); + + + // Extract topic keywords for a web URL. + string response = alchemyObj.URLGetRankedKeywords("http://www.techcrunch.com/", alchemyParams); + Console.WriteLine (response); + + + // Extract topic keywords for a text string. + response = alchemyObj.TextGetRankedKeywords("Hello there, my name is Bob Jones. I live in the United States of America. Where do you live, Fred?", alchemyParams); + Console.WriteLine (response); + + + // Load a HTML document to analyze. + StreamReader streamReader = new StreamReader("data/example.html"); + string htmlDoc = streamReader.ReadToEnd(); + streamReader.Close(); + + + // Extract topic keywords for a HTML document. + response = alchemyObj.HTMLGetRankedKeywords(htmlDoc, "http://www.test.com/", alchemyParams); + Console.WriteLine (response); + } + +} + diff --git a/module/AlchemyAPI.cs b/module/AlchemyAPI.cs index a936ec6..5ca9a37 100644 --- a/module/AlchemyAPI.cs +++ b/module/AlchemyAPI.cs @@ -939,13 +939,18 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o { StreamReader r = new StreamReader (wres.GetResponseStream()); - string xml = r.ReadToEnd(); + string response = r.ReadToEnd(); + + if (string.IsNullOrEmpty(response)) + throw new Exception ("The API request returned back an empty response. Please verify that the url is correct."); + + if (AlchemyAPI_BaseParams.OutputMode.XML != outputMode && AlchemyAPI_BaseParams.OutputMode.RDF != outputMode) + { + return response; // don't parse response, just return as is + } - 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); + xmlDoc.LoadXml(response); XmlElement root = xmlDoc.DocumentElement; @@ -997,7 +1002,7 @@ private string DoRequest(HttpWebRequest wreq, AlchemyAPI_BaseParams.OutputMode o } } - return xml; + return response; } }