Last active
February 3, 2020 21:29
-
-
Save tgranqvist/47ccb78d126cbe4133d31eea4cbb351c to your computer and use it in GitHub Desktop.
A very basic API client for KoboToolbox API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Text.RegularExpressions; | |
| using System.Net; | |
| using System.Net.Http; | |
| using System.Net.Http.Headers; | |
| using System.Collections.Generic; | |
| namespace APITest | |
| { | |
| public class KoboClient | |
| { | |
| public KoboClient(string token) | |
| { | |
| httpClient = new HttpClient(); | |
| httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", token); | |
| } | |
| public string CloneForm(string templateUid, string name) | |
| { | |
| var data = new Dictionary<string, string>(){ | |
| {"clone_from", templateUid}, | |
| {"asset_type", "survey"}, | |
| {"name", name}, | |
| }; | |
| var op = ""; | |
| var response = httpClient.PostAsync(string.Format(BASE_URL, op), new FormUrlEncodedContent(data)).Result; | |
| if (response.StatusCode != HttpStatusCode.Created) { | |
| throw new InvalidOperationException($"Unable to clone form. Response was {response.StatusCode}"); | |
| } | |
| var match = LOCATION_REGEX.Match(response.Headers.Location.ToString()); | |
| if (!match.Success) | |
| { | |
| throw new InvalidOperationException($"Unable to parse new location from result. Header was {response.Headers.Location}"); | |
| } | |
| return match.Groups[1].Value; | |
| } | |
| public void DeployForm(string uid) | |
| { | |
| var data = new Dictionary<string, string>(){ | |
| {"active", "true"} | |
| }; | |
| var op = $"{uid}/deployment/"; | |
| var url = string.Format(BASE_URL, op); | |
| var response = httpClient.PostAsync(url, new FormUrlEncodedContent(data)).Result; | |
| if (response.StatusCode != HttpStatusCode.OK) | |
| { | |
| throw new InvalidOperationException($"Unable to deploy form. Response was {response}"); | |
| } | |
| } | |
| public void AddWebhook(string uid, string name, string endpoint) | |
| { | |
| var data = new Dictionary<string, string>(){ | |
| {"name", name}, | |
| {"active", "true"}, | |
| {"export_type", "json"}, | |
| {"endpoint", endpoint} | |
| }; | |
| var op = $"{uid}/hooks/"; | |
| var url = string.Format(BASE_URL, op); | |
| var response = httpClient.PostAsync(url, new FormUrlEncodedContent(data)).Result; | |
| if (response.StatusCode != HttpStatusCode.Created) | |
| { | |
| throw new InvalidOperationException($"Unable to add webhook. Response was {response}"); | |
| } | |
| } | |
| private readonly string token; | |
| private readonly HttpClient httpClient; | |
| private static readonly string BASE_URL = "https://kobo.humanitarianresponse.info/api/v2/assets/{0}?format=json"; | |
| private static readonly Regex LOCATION_REGEX = new Regex("assets/(.+)/"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace APITest | |
| { | |
| public class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| var apiClient = new KoboClient("REDACTED"); | |
| Console.WriteLine("Cloning form"); | |
| var uid = apiClient.CloneForm(BASE_FORM, "Test 3rd feb - API"); | |
| Console.WriteLine($"New form id is {uid}"); | |
| Console.WriteLine("Deploying form"); | |
| apiClient.DeployForm(uid); | |
| Console.WriteLine("Deployment done"); | |
| Console.WriteLine("Adding webhook"); | |
| apiClient.AddWebhook(uid); | |
| Console.WriteLine("Webhook okay"); | |
| } | |
| const string BASE_FORM = "REDACTED"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment