0

Hi I am writing a small task to learn ,querying stackoverflow api I want to achieve below ● Number of questions asked on that date ● Total number of views across all questions ● Distinct (i.e. no duplicates) comma separated list of the tags used across all questions for that date, ordered alphabetically.

I have done below api call but this is not I have expected.First thing I didn't find api end point to get no.of questions for date from stackexchange api . Any suggestions are much appreciated

 static void Main()
    {
      Console.WriteLine("Making stackoverflow API Call...");
        using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
        {
            client.BaseAddress = new Uri("https://api.stackexchange.com/docs");
           // Int32 unixTimestamp = (Int32) (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1,0,0,0,0))).TotalSeconds;
            var unixStamp = Helper.ConvertToDateTime("2017, 09, 23, 0, 0, 0");
            var response = client.GetAsync("questions?fromdate="+ unixStamp + "&todate="+ unixStamp + "&site=stackoverflow").Result;
            response.EnsureSuccessStatusCode();
            var s = response.Content.ReadAsStringAsync().Result;
            var result = response.Content.ReadAsStringAsync().Result.Count();
            Console.WriteLine("Total no.of questions asked: " + result);
        }
        Console.ReadLine();
    }
62071072SP
  • 1,963
  • 2
  • 19
  • 38

1 Answers1

1

First of all your base address should be https://api.stackexchange.com/2.2. To get the number of questions that were created on a single day you have to modify the default filer to include the total field. Running the following request:

/2.2/questions?fromdate=1506211200&todate=1506297600&order=desc&sort=creation&site=stackoverflow&filter=!)5IW-5QudQH7_nJ7d.eBuocQb(B)

will return:

{
    "items": [],
    "has_more": false,
    "quota_max": 10000,
    "quota_remaining": 9978,
    "total": 3666
}

For the other two requirements you should create a new filter that includes the desired fields (if you want to work with the returned questions you might want to inlcude more fields in the filter to avoid duplicate requests). For creating a see: filter/create and Custom Filters

After you get the result you have to parse the returned JSON string (if you want XML to be returned you have to parse the XML). You can either use Newtonsoft or the build in DataContractJsonSerializer (for more information see here).

Here is an example using the build in serializer:

using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
using System.IO;

[DataContract]
public class Question
{
    // Question members...
}

[DataContract]
public class TotalQuestionsResult
{
    [DataMember(Name = "items")]
    public Question[] Items { get; set; }
    [DataMember(Name = "has_more")]
    public bool HasMore { get; set; }
    [DataMember(Name = "quota_max")]
    public int QuotaMax { get; set; }
    [DataMember(Name = "quota_remaining")]
    public int QuotaRemaining { get; set; }
    [DataMember(Name = "total")]
    public int Total { get; set; }
}

public async Task<int> GetTotalNumberOfQuestions()
{
    // Get the data.
    using (var client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }))
    {
        client.BaseAddress = new Uri("https://api.stackexchange.com/2.2");
        var response = await client.GetAsync("questions?fromdate=1506211200&todate=1506297600&order=desc&sort=creation&site=stackoverflow&filter=!)5IW-5QudQH7_nJ7d.eBuocQb(B)");
        var responseStream = new MemoryStream(await response.Content.ReadAsByteArrayAsync());
        var serializer = new DataContractJsonSerializer(typeof(TotalQuestionsResult));
        var result = (TotalQuestionsResult)serializer.ReadObject(responseStream);

        return result.Total;
    }
}
Streamline
  • 952
  • 1
  • 15
  • 23