public static async Task <T> DeleteAsync <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)
                        .DeleteAsync();

            return(Parse <T>(bytes));
        }
Example #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);
        }
        private async void GetAsync(ManualResetEvent waitHandle)
        {
            try
            {
                var content = await HttpOperation
                              .WithUrl("http://www.google.co.in")
                              .GetAsync();

                Console.WriteLine(Encoding.UTF8.GetString(content));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                waitHandle.Set();
            }
        }