internal BatchState(HttpWebRequest request, Batch batch) { Request = request; Batch = batch; Event = new ManualResetEvent(false); }
public void SendBatch(Batch batch) { Dict props = new Dict { { "batch id", batch.MessageId }, { "batch size", batch.batch.Count } }; try { // set the current request time batch.SentAt = DateTime.Now.ToString("o"); string json = JsonConvert.SerializeObject(batch); props["json size"] = json.Length; Uri uri = new Uri(_host + "/v1/import"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri); // basic auth: https://segment.io/docs/tracking-api/reference/#authentication request.Headers.Add("Authorization", BasicAuthHeader(batch.WriteKey, "")); request.Content = new StringContent(json, Encoding.UTF8, "application/json"); Logger.Info("Sending analytics request to Segment.io ..", props); var start = DateTime.Now; var response = _client.SendAsync(request).Result; var duration = DateTime.Now - start; props["success"] = response.IsSuccessStatusCode; props["duration (ms)"] = duration.TotalMilliseconds; if (response.IsSuccessStatusCode) { Succeed(batch); Logger.Info("Request successful", props); } else { string reason = string.Format("Status Code {0} ", response.StatusCode); reason += response.Content.ToString(); props["reason"] = reason; Logger.Error("Request failed", props); Fail(batch, new APIException("Unexpected Status Code", reason)); } } catch (System.Exception e) { props ["reason"] = e.Message; Logger.Error ("Request failed", props); Fail(batch, e); } }
public void MakeRequest(Batch batch) { Stopwatch watch = new Stopwatch(); try { Uri uri = new Uri(_client.Config.Host + "/v1/import"); // set the current request time batch.SentAt = DateTime.Now.ToString("o"); string json = JsonConvert.SerializeObject(batch, settings); HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri); // Basic Authentication // https://segment.io/docs/tracking-api/reference/#authentication request.Headers["Authorization"] = BasicAuthHeader(batch.WriteKey, ""); request.Timeout = (int)Timeout.TotalMilliseconds; request.ContentType = "application/json"; request.Method = "POST"; // do not use the expect 100-continue behavior request.ServicePoint.Expect100Continue = false; // buffer the data before sending, ok since we send all in one shot request.AllowWriteStreamBuffering = true; Logger.Info("Sending analytics request to Segment.io ..", new Dict { { "batch id", batch.MessageId }, { "json size", json.Length }, { "batch size", batch.batch.Count } }); watch.Start(); using (var requestStream = request.GetRequestStream()) { using (StreamWriter writer = new StreamWriter(requestStream)) { writer.Write(json); } } using (var response = (HttpWebResponse)request.GetResponse()) { watch.Stop(); if (response.StatusCode == HttpStatusCode.OK) { Succeed(batch, watch.ElapsedMilliseconds); } else { string responseStr = String.Format("Status Code {0}. ", response.StatusCode); responseStr += ReadResponse(response); Fail(batch, new APIException("Unexpected Status Code", responseStr), watch.ElapsedMilliseconds); } } } catch (WebException e) { watch.Stop(); Fail(batch, ParseException(e), watch.ElapsedMilliseconds); } catch (System.Exception e) { watch.Stop(); Fail(batch, e, watch.ElapsedMilliseconds); } }
private void Succeed(Batch batch, long duration) { foreach (BaseAction action in batch.batch) { _client.Statistics.Succeeded += 1; _client.RaiseSuccess(action); } Logger.Info("Segment.io request successful.", new Dict { { "batch id", batch.MessageId }, { "duration (ms)", duration } }); }
private void Fail(Batch batch, System.Exception e, long duration) { foreach (BaseAction action in batch.batch) { _client.Statistics.Failed += 1; _client.RaiseFailure(action, e); } Logger.Info("Segment.io request failed.", new Dict { { "batch id", batch.MessageId }, { "reason", e.Message }, { "duration (ms)", duration } }); }
private void Succeed(Batch batch) { foreach (BaseAction action in batch.batch) { if (Succeeded != null) { Succeeded (action); } } }
private void Fail(Batch batch, System.Exception e) { foreach (BaseAction action in batch.batch) { if (Failed != null) { Failed (action, e); } } }