Skip to content

Instantly share code, notes, and snippets.

@paully21
Created August 21, 2014 10:47
Show Gist options
  • Save paully21/e57e4a27f018315b429e to your computer and use it in GitHub Desktop.
Save paully21/e57e4a27f018315b429e to your computer and use it in GitHub Desktop.
Search Discourse Topics Via API and C#
private List<DiscoursSearchResult> SearchForum(string term)
{
List<DiscoursSearchResult> results = new List<DiscoursSearchResult>();
if (string.IsNullOrEmpty(term))
return results;
string url = ConfigurationManager.AppSettings["DiscourseUrl"] + "/search.json?include_blurbs=true&type_filter=topic&api_key=" + ConfigurationManager.AppSettings["DiscourseAPIKey"] + "&api_username=" + ConfigurationManager.AppSettings["DiscourseUrl"] + "&term=" + term;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = WebRequestMethods.Http.Get;
req.Accept = "application/json";
try
{
using (var response = (HttpWebResponse)req.GetResponse())
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
string responseString = reader.ReadToEnd();
var jsonObj = JArray.Parse(responseString);
var r = (JArray)jsonObj[0]["results"];
foreach (var item in r)
{
results.Add(new DiscoursSearchResult()
{
Id = Convert.ToInt32((string)item["id"]),
Title = (string)item["title"],
Url = ConfigurationManager.AppSettings["DiscourseUrl"] + (string)item["url"],
Blurb = (string)item["blurb"]
});
}
}
}
}
catch (Exception error)
{
//not throwing ... no forum responses or bad request not logged on purpose
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment