public static async Task <string> GetJArrayPaged(this HttpClient client, string endpoint, JArray array) { using (var response = await client.GetAsync(endpoint)) { if (response.IsSuccessStatusCode) { var text = await response.Content.ReadAsStringAsync(); var json = JObject.Parse(text); JArray newItems = (JArray)json.SelectToken("value"); array.AddRange(newItems); if (json["@odata.nextLink"] != null) { // Recursively get more return(await client.GetJArrayPaged(json["@odata.nextLink"].Value <string>(), array)); } else if (json["@odata.deltaLink"] != null) { return(json["@odata.deltaLink"].Value <string>()); } else { return(null); // ERROR } } else { return(null); // ERROR } } }
public bool SendJournalEvents(List <JObject> entries, bool ody) // protected against bad JSON { JArray message = new JArray(Header(ody)); message.AddRange(entries); System.Diagnostics.Debug.WriteLine("EDAstro send " + message.ToString(true)); var response = RequestPost(message.ToString(), "journal", handleException: true); return(response.StatusCode == System.Net.HttpStatusCode.OK); }
private static string SaveEtsyEntities(string restMethod, string saveFilePathTag) { const string baseUrl = "https://openapi.etsy.com/v2"; var appSecretsConfigFilePath = CloudStorage.GetDropBoxPath(@"MoneyAI\AppConfig\appSecrets.json"); var appSecretsConfigJson = File.ReadAllText(appSecretsConfigFilePath); var appSecretsConfig = JObject.Parse(appSecretsConfigJson); var accessTokens = appSecretsConfig["accessTokens"][0]; var restClient = new RestClient(baseUrl); int?offset = 0; var limit = 100; restClient.Authenticator = RestSharp.Authenticators.OAuth1Authenticator.ForProtectedResource( (string)appSecretsConfig["consumerKey"], (string)appSecretsConfig["consumerSecret"], (string)accessTokens["accessToken"], (string)accessTokens["accessTokenSecret"]); JArray transactions = new JArray(); do { var request = new RestRequest(); request.Resource = restMethod; request.Method = Method.GET; request.AddParameter("offset", offset); request.AddParameter("limit", limit); var response = restClient.Execute(request); if (response.StatusCode == HttpStatusCode.OK) { var responseJson = JObject.Parse(response.Content); transactions.AddRange(responseJson["results"]); offset = (int?)responseJson["pagination"]["next_offset"]; } else { throw new Exception("Recieved Response {0}-{1}, Body {2} for request {3}, Error: {4}".FormatEx(response.StatusCode, response.StatusDescription, response.Content, response.ResponseUri, response.ErrorMessage)); } } while (offset > 0); var savePath = CloudStorage.GetDropBoxPath(@"MoneyAI\Statements\Etsy-Buyer\{0}-{1}-raw-{2}.json" .FormatEx((string)accessTokens["userLogin"], saveFilePathTag, DateTime.Now.ToString("yyyyMMdd"))); File.WriteAllText(savePath, transactions.ToString()); return(savePath); }