private void SendRequestForDownload(Stream outStream, ProgressCallbackHelper progressCallback, KiiHttpClientCallback callback)
        {
            SetSDKClientInfo();
            try
            {
                HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

                ApiResponse res = ParseResponseHeader(httpResponse);
                // read body
                ReadBodyStream(httpResponse, progressCallback, outStream);
                res.Body = "";

                callback(res, null);
            }
            catch (System.Net.WebException e)
            {
                Console.Write("Exception " + e.Message);
                System.Net.HttpWebResponse err = (System.Net.HttpWebResponse)e.Response;
                // read body
                string body = ReadBodyFromResponse(err);

                callback(null, KiiHttpUtils.TypedException((int)err.StatusCode, body));
            }
            catch (Exception e)
            {
                callback(null, e);
            }
        }
        public virtual void SendRequest(KiiHttpClientCallback callback)
        {
            SetSDKClientInfo();
            try
            {
                HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

                ApiResponse res = ParseResponseHeader(httpResponse);
                // read body
                res.Body = ReadBodyFromResponse(httpResponse);

                callback(res, null);
            }
            catch (System.Net.WebException e)
            {
                Console.Write("Exception " + e.Message);
                System.Net.HttpWebResponse err = (System.Net.HttpWebResponse)e.Response;
                // read body
                string body = ReadBodyFromResponse(err);

                callback(null, KiiHttpUtils.TypedException((int)err.StatusCode, body));
            }
            catch (Exception e)
            {
                callback(null, e);
            }
        }
        public ApiResponse SendRequest()
        {
            SetSDKClientInfo();
            try {
                HttpWebResponse httpResponse = (HttpWebResponse)request.GetResponse();

                ApiResponse res = new ApiResponse();
                // status
                res.Status = (int)httpResponse.StatusCode;
                // Etag
                res.ETag = httpResponse.Headers["ETag"];
                // ContentType
                res.ContentType = httpResponse.ContentType;
                // read body
                res.Body = ReadBodyFromResponse(httpResponse);
                CopyHttpHeaders(httpResponse, res);
                return(res);
            } catch (System.Net.WebException e) {
                Console.Write("Exception " + e.Message);
                System.Net.HttpWebResponse err = (System.Net.HttpWebResponse)e.Response;
                // read body
                string body = ReadBodyFromResponse(err);

                throw KiiHttpUtils.TypedException((int)err.StatusCode, body);
            }
        }
Exemple #4
0
 public void TestHTTP400_Reason_INVALID_ACCOUNT_STATUS()
 {
     // call methods
     try
     {
         throw KiiHttpUtils.TypedException(400, "{" +
                                           "\"errorCode\" : \"INVALID_ACCOUNT_STATUS\"}");
     }catch (BadRequestException be)
     {
         Assert.AreEqual(be.reason, BadRequestException.Reason.INVALID_ACCOUNT_STATUS);
     }catch (Exception ex)
     {
         Assert.Fail("throw unexpected exception " + ex);
     }
 }
Exemple #5
0
 public void TestHTTP400_Reason_QUERY_NOT_SUPPORTED()
 {
     // call methods
     try
     {
         throw KiiHttpUtils.TypedException(400, "{" +
                                           "\"errorCode\" : \"QUERY_NOT_SUPPORTED\"}");
     }catch (BadRequestException be)
     {
         Assert.AreEqual(be.reason, BadRequestException.Reason.QUERY_NOT_SUPPORTED);
     }catch (Exception ex)
     {
         Assert.Fail("throw unexpected exception " + ex);
     }
 }
Exemple #6
0
 public void TestHTTP409_Reason_USER_ALREADY_EXISTS()
 {
     // call methods
     try
     {
         throw KiiHttpUtils.TypedException(409, "{" +
                                           "\"errorCode\" : \"USER_ALREADY_EXISTS\"}");
     }catch (ConflictException ce)
     {
         Assert.AreEqual(ce.reason, ConflictException.Reason.USER_ALREADY_EXISTS);
     }catch (Exception ex)
     {
         Assert.Fail("throw unexpected exception " + ex);
     }
 }
