using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
namespace DemoApiExample
{
///
/// Tutorial example of using the Newtonsoft.Json library to parse JSON resources
/// on the Doxpop Court Api.
/// For more information on Newtonsoft.Json see http://www.newtonsoft.com/json
///
/// This code depends on the Newtonsoft.Json package version 9.0.1.
/// You can install it using the NuGet package manager or via the Package
/// Manager console with "PM> Install-Package Newtonsoft.Json".
///
internal class JsonClient
{
private const string BaseUrl = "https://demo-api.doxpop.com";
// The format is "username:password"
private const string Credentials = "demo:demo";
///
/// Makes an HTTP GET request to the api and returns the response body
/// body.
///
/// The HTTP response body
private static string getPage(string uri)
{
var client = new HttpClient { BaseAddress = new Uri(BaseUrl) };
var acceptHeader = new MediaTypeWithQualityHeaderValue("application/json");
client.DefaultRequestHeaders.Accept.Add(acceptHeader);
var base64CredentialBytes = Encoding.UTF8.GetBytes(Credentials);
var base64encodedCredentials = Convert.ToBase64String(base64CredentialBytes);
client.DefaultRequestHeaders.Add("Authorization", "Basic " + base64encodedCredentials);
return client.GetStringAsync(uri).Result;
}
///
/// Program entry point. Fetches and prints some data from the API.
///
///
public static void Main(string[] args)
{
var jsonString = getPage("/actors_cases.json?fullname=rifkind,+david");
var results = JsonConvert.DeserializeObject>(jsonString);
foreach (var result in results)
{
Console.WriteLine(result.assignedCaseRole);
// Print some actor information
var actor = result.actor;
Console.WriteLine(actor.fullName);
Console.WriteLine(actor.birthDate.Date.ToString("yyyy-MM-dd"));
// Print some address information
foreach (var address in actor.addresses)
{
Console.WriteLine(address.line1);
Console.WriteLine(address.city);
Console.WriteLine(address.stateCode);
Console.WriteLine(address.postalCode);
}
// Print some case information
var courtCase = result.courtCase;
Console.WriteLine(courtCase.caption);
Console.WriteLine(courtCase.globalDispositionCode);
Console.WriteLine(courtCase.dispositionDate.Date.ToString("yyyy-MM-dd"));
// Use the charges URI and print the descriptions
Console.WriteLine(courtCase.chargesUri);
var chargesJsonString = getPage(courtCase.chargesUri + ".json");
var charges = JsonConvert.DeserializeObject>(chargesJsonString);
foreach (var charge in charges)
{
Console.WriteLine(charge.description);
}
}
}
///
/// Sample model of the search result information
///
private class ActorCase
{
[JsonProperty("case")]
public CourtCase courtCase;
[JsonProperty("actor")]
public Actor actor;
[JsonProperty("assigned_case_role")]
public string assignedCaseRole;
}
///
/// Sample model of the court case information inside a search result
///
private class CourtCase
{
[JsonProperty("case_caption")]
public string caption;
[JsonProperty("case_global_disposition_code")]
public string globalDispositionCode;
[JsonProperty("case_disposition_date")]
public DateTime dispositionDate;
[JsonProperty("charges_uri")]
public string chargesUri;
}
///
/// Sample model of the actor information inside a search result
///
private class Actor
{
[JsonProperty("actor_full_name")]
public string fullName;
[JsonProperty("actor_person_date_of_birth")]
public DateTime birthDate;
[JsonProperty("addresses")]
public List addresses;
}
///
/// Sample model of the address information for an actor
///
private class Address
{
[JsonProperty("address_line1")]
public string line1;
[JsonProperty("address_city")]
public string city;
[JsonProperty("address_state_province_code")]
public string stateCode;
[JsonProperty("address_postal_code")]
public string postalCode;
}
///
/// Sample model of charge information
///
private class Charge
{
[JsonProperty("charge_desc")]
public string description;
}
}
}