private static async Task PostLog(ILoggingStore store, UssdRequest request, UssdResponse response,
                                          DateTime startTime, DateTime endTime, bool dispose)
        {
            if (store == null)
            {
                return;
            }
            var log = await store.Find(request.SessionId);

            if (log == null)
            {
                return;
            }
            log.EndTime = endTime;
            log.DurationInMilliseconds = endTime.Subtract(log.StartTime).TotalMilliseconds;
            log.ErrorStackTrace        = response.Exception == null ? null : response.Exception.StackTrace;
            var entry = new UssdSessionLogEntry
            {
                StartTime = startTime,
                EndTime   = endTime,
                DurationInMilliseconds = endTime.Subtract(startTime).TotalMilliseconds,
                UssdRequest            = request,
                UssdResponse           = response,
            };
            await store.Update(log);

            await store.AddEntry(request.SessionId, entry);

            if (dispose)
            {
                store.Dispose();
            }
        }
        /// <summary>
        /// Process USSD
        /// </summary>
        /// <param name="store"></param>
        /// <param name="request"></param>
        /// <param name="initiationController">Initiation controller</param>
        /// <param name="initiationAction">Initiation action</param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static async Task <UssdResponse> Process(IStore store, UssdRequest request,
                                                        string initiationController, string initiationAction,
                                                        Dictionary <string, string> data = null)
        {
            if (data == null)
            {
                data = new Dictionary <string, string>();
            }
            var context = new UssdContext(store, request, data);

            try
            {
                switch (request.RequestType)
                {
                case UssdRequestTypes.Initiation:
                    var route = string.Format("{0}Controller.{1}",
                                              initiationController, initiationAction);
                    return(await OnInitiation(context, route));

                default:
                    return(await OnResponse(context));
                }
            }
            catch (Exception e)
            {
                return(UssdResponse.Render(e.Message));
            }
            finally
            {
                context.Dispose();
            }
        }
Exemple #3
0
        public UssdResponse Render(string message, string action = null,
                                   string controller             = null)
        {
            string route = null;

            if (action != null)
            {
                route = Route(action, controller);
            }
            return(UssdResponse.Render(message, route));
        }
        /// <summary>
        /// Process USSD requests.
        /// </summary>
        /// <param name="store">Session store</param>
        /// <param name="request"></param>
        /// <param name="initiationController">Initiation controller</param>
        /// <param name="initiationAction">Initiation action</param>
        /// <param name="data">Data available to controllers</param>
        /// <param name="loggingStore">Logging store</param>
        /// <param name="arbitraryLogData">Arbitrary data to add to session log</param>
        /// <param name="dispose">Dispose stores</param>
        /// <returns></returns>
        public static async Task <UssdResponse> ProcessRequest(IStore store, UssdRequest request,
                                                               string initiationController, string initiationAction, Dictionary <string, string> data = null,
                                                               ILoggingStore loggingStore = null, string arbitraryLogData = null, bool dispose = true)
        {
            var startTime = DateTime.UtcNow;

            if (data == null)
            {
                data = new Dictionary <string, string>();
            }
            var          context = new UssdContext(store, request, data);
            UssdResponse response;
            DateTime     endTime;

            await PreLog(loggingStore, request, startTime, arbitraryLogData);

            try
            {
                switch (request.RequestType)
                {
                case UssdRequestTypes.Initiation:
                    var route = string.Format("{0}Controller.{1}",
                                              initiationController, initiationAction);
                    response = await OnInitiation(context, route);

                    break;

                case UssdRequestTypes.Response:
                    response = await OnResponse(context);

                    break;

                default:
                    response = UssdResponse.Render(string.Format("Request Type is {0}. Will ignore intended action.", request.Type));
                    break;
                }
            }
            catch (Exception e)
            {
                response = UssdResponse.Render(e.Message);
                response.SetException(e);
            }
            finally
            {
                if (dispose)
                {
                    context.Dispose();
                }
                endTime = DateTime.UtcNow;
            }
            await PostLog(loggingStore, request, response, startTime, endTime, dispose);

            return(response);
        }
Exemple #5
0
 public UssdResponse Redirect(string action, string controller = null)
 {
     return(UssdResponse.Redirect(Route(action, controller)));
 }
Exemple #6
0
 private static async Task PostLog(ILoggingStore store, UssdRequest request, UssdResponse response,
     DateTime startTime, DateTime endTime, bool dispose)
 {
     if (store == null) return;
     var log = await store.Find(request.SessionId);
     if (log == null) return;
     log.EndTime = endTime;
     log.DurationInMilliseconds = endTime.Subtract(log.StartTime).TotalMilliseconds;
     log.ErrorStackTrace = response.Exception == null ? null : response.Exception.StackTrace;
     var entry = new UssdSessionLogEntry
     {
         StartTime = startTime,
         EndTime = endTime,
         DurationInMilliseconds = endTime.Subtract(startTime).TotalMilliseconds,
         UssdRequest = request,
         UssdResponse = response,
     };
     await store.Update(log);
     await store.AddEntry(request.SessionId, entry);
     if (dispose) store.Dispose();
 }