C# REST Sample Code
Use the following example classes to deserialize the JSON returned by the REST api.
Â
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
namespace com.doforms.rest.client
{
public class Blob
{
public string key { get; set; }
public string id { get; set; }
public string type { get; set; }
}
public class Column
{
public string name { get; set; }
public string caption { get; set; }
}
public class Choice
{
public string text { get; set; }
public Label label { get; set; }
}
public class Device
{
public string key { get; set; }
public string name { get; set; }
public string carrier { get; set; }
public DateTimeOffset createTime { get; set; }
public DateTimeOffset formTime { get; set; }
public string model { get; set; }
public string number { get; set; }
public string os { get; set; }
public string teamKey { get; set; }
public DateTimeOffset updateTime { get; set; }
public string version { get; set; }
}
public class Email
{
public List<string> addresses { get; set; }
public string message { get; set; }
}
public class Error
{
public string code { get; set; }
public string request { get; set; }
public string exception { get; set; }
public string message { get; set; }
}
public class Fax
{
public List<string> addresses { get; set; }
public string message { get; set; }
}
public class Field
{
public string name { get; set; }
[JsonProperty("data")]
[JsonConverter(typeof(StringEnumConverter))]
public DataType data { get; set; }
[JsonProperty("type")]
[JsonConverter(typeof(StringEnumConverter))]
public ControlType type { get; set; }
//Begin data fields
public Blob blob { get; set; }
public DateTimeOffset? date { get; set; } //Ignore the time value which will always be 00:00:00Z
public DateTimeOffset? datetime { get; set; }
public Email email { get; set; }
public Fax fax { get; set; }
public long? integer { get; set; }
public Location location { get; set; }
public double? number { get; set; }
public List<string> strings { get; set; }
public string text { get; set; }
public DateTimeOffset? time { get; set; } //Ignore date value which will always be Jan 1, 1970
//End data fields
public string value { get; set; } //Default value
public Options options { get; set; }
public List<Choice> choices { get; set; }
public List<Field> fields { get; set; }
public List<Row> rows { get; set; }
}
public class Form
{
public string key { get; set; }
public string id { get; set; }
public string name { get; set; }
public string displayName { get; set; }
public DateTimeOffset createTime { get; set; }
public string createUser { get; set; }
public DateTimeOffset updateTime { get; set; }
public string updateUser { get; set; }
public double version { get; set; }
public List<Field> fields { get; set; }
}
public class Label
{
public string en { get; set; }
}
public class Location
{
public double? latitude { get; set; }
public double? longitude { get; set; }
public double? altitude { get; set; }
public double? accuracy { get; set; }
}
public class Lookup
{
public string key { get; set; }
public string name { get; set; }
public string description { get; set; }
public bool useForm { get; set; }
public bool useFilter { get; set; }
public string formKey { get; set; }
public string projectKey { get; set; }
public bool update { get; set; }
public bool encrypt { get; set; }
public long version { get; set; }
public DateTimeOffset createTime { get; set; }
public string createUser { get; set; }
public DateTimeOffset updateTime { get; set; }
public string updateUser { get; set; }
public List<Column> columns { get; set; }
}
public class Options
{
public bool comment { get; set; }
public string format { get; set; }
public Label label { get; set; }
public int level { get; set; }
public string prefix { get; set; }
public long? rows { get; set; }
}
public class Project
{
public string key { get; set; }
public string name { get; set; }
}
public class Row
{
public long index { get; set; }
public List<Field> fields { get; set; }
}
public class Submission
{
public string key { get; set; }
public string id { get; set; }
public string name { get; set; }
public string formKey { get; set; }
public string formName { get; set; }
public double formVersion { get; set; }
public string projectKey { get; set; }
public int offset { get; set; }
public string timezone { get; set; }
public DateTimeOffset receiveTime { get; set; }
public List<Field> fields { get; set; }
}
public class Team
{
public string key { get; set; }
public string name { get; set; }
}
public class TokenRequest
{
public string username { get; set; }
public string password { get; set; }
public string account { get; set; }
public string code { get; set;}
public string webservice { get; set; }
}
public class TokenResult
{
public string token { get; set; }
public string code { get; set; }
public string[] accounts { get; set; }
public string message { get; set; }
}
public enum ControlType
{
action,
approval,
audio,
autonumber,
barcode,
bluetooth_le,
button,
button_grid,
calculation,
choose_one,
choose_multiple,
comment,
counter,
date_time,
email,
fax,
forward,
grid,
image,
instruction,
label,
location,
lookup,
nfc,
numeric,
page,
payment,
pod,
question,
questionnaire,
repeat,
retrieve,
schedule,
score,
score_summary,
signature,
sketch,
table,
text,
trends,
video
}
public enum DataType
{
blob,
date,
datetime,
email,
fax,
fields,
integer,
location,
number,
rows,
strings,
text,
time
}
}
Â
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
namespace com.doforms.rest.client
{
class Program
{
static void Main(string[] args)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://api.mydoforms.com/api/v2/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//Get authorization token
Console.WriteLine("Getting token");
TokenRequest tokenRequest = new TokenRequest();
tokenRequest.username = "myusername";
tokenRequest.password = "mypassword";
StringContent tokenContent = new StringContent(JsonConvert.SerializeObject(tokenRequest), Encoding.UTF8, "application/json");
HttpResponseMessage tokenResponse = client.PostAsync("tokens/user", tokenContent).Result;
//TODO: Check response code first for all requests
TokenResult tokenResult = JsonConvert.DeserializeObject<TokenResult>(tokenResponse.Content.ReadAsStringAsync().Result);
string token = tokenResult.token;
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
//Get the list of projects
Console.WriteLine("Getting projects");
HttpResponseMessage projectsResponse = client.GetAsync("projects").Result;
List<Project> projects = JsonConvert.DeserializeObject<List<Project>>(projectsResponse.Content.ReadAsStringAsync().Result);
foreach (Project project in projects)
{
Console.WriteLine("Project: " + project.name);
}
//Get the list of forms in the project
Console.WriteLine("Getting forms for project " + projects[0].name);
HttpResponseMessage formsResponse = client.GetAsync("projects/" + projects[0].key + "/forms").Result;
List<Form> forms = JsonConvert.DeserializeObject<List<Form>>(formsResponse.Content.ReadAsStringAsync().Result);
foreach(Form form in forms)
{
Console.WriteLine("Form: " + form.name);
}
//Get a form definition
Console.WriteLine("Getting definition for form " + forms[0].name);
HttpResponseMessage formResponse = client.GetAsync("forms/" + forms[0].key).Result;
Form definition = JsonConvert.DeserializeObject<Form>(formResponse.Content.ReadAsStringAsync().Result);
foreach(Field field in definition.fields)
{
Console.WriteLine("Field: " + field.name);
}
//Get the first submission in the form
Console.WriteLine("Getting submission " + definition.id + "&&1");
HttpResponseMessage submissionResponse = client.GetAsync("submissions/" + definition.id + "&&1").Result;
if (submissionResponse.StatusCode.Equals(HttpStatusCode.OK))
{
Submission submission = JsonConvert.DeserializeObject<Submission>(submissionResponse.Content.ReadAsStringAsync().Result);
Console.WriteLine("Submission: " + submission.key);
}
else
{
Error error = JsonConvert.DeserializeObject<Error>(submissionResponse.Content.ReadAsStringAsync().Result);
Console.WriteLine("Error message: " + error.message);
}
Console.ReadLine();
}
}
}