public static CheckApiErrorCode ( dynamic apiResponse ) : void | ||
apiResponse | dynamic | Deserialized json response from a Keen API call. |
return | void |
/// <summary> /// Get details of all schemas in the project. /// </summary> /// <returns></returns> public async Task <JArray> GetSchemas() { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", _prjSettings.MasterKey); client.DefaultRequestHeaders.Add("Keen-Sdk", KeenUtil.GetSdkVersion()); var responseMsg = await client.GetAsync(_serverUrl) .ConfigureAwait(continueOnCapturedContext: false); var responseString = await responseMsg.Content.ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); dynamic response = JArray.Parse(responseString); // error checking, throw an exception with information from the json // response if available, then check the HTTP response. KeenUtil.CheckApiErrorCode(response); if (!responseMsg.IsSuccessStatusCode) { throw new KeenException("GetSchemas failed with status: " + responseMsg.StatusCode); } return(response); } }
/// <summary> /// Get details of all schemas in the project. /// </summary> /// <returns></returns> public async Task <JArray> GetSchemas() { if (string.IsNullOrWhiteSpace(_readKey)) { throw new KeenException("An API ReadKey is required to get schemas."); } var responseMsg = await _keenHttpClient .GetAsync(_eventsRelativeUrl, _readKey) .ConfigureAwait(continueOnCapturedContext: false); var responseString = await responseMsg .Content .ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); var response = JArray.Parse(responseString); // error checking, throw an exception with information from the json // response if available, then check the HTTP response. KeenUtil.CheckApiErrorCode(response); if (!responseMsg.IsSuccessStatusCode) { throw new KeenException("GetSchemas failed with status: " + responseMsg.StatusCode); } return(response); }
public async Task AddEvent(string collection, JObject anEvent) { if (string.IsNullOrWhiteSpace(_writeKey)) { throw new KeenException("An API WriteKey is required to add events."); } var content = anEvent.ToString(); var responseMsg = await _keenHttpClient .PostAsync(GetCollectionUrl(collection), _writeKey, content) .ConfigureAwait(continueOnCapturedContext: false); var responseString = await responseMsg .Content .ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); JObject jsonResponse = null; try { // Normally the response content should be parsable JSON, // but if the server returned a 404 error page or something // like that, this will throw. jsonResponse = JObject.Parse(responseString); } catch (Exception) { } // error checking, throw an exception with information from the // json response if available, then check the HTTP response. KeenUtil.CheckApiErrorCode(jsonResponse); if (!responseMsg.IsSuccessStatusCode) { throw new KeenException("AddEvent failed with status: " + responseMsg.StatusCode); } }
public async System.Threading.Tasks.Task AddEvent(string collection, JObject anEvent) { var content = anEvent.ToString(); using (var client = new HttpClient()) using (var contentStream = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(content)))) { contentStream.Headers.Add("content-type", "application/json"); client.DefaultRequestHeaders.Add("Authorization", _prjSettings.WriteKey); client.DefaultRequestHeaders.Add("Keen-Sdk", KeenUtil.GetSdkVersion()); var httpResponse = await client.PostAsync(_serverUrl + collection, contentStream) .ConfigureAwait(continueOnCapturedContext: false); var responseString = await httpResponse.Content.ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); JObject jsonResponse = null; try { // Normally the response content should be parsable JSON, // but if the server returned a 404 error page or something // like that, this will throw. jsonResponse = JObject.Parse(responseString); } catch (Exception) { } // error checking, throw an exception with information from the // json response if available, then check the HTTP response. KeenUtil.CheckApiErrorCode(jsonResponse); if (!httpResponse.IsSuccessStatusCode) { throw new KeenException("AddEvent failed with status: " + httpResponse); } } }
public async Task <JObject> GetSchema(string collection) { // TODO : So much of this code, both in the constructor and in the actual message // dispatch, response parsing and error checking is copy/paste across Queries, Event // and EventCollection everywhere we use KeenHttpClient. We could shove some of that // into shared factory functionality (for the ctor stuff) and some of it into the // KeenHttpClient (for the dispatch/response portions). if (string.IsNullOrWhiteSpace(_readKey)) { throw new KeenException("An API ReadKey is required to get collection schema."); } var responseMsg = await _keenHttpClient .GetAsync(GetCollectionUrl(collection), _readKey) .ConfigureAwait(continueOnCapturedContext: false); var responseString = await responseMsg .Content .ReadAsStringAsync() .ConfigureAwait(continueOnCapturedContext: false); dynamic response = JObject.Parse(responseString); // error checking, throw an exception with information from the json // response if available, then check the HTTP response. KeenUtil.CheckApiErrorCode(response); if (!responseMsg.IsSuccessStatusCode) { throw new KeenException("GetSchema failed with status: " + responseMsg.StatusCode); } return(response); }