public async Task Provide_Response_With_Release_Type_When_SessionId_Is_Not_Found_For_Any_Response_Request() { //arrange var service = serviceProvider.GetService <IHubtelProgrammableService>(); var sessionIdFromHubtel = Guid.NewGuid().ToString("N"); var request = new ProgrammableServiceRequest { Type = ProgrammableServiceActionType.Initiation.ToString("G"), Message = "", Operator = "Hubtel-Android", Mobile = "233249441409", SessionId = sessionIdFromHubtel }; //arrange again var request2 = new ProgrammableServiceRequest { Type = ProgrammableServiceActionType.Response.ToString("G"), Message = "Augustine", //the expected response Operator = "Hubtel-Android", Mobile = "233249441409", SessionId = Guid.NewGuid().ToString("") //perhaps a wrong call from Hubtel??? }; //act var resp = await service.ExecuteInteraction(request, nameof(Example2Controller), nameof(Example2Controller.Index)); resp = await service.ExecuteInteraction(request2, nameof(Example2Controller)); resp.Type.Should().Be(ProgrammableServiceActionType.Release.ToString("G")); }
public async Task <IActionResult> DoEvd( [FromBody] ProgrammableServiceRequest request ) { //this action will be called anytime a user wants to interact with your application _logger.LogDebug("received request for {msisdn} {session_id} {gs_request}", request.Mobile, request.SessionId, JsonConvert.SerializeObject(request)); var response = await _programmableService.ExecuteInteraction(request, nameof(EvdController)); return(Ok(response)); }
public async Task Provide_Response_With_Release_Type_When_Request_Type_Is_Timeout_From_Hubtel() { //arrange var service = serviceProvider.GetService <IHubtelProgrammableService>(); var request = new ProgrammableServiceRequest { Type = ProgrammableServiceActionType.Timeout.ToString("G") }; //act var resp = await service.ExecuteInteraction(request, nameof(Example1Controller)); resp.Type.Should().Be(ProgrammableServiceActionType.Release.ToString("G")); }
public void HandleRequest(IApplicationBuilder app) { app.Run(async context => { //read payload, convert to request object //grab response from _programmableService instance, convert to JSON and send back var requestString = await context.Request.ReadRequestStreamAsStringAsync(); ProgrammableServiceRequest request = null; ProgrammableServiceResponse response; try { request = JsonConvert.DeserializeObject <ProgrammableServiceRequest>(requestString); } catch (Exception e) { // we don't know what happened oooo } if (request == null) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; response = new ProgrammableServiceResponse { Message = "Something bad happened", Type = ProgrammableServiceActionType.Release.ToString("G"), Label = "Something bad happened" }; } else { context.Response.StatusCode = (int)HttpStatusCode.OK; response = await _programmableService.ExecuteInteraction(request, _controller, _action); } var responseJson = JsonConvert.SerializeObject(response, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Formatting.Indented }); context.Response.ContentLength = responseJson.Length; context.Response.ContentType = "application/json"; await context.Response.WriteAsync(responseJson, Encoding.UTF8); }); }
public async Task Provide_Response_With_Release_Type_When_ActionMethod_Is_Unknown() { //arrange var service = serviceProvider.GetService <IHubtelProgrammableService>(); var request = new ProgrammableServiceRequest { Type = ProgrammableServiceActionType.Initiation.ToString("G"), Message = "", Operator = "Hubtel-Android", Mobile = "233249441409", SessionId = Guid.NewGuid().ToString("N") }; //act var resp = await service.ExecuteInteraction(request, nameof(Example1Controller), "Boom"); resp.Type.Should().Be(ProgrammableServiceActionType.Release.ToString("G")); }
private async Task <ProgrammableServiceResponse> ExecuteInteractionCore(ProgrammableServiceRequest request, string controllerName , string actionName = "") { if (string.IsNullOrEmpty(controllerName)) { throw new ArgumentNullException(nameof(controllerName)); } controllerName = controllerName.EndsWith("controller", StringComparison.OrdinalIgnoreCase) ? controllerName : $"{controllerName}Controller"; if (!_controllerActivator.ControllerCollection.ContainsKey(controllerName)) { return(new ProgrammableServiceResponse { Message = $"Could not find {controllerName}", Type = ProgrammableServiceActionType.Release.ToString("G"), Label = $"Could not find {controllerName}", DataType = ProgrammableServiceDataTypes.Display, FieldType = ProgrammableServiceFieldTypes.Text }); } if (request == null) { throw new ArgumentNullException(nameof(request)); } var context = new ProgrammableServiceContextBuilder(request, _configuration).Build(); if (context.Request.GetActionType() == ProgrammableServiceActionType.Unknown) { return(new ProgrammableServiceResponse { Message = "Request type could not be parsed", Type = ProgrammableServiceActionType.Release.ToString("G"), Label = "Request type could not be parsed", DataType = ProgrammableServiceDataTypes.Display, FieldType = ProgrammableServiceFieldTypes.Text }); } if (context.Request.GetActionType() == ProgrammableServiceActionType.Timeout) { //we have to check if the user wants to explicitly ignore timeouts return(new ProgrammableServiceResponse { Message = "Timeout from gateway. Request has been ignored", Type = ProgrammableServiceActionType.Release.ToString("G"), Label = "Timeout from gateway. Request has been ignored", DataType = ProgrammableServiceDataTypes.Display, FieldType = ProgrammableServiceFieldTypes.Text }); } var controllerInfo = _controllerActivator.ControllerCollection[controllerName]; if (context.Request.GetActionType() == ProgrammableServiceActionType.Initiation) { //we ought to check if the action name is in the controller //if actionName is null, we find the method that has [HandleInitiation] attribute if (string.IsNullOrEmpty(actionName)) { var intendedAction = controllerInfo.Methods.Values.FirstOrDefault(m => m.IsInitiationMethod); if (intendedAction == null) { return(new ProgrammableServiceResponse { Message = "no initiation handler", Type = ProgrammableServiceActionType.Release.ToString("G"), Label = "no initiation handler", DataType = ProgrammableServiceDataTypes.Display, FieldType = ProgrammableServiceFieldTypes.Text }); } actionName = intendedAction.Method.Name; if (string.IsNullOrEmpty(actionName)) //perhaps the initiation handler attribute is not anywhere { return(new ProgrammableServiceResponse { Message = "no initiation handler", Type = ProgrammableServiceActionType.Release.ToString("G"), Label = "no initiation handler", DataType = ProgrammableServiceDataTypes.Display, FieldType = ProgrammableServiceFieldTypes.Text }); } } else { if (!controllerInfo.Methods.ContainsKey(actionName)) { return(new ProgrammableServiceResponse { Message = $"Initiation handler {actionName} does not exists", Type = ProgrammableServiceActionType.Release.ToString("G"), Label = $"Initiation handler {actionName} does not exists", DataType = ProgrammableServiceDataTypes.Display, FieldType = ProgrammableServiceFieldTypes.Text }); } } await context.Store.Delete(context.NextRouteKey); await context.Store.Delete(context.DataBagKey); await context.Store.Set(context.NextRouteKey, new ProgrammableServiceRoute { ActionName = actionName, ControllerName = controllerName }.ToJson()); } else if (context.Request.GetActionType() == ProgrammableServiceActionType.Query || context.Request.GetActionType() == ProgrammableServiceActionType.Favorite) { await context.Store.Set(context.NextRouteKey, new ProgrammableServiceRoute { ActionName = actionName, ControllerName = controllerName }.ToJson()); } try { return(await OnResponse(context, controllerInfo)); } catch (Exception e) { return(new ProgrammableServiceResponse { Message = e.Message, Type = ProgrammableServiceActionType.Release.ToString("G"), Label = e.Message, DataType = ProgrammableServiceDataTypes.Display, FieldType = ProgrammableServiceFieldTypes.Text }); } }
public async Task <ProgrammableServiceResponse> ExecuteInteraction(ProgrammableServiceRequest request, string controllerName, string actionName = "") { return(await ExecuteInteractionCore(request, controllerName, actionName)); }
public ProgrammableServiceContextBuilder(ProgrammableServiceRequest request, ProgrammableServiceConfiguration configuration) { _request = request; _configuration = configuration; }