Exemple #1
0
        internal BatchState(HttpWebRequest request, Batch batch)
        {
            this.Request = request;
            this.Batch = batch;

			this.Event = new ManualResetEvent(false);
        }
 private void Fail(Batch batch, System.Exception e)
 {
     foreach (BaseAction action in batch.batch) {
         client.Statistics.Failed += 1;
         client.RaiseFailure(action, e);
     }
 }
		public void MakeRequest(Batch batch)
		{
			try
			{
				Uri uri = new Uri(_client.Options.Host + "/v1/import");
				
				string json = JsonConvert.SerializeObject(batch, settings);
				
				HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
				
				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;

				using (var requestStream = request.GetRequestStream())
				{
					using (StreamWriter writer = new StreamWriter(requestStream))
					{
						writer.Write(json);
					}
				}

				using (var response = (HttpWebResponse)request.GetResponse())
				{
					
					if (response.StatusCode == HttpStatusCode.OK)
					{
						Succeed(batch);
					}
					else
					{
						string responseStr = String.Format("Status Code {0}. ", response.StatusCode);
						
						responseStr += ReadResponse(response);
						
						Fail(batch, new APIException("Unexpected Status Code", responseStr));
					}
				}
			}
			catch (WebException e) 
			{
				Fail(batch, ParseException(e));
			}
			catch (System.Exception e)
			{
				Fail(batch, e);
			}
		}
        public void Poll()
        {
            if (www != null && www.isDone) {
                if (www.error != null) {
                    Debug.Log("WWW error: " + www.error);
                    Fail(currentBatch, new System.Exception(www.error));
                    // Retry
                    MakeRequest(currentBatch);

                } else {
            //					Debug.Log("Batch was succesfully sent"); // git:reject
                    Succeed(currentBatch);
                    www = null;
                    currentBatch = null;
                }
            }
        }
        public void MakeRequest(Batch batch)
        {
            try {
                Uri uri = new Uri(client.Options.Host + "/v1/import");

                string json = Json.Serialize(batch);

                if (client.Options.DebugLogJson) {
                    Debug.Log(json);
                }

                var encoding = new System.Text.UTF8Encoding();
                var data = encoding.GetBytes(json);
                var postHeaders = new Dictionary<string, string>();
                postHeaders.Add("Content-Type", "application/json");
                postHeaders.Add("Content-Length", data.Length.ToString());

                www = new WWW(uri.AbsoluteUri, data, postHeaders);
                currentBatch = batch;

            } catch (System.Exception e) {
                Fail(batch, e);
            }
        }
		private void Succeed(Batch batch) 
		{
			foreach (BaseAction action in batch.batch)
			{
				_client.Statistics.Succeeded += 1;
				_client.RaiseSuccess(action);
			}
		}