Beispiel #1
0
		public Review AddChangeToReview1_1(int ReviewId, int ChangeId)
		{
			string RequestUrl = string.Format(AddChangeRequest1_1, SwarmUri, ReviewId);
			string RequestContent = string.Format(AddChangeContent, ChangeId);

			JSONParser.JSONDoc jDoc = HttpPost(RequestUrl, RequestContent);

			return new Review(jDoc);
		}
Beispiel #2
0
		public ReviewList GetReviews(Options options)
		{
			ReviewList val = null;

			string RequestUrl = string.Format(ReviewSearchRequest, SwarmUri, options.ToString());
			JSONParser.JSONDoc jDoc = HttpGet(RequestUrl);
			val = new ReviewList(jDoc);

			return val;
		}
Beispiel #3
0
		public ReviewList GetReviews()
		{
			ReviewList val = null;

			string RequestUrl = string.Format(ReviewSearchRequest, SwarmUri, string.Empty);
			JSONParser.JSONDoc jDoc = HttpGet(RequestUrl);
			val = new ReviewList(jDoc);

			return val;
		}
Beispiel #4
0
        public Review CreateReview7(int ChangeId, Options options)
        {
            string RequestUrl = string.Format(CreateReviewRequest7, SwarmUri);

            string RequestContent = string.Format(CreateReviewContent, ChangeId);
            if ((options != null) && (options.Count > 0))
            {
                RequestContent += "&" + options.ToString();
            }
            JSONParser.JSONDoc jDoc = HttpPost(RequestUrl, RequestContent);

            return new Review(jDoc);
        }
Beispiel #5
0
		public Review GetReview(int reviewId)
		{
			Review val = null;

			string RequestUrl = string.Format(ReviewDetailsRequest, SwarmUri, reviewId);
#if use_DataContractJsonSerializer
				//MessageBox.Show(swarmUri + "\r\n\r\n" + ReqData);

				HttpWebRequest req = (HttpWebRequest) HttpWebRequest.Create(RequestUrl);
				req.ContentType = "application/x-www-form-urlencoded";
				req.Credentials = credentials;
				req.Method = "GET";

				WebResponse res = req.GetResponse();
				if (req.HaveResponse && (res.GetResponseStream() != null))
				{
					/* 
					 * Method 1, use a DataContractJsonSerializer to convert the JSON
					 * to a c# object
					 * 
					 * Advantages: Class Library, and easy to code
					 * Downsides: The C# objects are somewhat simplistic and rigidly tied to the JSON
					 */

			try
			{
					DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Review));
					//while ((val = (SwarmVersion)jsonSerializer.ReadObject(res.GetResponseStream())) != null)
					//{
					//    ;
					//}
					val = (Review)jsonSerializer.ReadObject(res.GetResponseStream());
					if (val == null)
					{
						res.GetResponseStream().Seek(0, System.IO.SeekOrigin.Begin);
						byte[] buff = new byte[res.ContentLength];
						int cnt = 0;
						while (cnt < (int)res.ContentLength)
						{
							int bytesLeft = (int)res.ContentLength - cnt;
							cnt += res.GetResponseStream().Read(buff, cnt, bytesLeft);
						}
						//res.GetResponseStream().Position = 0;

						string s = Encoding.UTF8.GetString(buff);
						throw new SwarmException(s);
					}
#endif

			/* 
					 * Method 2, use a JsonReaderWriterFactory to convert the JSON to an XML Doc
					 * to a c# object
					 * 
					 * Advantages: Class Library, XML well supported in C#
					 * Downsides: Still have to walk the tree to convert to a C# object.
					 */

#if use_JsonReaderWriterFactory
					XmlDictionaryReader jrw = JsonReaderWriterFactory.CreateJsonReader(res.GetResponseStream(), XmlDictionaryReaderQuotas.Max);
					XmlDocument xDoc = new XmlDocument();
					xDoc.Load(jrw);

					string xmlStr = xDoc.OuterXml;

					val = new Review(xDoc);
#endif
			/* 
					 * Method 2, use JSONParse to convert the JSON to an easy to interpret JSONdoc
					 * to a c# object
					 * 
					 * Advantages: Easy to convert JSon to c#
					 * Downsides: Internal library
					 */

#if true //use_JsonParser
#if debug_dump

					//res.GetResponseStream().Seek(0, System.IO.SeekOrigin.Begin);
					byte[] buff = new byte[res.ContentLength];
					int cnt = 0;
					while (cnt < (int)res.ContentLength)
					{
						int bytesLeft = (int)res.ContentLength - cnt;
						cnt += res.GetResponseStream().Read(buff, cnt, bytesLeft);
					}
					//res.GetResponseStream().Position = 0;

					string s = Encoding.UTF8.GetString(buff);
//					throw new SwarmException(s);

					res = req.GetResponse();

//					res.GetResponseStream().Seek(0, System.IO.SeekOrigin.Begin);
			}
			//catch (SwarmException)
			//{
			//    throw;
			//}
			catch (Exception ex)
			{
				logger.Trace("Exception in SwarmServer.Version: {0}", ex.Message));
				logger.Trace(ex.StackTrace);
			}
				}

#endif
			JSONParser.JSONDoc jDoc = HttpGet(RequestUrl);
			val = new Review(jDoc);
#endif
			return val;
		}