Exemple #7
0
 public void TestHTTP409_Reason_OBJECT_VERSION_IS_STALE()
 {
     // call methods
     try
     {
         throw KiiHttpUtils.TypedException(409, "{" +
                                           "\"errorCode\" : \"OBJECT_VERSION_IS_STALE\"}");
     }catch (ConflictException ce)
     {
         Assert.AreEqual(ce.reason, ConflictException.Reason.OBJECT_VERSION_IS_STALE);
     }catch (Exception ex)
     {
         Assert.Fail("throw unexpected exception " + ex);
     }
 }
Exemple #8
0
 public void TestHTTP404_Reason_PUSH_SUBSCRIPTION_NOT_FOUND()
 {
     // call methods
     try
     {
         throw KiiHttpUtils.TypedException(404, "{" +
                                           "\"errorCode\" : \"\"}");
     }catch (NotFoundException ne)
     {
         Assert.AreEqual(ne.reason, NotFoundException.Reason.__UNKNOWN__);
     }catch (Exception ex)
     {
         Assert.Fail("throw unexpected exception " + ex);
     }
 }
Exemple #9
0
 public void TestHTTP404_Reason_USER_ADDRESS_NOT_FOUND()
 {
     // call methods
     try
     {
         throw KiiHttpUtils.TypedException(404, "{" +
                                           "\"errorCode\" : \"USER_ADDRESS_NOT_FOUND\"}");
     }catch (NotFoundException ne)
     {
         Assert.AreEqual(ne.reason, NotFoundException.Reason.USER_ADDRESS_NOT_FOUND);
     }catch (Exception ex)
     {
         Assert.Fail("throw unexpected exception " + ex);
     }
 }
Exemple #10
0
 public void TestHTTP400_Reason_Unknown()
 {
     // call methods
     try
     {
         throw KiiHttpUtils.TypedException(400, "{" +
                                           "\"errorCode\" : \"\"" + "}");
     }catch (BadRequestException be)
     {
         Assert.AreEqual(be.reason, BadRequestException.Reason.__UNKNOWN__);
     }catch (Exception ex)
     {
         Assert.Fail("throw unexpected exception " + ex);
     }
 }
Exemple #11
0
        public void TypedExceptionMappingTest()
        {
            Dictionary <int, Type> testCases = new Dictionary <int, Type>();

            testCases.Add(400, typeof(BadRequestException));
            testCases.Add(401, typeof(UnauthorizedException));
            testCases.Add(403, typeof(ForbiddenException));
            testCases.Add(404, typeof(NotFoundException));
            testCases.Add(409, typeof(ConflictException));
            testCases.Add(410, typeof(GoneException));
            testCases.Add(500, typeof(CloudException));

            foreach (int status in testCases.Keys)
            {
                Exception e = KiiHttpUtils.TypedException(status, "");
                if (e.GetType() != testCases [status])
                {
                    Assert.Fail("expected exception class is " + testCases [status].ToString() + " but actual is " + e.GetType().ToString());
                }
                Assert.AreEqual(status, ((CloudException)e).Status);
            }
        }
Exemple #12
0
 public void TestHTTP505()
 {
     throw KiiHttpUtils.TypedException(505, "{" +
                                       "\"errorCode\" : \"\"}");
 }
Exemple #13
0
 public void TestHTTP403()
 {
     throw KiiHttpUtils.TypedException(403, "{" +
                                       "\"errorCode\" : \"INVALID_VERIFICATION_CODE\"}");
 }
Exemple #14
0
 public void TestHTTP401()
 {
     throw KiiHttpUtils.TypedException(401, "{" +
                                       "\"errorCode\" : \"UNAUTHORIZED\"}");
 }