public async Task <WorklightResult> RestInvokeAsync() { var result = new WorklightResult(); try { StringBuilder uriBuilder = new StringBuilder() .Append(client.ServerUrl.AbsoluteUri) // Get the server URL .Append("/adapters") .Append("/SampleHTTPAdapter") //Name of the adapter .Append("/getStories"); // Name of the adapter procedure WorklightResourceRequest rr = client.ResourceRequest(new Uri(uriBuilder.ToString()), "GET"); WorklightResponse resp = await rr.Send(); result.Success = resp.Success; result.Message = (resp.Success) ? "Connected" : resp.Message; result.Response = resp.ResponseText; } catch (Exception ex) { result.Success = false; result.Message = ex.Message; } return(result); }
public async Task <WorklightResult> UnprotectedInvokeAsync() { var result = new WorklightResult(); try { StringBuilder uriBuilder = new StringBuilder() .Append("/adapters") .Append("/ResourceAdapter") //Name of the adapter .Append("/publicData"); // Name of the adapter procedure WorklightResourceRequest rr = client.ResourceRequest(new Uri(uriBuilder.ToString(), UriKind.Relative), "GET"); WorklightResponse resp = await rr.Send(); result.Success = resp.Success; //result.Message = (resp.Success) ? "Connected" : resp.Message; result.Response = resp.ResponseText; } catch (Exception ex) { result.Success = false; result.Message = ex.Message; } return(result); }
private async void InvokeProcedure() { //Invoke Procedure - the new REST way Articles.Clear(); //hopefully I can integrate oAuth with this sample soon Uri adapterMethod = new Uri(App.wlClient.ServerUrl + "/adapters/SampleHTTPAdapter/getFeed"); WorklightResourceRequest wlResReq = MFPNewsReader.App.wlClient.ResourceRequest(adapterMethod, "GET"); WorklightResponse resp = await wlResReq.Send(); if (resp.Success) { JsonArray jsonArray = (JsonArray)resp.ResponseJSON["rss"]["channel"]["item"]; foreach (JsonObject title in jsonArray) { System.Json.JsonValue titleString; title.TryGetValue("title", out titleString); System.Json.JsonValue itemString; title.TryGetValue("description", out itemString); Articles.Add(new Article(titleString, itemString)); } } else { DisplayAlert("Connection Status failed", resp.Message, "Close"); } }
private async void SubmitValues() { User _UserFromPage = ResourceRequestXamarinFormsPage.GetTextValues(); IWorklightClient _newClient = App.GetWorklightClient; StringBuilder uriBuilder = new StringBuilder().Append("/adapters/JavaAdapter/users") .Append("/").Append(_UserFromPage.FirstName) .Append("/").Append(_UserFromPage.MiddleName) .Append("/").Append(_UserFromPage.LastName); WorklightResourceRequest rr = _newClient.ResourceRequest(new Uri(uriBuilder.ToString(), UriKind.Relative), "POST", ""); rr.SetQueryParameter("age", _UserFromPage.Age); System.Net.WebHeaderCollection headerCollection = new WebHeaderCollection(); headerCollection["birthdate"] = _UserFromPage.BirthDate; rr.AddHeader(headerCollection); Dictionary <string, string> formParams = new Dictionary <string, string>(); formParams.Add("height", _UserFromPage.Height); WorklightResponse resp = await rr.Send(formParams); Debug.WriteLine(resp.ResponseJSON); ResourceRequestXamarinFormsPage.DisplayOutput(resp.ResponseText); }
/// <summary> /// Make a REST call to the Adapter on the Server /// </summary> /// <param name="adapterName">The name of the adapter</param> /// <param name="adapterProcedureName">The name of the procedure</param> /// <param name="methodType">The HTTP verb used to call the procedure</param> /// <param name="parameters">JSON parameters</param> /// <returns></returns> public async Task <WorklightResult> RestInvokeAsync(string adapterName, string adapterProcedureName, string methodType, object[] parameters) { var result = new WorklightResult(); try { var uriBuilder = $"{_client.ServerUrl.AbsoluteUri}/adapters/{adapterName}/{adapterProcedureName}"; WorklightResourceRequest rr = _client.ResourceRequest(new Uri(uriBuilder.ToString()), methodType); WorklightResponse resp = await rr.Send(); result.Success = resp.Success; result.Message = (resp.Success) ? "Connected" : resp.Message; result.Response = resp.ResponseText; } catch (Exception ex) { result.Success = false; result.Message = ex.Message; } return(result); }
//mfp initialilzing environment public async void setOffers() { var isConnected = false; //mobileFirst client if (mfpClient == null) { mfpClient = WorklightClient.CreateInstance(this); } System.Console.WriteLine("MFP before connect"); //getting connection WorklightResponse task1 = await mfpClient.Connect(); if (task1.Success) { Console.WriteLine("connected to MFP"); logger = mfpClient.Logger("Xamarin: MainActivity.cs"); logger.Info("connection from Android Plenty app established"); //write to the server the connection status mfpClient.Analytics.Log("Connect response : " + task1.Success); isConnected = true; } else { Console.WriteLine("connection failed"); } //accessing protected feedAdapter if (isConnected) { //adding challengehandler string appRealm = "PlentyAppRealm"; ChallengeHandler customCH = new CustomChallengeHandler(appRealm); mfpClient.RegisterChallengeHandler(customCH); System.Console.WriteLine("MFP before adapter"); WorklightProcedureInvocationData invocationData = new WorklightProcedureInvocationData( "FeedAdapter", "getFeed", new object[] { "" }); WorklightResponse task3 = await mfpClient.InvokeProcedure(invocationData); if (task3.Success) { isInitialCall = false; Console.WriteLine("adapter connected"); Console.WriteLine(task3.ResponseText.ToString()); logger.Info("FeedAdapter Accessed from Android Plenty app"); //write to the server the connection status mfpClient.Analytics.Log("FeedAdapter response : " + task3.Success); } else { Console.WriteLine("adapter failed"); Console.WriteLine(task3.ResponseJSON.ToString()); logger.Error("FeedAdapter FAILED from Android Plenty app"); //write to the server the connection status mfpClient.Analytics.Log("FeedAdapter response : " + task3.ResponseText); } } //end of protected feedAdapter task1 = await mfpClient.Connect(); if (task1.Success) { Console.WriteLine("connected to MFP"); isConnected = true; } else { Console.WriteLine("connection failed"); } //ACCESSING STRONGLOOP for Offers if (isConnected) { System.Console.WriteLine("MFP before SL url"); //calling protected adapter (strongloop) /*WorklightProcedureInvocationData invocationData = * new WorklightProcedureInvocationData ( * "SLAdapter", "getOffers", new object[] { "" }); * * WorklightResponse task2 = await mfpClient.InvokeProcedure (invocationData); */ //calling protected url (strongLoop) WorklightResourceRequest resourceRequest = mfpClient.ResourceRequest( new Uri(URL_STRONGLOOP + "Offers"), "GET"); WorklightResponse task2 = await resourceRequest.Send(); if (task2.Success) { Console.WriteLine("strongloop url connected"); Console.WriteLine(task2.ResponseText.ToString()); logger.Info("Strongloop Accessed/Offers from Android Plenty app"); //write to the server the connection status mfpClient.Analytics.Log("Strongloop response : " + task2.Success); Plenty.Offers.offers = new List <OffersModel> { }; JsonArray jsonArray = new JsonArray(task2.ResponseText); Console.WriteLine(jsonArray.Count); var myObjectList = (List <Offer>) JsonConvert.DeserializeObject(task2.ResponseText, typeof(List <Offer>)); foreach (Offer offer in myObjectList) { /* * Console.WriteLine (offer.offername); * Console.WriteLine (offer.offerdescription); * Console.WriteLine (offer.offerpicture); */ Plenty.Offers.offers.Add(new OffersModel( offer.offername, offer.offerdescription, offer.offerpicture)); } } else { Console.WriteLine("strongloop url failed"); Console.WriteLine(task2.ResponseText.ToString()); logger.Error("Strongloop FAILED on Offers from Android Plenty app"); //write to the server the connection status mfpClient.Analytics.Log("Strongloop response : " + task2.ResponseText); //dummy offers Plenty.Offers.offers = new List <OffersModel> { new OffersModel("potatoes", "For offers refer", "Potatoes.jpg"), new OffersModel( "Buy one get one free on salted nuts for the big game!", "This offer entitles you to a free can of salted nuts for every can you purchase. Limit 4.", "offers/asparagus.jpg"), }; } } else { Console.WriteLine("connection for SL failed"); //dummy offers logger.Error("Strongloop FAILED even on Connection to MFP? while on Offers from Android Plenty app"); //write to the server the connection status mfpClient.Analytics.Log("FeedAdapter response : " + task1.ResponseText); Plenty.Offers.offers = new List <OffersModel> { new OffersModel( "Buy one get one free on salted nuts for the big game!", "This offer entitles you to a free can of salted nuts for every can you purchase. Limit 4.", "NUTS.jpg"), }; } //end Strongloops for Offers task1 = await mfpClient.Connect(); if (task1.Success) { Console.WriteLine("connected to MFP"); isConnected = true; } else { Console.WriteLine("connection failed"); } //ACCESSING STRONGLOOP for Events if (isConnected) { System.Console.WriteLine("MFP before SL url"); //calling protected url (strongLoop) WorklightResourceRequest resourceRequest = mfpClient.ResourceRequest( new Uri(URL_STRONGLOOP + "Events"), "GET"); WorklightResponse task4 = await resourceRequest.Send(); if (task4.Success) { Console.WriteLine("strongloop url connected"); Console.WriteLine(task4.ResponseText.ToString()); logger.Info("Strongloop Accessed/Events from Android Plenty app"); //write to the server the connection status mfpClient.Analytics.Log("Strongloop response : " + task4.Success); Plenty.SpecialEvents.events = new List <EventsModel> { }; JsonArray jsonArray = new JsonArray(task4.ResponseText); Console.WriteLine(jsonArray.Count); var myObjectList = (List <Event>)JsonConvert.DeserializeObject(task4.ResponseText, typeof(List <Event>)); foreach (Event myevent in myObjectList) { Plenty.SpecialEvents.events.Add(new EventsModel( myevent.offername, myevent.offerdescription, myevent.offerpicture)); } } else { Console.WriteLine("strongloop url failed"); Console.WriteLine(task4.ResponseText.ToString()); logger.Error("Strongloop FAILED on Events from Android Plenty app"); //write to the server the connection status mfpClient.Analytics.Log("Strongloop response : " + task4.ResponseText); //dummy events Plenty.SpecialEvents.events = new List <EventsModel> { new EventsModel( "Growler Fill-Up", "Fill up your growler from a selection of local craft beers!", "events/fillyourgrowler.jpg"), }; } } else { Console.WriteLine("connection for SL failed"); logger.Error("Strongloop FAILED even on Connection to MFP? while on Offers from Android Plenty app"); //write to the server the connection status mfpClient.Analytics.Log("FeedAdapter response : " + task1.ResponseText); //dummy offers Plenty.Offers.offers = new List <OffersModel> { new OffersModel( "Buy one get one free on salted nuts for the big game!", "This offer entitles you to a free can of salted nuts for every can you purchase. Limit 4.", "NUTS.jpg"), }; } }