// Lets Execute the Request. public bool Execute() { try { // Here we will validate the run, and execute the query. // Lets validate if the Request is allowed to be run. bool canExecute = true; if (url == null || url.Trim().Length == 0) { // The URL is not provided. canExecute = false; } // Lets run the Request if Allowed. if (canExecute) { // Execute the Request. AddStatus("Executing"); // Setup the Waiter Manager to Insert into the database. WaiterManager.Begin(this); // Lets create another thread. AsyncStart(); return(true); } else { // There was an Error, and we cannot execute the request. return(false); } } catch (Exception e) { // There was an Error. return(false); } }
// Set the Callback of the User. public Waiter SetOnResponse(OnResponse onResponse) { this.onResponse = onResponse; // Lets update our status with the manager. WaiterManager.Update(this); return(this); }
public Waiter() { try { // Initialize the Required Variables. paramDatas = new List <ParamData>(); // Now we will communicate with the Waiter Manager to Clear all the Running Queries from the system. ID = WaiterManager.Init(this); // Add the Status. AddStatus("Initialized"); } catch (Exception e) { // There was an Error. } }
// Lets allow the user to set the params that he wants to send to the API. // We will also allow the user to add params one at a time. public Waiter AddParam(ParamData paramData) { try { // Lets try to add the params to the list. paramDatas.Add(paramData); } catch (Exception e) { // There was an Error. AddStatus("Param Err : " + e.Message); } // Lets update our status with the manager. WaiterManager.Update(this); return(this); }
// Set the Method to be used to make the request. public Waiter Method(CallMethod method) { try { // Lets set the Method. callMethod = method; AddStatus("Method Set"); } catch (Exception e) { // There was an Error. AddStatus("Method Not Set"); } // Lets update our status with the manager. WaiterManager.Update(this); return(this); }
// Lets set the Endpoint of the API. public Waiter Endpoint(string endpointT) { try { // Lets set the API. endpoint = endpointT; AddStatus("Endpoint Added"); // Lets update our status with the manager. WaiterManager.Update(this); return(this); } catch (Exception e) { // There was an Error. return(null); } }
// Lets set the URL of the API that needs to be called. public Waiter Url(string urlP) { try { // Lets set the API. url = urlP; // Add the Status. AddStatus("URL Added"); // Lets update our status with the manager. WaiterManager.Update(this); return(this); } catch (Exception e) { // There was an Error. return(null); } }
/* * * This class will make the Calls to the Server. * Only one instance of this Class will be Created. * The Waiter will call this class for Hitting the API. * */ private async Task Async(Waiter waiter) { try { // Lets create the Caller, and Call the API with the params. // Lets Get the Ready to call the API. HttpClient client = new HttpClient(); // Add the Base Address and the End Point client.BaseAddress = new Uri((callType.Equals(CallType.HTTPS) ? "https://" : "http://") + waiter.url + "/" + waiter.endpoint); // Lets Add the Params. StringBuilder paramBuild = new StringBuilder(); foreach (ParamData paramData in paramDatas) { if (paramBuild.Length == 0) { paramBuild.Append("?"); } paramBuild.Append(paramData.GetParam()) .Append("=") .Append(paramData.GetData()) .Append('&'); } HttpResponseMessage response = null; // Set the Method of the Request. switch (waiter.callMethod) { case CallMethod.GET: response = client.GetAsync(paramBuild.ToString()).Result; break; case CallMethod.POST: response = client.PostAsync(paramBuild.ToString(), null).Result; break; case CallMethod.PUT: response = client.PutAsync(paramBuild.ToString(), null).Result; break; case CallMethod.DELETE: response = client.DeleteAsync(paramBuild.ToString()).Result; break; } // Lets Execute the Request. if (response.IsSuccessStatusCode) { // Get the response string responseJsonString = await response.Content.ReadAsStringAsync(); // Sent the data to the user. if (waiter.onResponse != null) { waiter.SendToUser(true, responseJsonString); } waiter.AddStatus("Executed Successfully"); } else { // The Data was not sent. if (waiter.onResponse != null) { waiter.SendToUser(false, "Err : Not Writend Null"); } waiter.AddStatus("Executed With an Error"); } // Set to the Waiter Manager, that the tast has been ended. WaiterManager.End(waiter); } catch (Exception e) { // There was an Error. if (waiter.onResponse != null) { waiter.SendToUser(false, "Err : Async : " + e.Message); } waiter.AddStatus("Executed With an Error : " + e.Message); // Set to the Waiter Manager, that the tast has been ended. WaiterManager.End(waiter); } // Set to the Waiter Manager, that the tast has been ended. WaiterManager.End(waiter); }