Beispiel #6
0
		public JSONDoc HttpPost(string RequestUrl, string RequestContent)
		{
			JSONDoc jDoc = null;
			HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(RequestUrl);

			SetBasicAuthHeader(req, credentials);

			req.ContentType = "application/x-www-form-urlencoded";
			req.Method = "POST";
			try
			{
				byte[] bytedata = Encoding.UTF8.GetBytes(RequestContent);
                req.ContentLength = bytedata.Length;
				req.GetRequestStream().Write(bytedata, 0, bytedata.Length);

				WebResponse res = req.GetResponse();

				if (req.HaveResponse && (res != null) && (res.GetResponseStream() != null))
				{
					if (res.ContentLength > 0)
					{
						// the easy way, we know how much we need to read
						byte[] buff;
						buff = new byte[res.ContentLength];
						int cnt = 0;
						while (cnt < (int)res.ContentLength)
						{
							int bytesLeft = (int)res.ContentLength - cnt;
							cnt += res.GetResponseStream().Read(buff, cnt, bytesLeft);
						}
						string str = Encoding.UTF8.GetString(buff);
						jDoc = new JSONParser.JSONDoc(str);
					}
					else
					{
						TextReader str = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
						jDoc = new JSONParser.JSONDoc(str);
					}
				}
				res.Close();

				return jDoc;
			}
			catch (WebException ex)
			{
				if ((ex.Response != null) && (ex.Response.GetResponseStream() != null))
				{
					if (ex.Response.ContentLength > 0)
					{
						// the easy way, we know how much we need to read
						byte[] buff;
						buff = new byte[ex.Response.ContentLength];
						int cnt = 0;
						while (cnt < (int)ex.Response.ContentLength)
						{
							int bytesLeft = (int)ex.Response.ContentLength - cnt;
							cnt += ex.Response.GetResponseStream().Read(buff, cnt, bytesLeft);
						}
						string str = Encoding.UTF8.GetString(buff);
						jDoc = new JSONParser.JSONDoc(str);
						throw new SwarmException(jDoc.ToString(), ex);
					}
					else
					{
						TextReader str = new StreamReader(ex.Response.GetResponseStream(), Encoding.UTF8);
						jDoc = new JSONParser.JSONDoc(str);
						throw new SwarmException(jDoc.ToString());
					}
				}
				logger.Trace("Exception in SwarmServer.HttpPost: {0}", ex.Message);
				logger.Trace(ex.StackTrace);

				throw new SwarmException(ex.Message);
			}
			catch (Exception ex)
			{
				logger.Trace("Exception in SwarmServer.HttpPost: {0}", ex.Message);
				logger.Trace(ex.StackTrace);

				throw;
			}
		}
Beispiel #7
0
		public JSONDoc HttpGet(string RequestUrl)
		{
			JSONDoc jDoc = null;

			try
			{
				HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(RequestUrl);
				SetBasicAuthHeader(req, credentials);
				req.ContentType = "application/x-www-form-urlencoded";
				req.Credentials = credentials;
				req.Method = "GET";

				WebResponse res = req.GetResponse();
				if (req.HaveResponse && (res.GetResponseStream() != null))
				{
					// Sometimes the stream reader 'stalls' when we try to tokenize while reading 
					//  the stream and only returns the partial result, so need to read the entire
					//  response stream before passing it to the JSON parser
					//TextReader str = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
					//jDoc = new JSONParser.JSONDoc(str);

					// read the entire response buffer into a string
					string str = string.Empty;
					if (res.ContentLength > 0)
					{
						// the easy way, we know how much we need to read
						byte[] buff;
						buff = new byte[res.ContentLength];
						int cnt = 0;
						while (cnt < (int)res.ContentLength)
						{
							int bytesLeft = (int)res.ContentLength - cnt;
							cnt += res.GetResponseStream().Read(buff, cnt, bytesLeft);
						}

						str = Encoding.UTF8.GetString(buff);
					}
					else
					{
						// we didn't get a length, so read the response in chunks and dynamically build the string
						// the easy way, we know how much we need to read
						byte[] buff = new byte[buffSize];

						StringBuilder sb = new StringBuilder(stringBuilderSize);
						int bytesRead = 1; // trick it first time through
						while (bytesRead > 0)
						{
							bytesRead = res.GetResponseStream().Read(buff, 0, buffSize);
							if (bytesRead > 0)
							{
								sb.Append(Encoding.UTF8.GetString(buff, 0, bytesRead));
							}
						}
						str = sb.ToString();
					}
					jDoc = new JSONParser.JSONDoc(str);
				}
				res.Close();

				return jDoc;
			}
			catch (WebException ex)
			{
				if ((ex.Response != null) && (ex.Response.GetResponseStream() != null))
				{
					if (ex.Response.ContentLength > 0)
					{
						// the easy way, we know how much we need to read
						byte[] buff;
						buff = new byte[ex.Response.ContentLength];
						int cnt = 0;
						while (cnt < (int)ex.Response.ContentLength)
						{
							int bytesLeft = (int)ex.Response.ContentLength - cnt;
							cnt += ex.Response.GetResponseStream().Read(buff, cnt, bytesLeft);
						}

						string str = Encoding.UTF8.GetString(buff);

						jDoc = new JSONParser.JSONDoc(str);

						throw new SwarmException(jDoc.ToString(), ex);
					}
					else
					{
						TextReader str = new StreamReader(ex.Response.GetResponseStream(), Encoding.UTF8);
						jDoc = new JSONParser.JSONDoc(str);

						throw new SwarmException(jDoc.ToString(), ex);
					}
				}
				logger.Trace("Exception in SwarmServer.HttpPost: {0}", ex.Message);
				logger.Trace(ex.StackTrace);

				throw new SwarmException(ex.Message, ex);
			}
			catch (Exception ex)
			{
				logger.Trace("Exception in SwarmServer.HttpPost: {0}", ex.Message);
				logger.Trace(ex.StackTrace);

				throw;
			}
		}