示例#1
0
        /// <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();
            }
        }
示例#2
0
        private static async Task <UssdResponse> OnInitiation(UssdContext context,
                                                              string route)
        {
            await context.SessionClose();

            await context.SessionSetNextRoute(route);

            return(await OnResponse(context));
        }
示例#3
0
        /// <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);
        }
示例#4
0
 /// <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;
             default:
                 response = await OnResponse(context);
                 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;
 }
示例#5
0
        private static async Task <UssdResponse> OnResponse(UssdContext context)
        {
            while (true)
            {
                var exists = await context.SessionExists();

                if (!exists)
                {
                    throw new Exception("Session does not exist.");
                }
                var response = await context.SessionExecuteAction();

                if (!response.IsRelease)
                {
                    await context.SessionSetNextRoute(response.NextRoute);
                }
                if (response.IsRedirect)
                {
                    continue;
                }
                return(response);
            }
        }
示例#6
0
 private static async Task<UssdResponse> OnResponse(UssdContext context)
 {
     while (true)
     {
         var exists = await context.SessionExists();
         if (!exists)
         {
             throw new Exception("Session does not exist.");
         }
         var response = await context.SessionExecuteAction();
         if (!response.IsRelease)
         {
             await context.SessionSetNextRoute(response.NextRoute);
         }
         if (response.IsRedirect)
         {
             continue;
         }
         return response;
     }
 }
示例#7
0
 private static async Task<UssdResponse> OnInitiation(UssdContext context,
     string route)
 {
     await context.SessionClose();
     await context.SessionSetNextRoute(route);
     return await OnResponse(context);
 }