public static async Task <T> PostAsync <T>(ApiRequest request, string url)
            where T : ApiResponse
        {
            var bytes = await HttpOperation
                        .WithUrl(url)
                        .WithAppacitiveKeyOrSession(request.ApiKey, request.SessionToken, request.UseApiSession)
                        .WithEnvironment(request.Environment)
                        .WithUserToken(request.UserToken)
                        .PostAsyc(request.ToBytes());

            return(Parse <T>(bytes));
        }
Beispiel #2
0
        public static async Task <T> PostAsync <T>(ApiRequest request, string url)
            where T : ApiResponse
        {
            T         response = null;
            Exception fault    = null;

            byte[] responseBytes  = null, requestBytes = null;
            long   elasedTimeInMs = 0;
            var    op             = HttpOperation
                                    .WithUrl(url)
                                    .WithApiKey(request.ApiKey)
                                    .WithEnvironment(request.Environment)
                                    .WithUserToken(request.UserToken);

            try
            {
#if !WINDOWS_PHONE7
                Stopwatch timer = Stopwatch.StartNew();
#else
                var startTime = DateTime.Now;
#endif
                requestBytes  = request.ToBytes();
                responseBytes = await op.PostAsyc(requestBytes);

#if !WINDOWS_PHONE7
                elasedTimeInMs = timer.ElapsedMilliseconds;
                timer.Stop();
                timer = null;
#else
                elasedTimeInMs = Convert.ToInt64(DateTime.Now.Subtract(startTime).TotalMilliseconds);
#endif
                response = Parse <T>(responseBytes);
                await HandleExpiredUserToken(response);
            }
            catch (Exception ex)
            {
                fault = ex;
            }
            await TraceAsync(response == null?string.Empty : response.Status.ReferenceId,
                             "POST", url, op.Headers, requestBytes, responseBytes, elasedTimeInMs,
                             request, response,
                             fault);

            if (fault != null)
            {
                throw fault;
            }
            return(response);
        }