示例#1
0
		public RESTResult (byte[] bytearray)
		{
			ReturnedData = null;
			Error = null;
			Sucessful = true;
			ByteArray = bytearray;
		}
示例#2
0
		public RESTResult (bool sucessful){
			Sucessful = sucessful;
			ReturnedData = null;
			Error = null;
		}
示例#3
0
		public RESTResult (JToken returnedData)
		{
			ReturnedData = returnedData;
			Error = null;
			Sucessful = true;
		}
示例#4
0
		public RESTResult (RESTError error){
			Error = error;
			ReturnedData = null;
			Sucessful = false;
		}
示例#5
0
		public static async Task<RESTResult> Async (string endpoint, Dictionary<string, object> postObject, List<string> appendString)
		{
			Endpoint Endpoint = new EndpointManager().GetEndpointByName(endpoint);
			if (Endpoint == null) {
				System.Diagnostics.Debug.WriteLine ("Endpoint not in the database: " + endpoint);
				throw new RESTError (endpoint, "UNKNOWN", postObject, null); 
			}

			if (appendString != null) {
				StringBuilder stringBuilder = new StringBuilder ();
				foreach (string str in appendString) {
					stringBuilder.Append ("/");
					stringBuilder.Append (str);
				}
				Endpoint.URL = Endpoint.URL + stringBuilder.ToString ();
			}

			System.Diagnostics.Debug.WriteLine (Endpoint.URL);
			HttpResponseMessage contentsTask = null;
			try
			{
				if (Endpoint.Method == "POST")
				{
					contentsTask = await RESTHelper.Post(Endpoint.URL, postObject, (endpoint != "register"));
				}
				else if (Endpoint.Method == "PUT")
				{
					if (endpoint == "addcomment" || endpoint == "updateprofile" || endpoint == "updatepassword")
					{
						contentsTask = await RESTHelper.Put(Endpoint.URL, postObject);
					}
					else {
						contentsTask = await RESTHelper.Get(Endpoint.URL, postObject, true);
					}
				}
				else if (Endpoint.Method == "GET")
				{
					contentsTask = await RESTHelper.Get(Endpoint.URL, postObject, false);
				}
				else if (Endpoint.Method == "DELETE")
				{
					contentsTask = await RESTHelper.Delete(Endpoint.URL, postObject);
				}
				else {
					throw new Exception();
				}

				if (contentsTask.IsSuccessStatusCode)
				{
					if (endpoint == "getpostimage" || endpoint == "getuserimage")
					{
						return new RESTResult(contentsTask.Content.ReadAsByteArrayAsync().Result);
					}
					else {
						return new RESTResult(Newtonsoft.Json.Linq.JObject.Parse(contentsTask.Content.ReadAsStringAsync().Result));
					}
				}
				else if (contentsTask.StatusCode == System.Net.HttpStatusCode.Unauthorized)
				{
					throw new RESTError(endpoint, Endpoint.Method, postObject, null);
				}
				else {
					string test = contentsTask.Content.ReadAsStringAsync().Result;
					JToken results = Newtonsoft.Json.Linq.JObject.Parse(test);
					RESTError error = new RESTError((int)contentsTask.StatusCode, results, endpoint, "POST", postObject);

					return new RESTResult(error);

				}

			}
			catch (RESTError e)
			{
				if (contentsTask != null && contentsTask.StatusCode == HttpStatusCode.Unauthorized)
				{
					e.StatusCode = (int)HttpStatusCode.Unauthorized;
				}
				throw e;
			}
			catch (JsonReaderException)
			{
				System.Diagnostics.Debug.WriteLine("Json parsing issue");
				throw new RESTError("There was a problem try again - Invalid User or Email");
			}
			catch (Exception e)
			{
				throw new RESTError(e);
			}
		}