public void SendRequestWithBadKeys_ResponseInterpretationIs_InvalidSig()
        {
            ID2LUserContext badContext = ContextProvider.BadUserContext();
            HttpWebRequest  request    = RequestProvider.PrepareApiRequest(badContext, RouteProvider.OrganizationInfoRoute);

            try {
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { }
            } catch (WebException ex) {
                var exceptionWrapper = new D2LWebException(ex);
                var interpretation   = badContext.InterpretResult(exceptionWrapper);
                Assert.AreEqual(RequestResult.RESULT_INVALID_SIG, interpretation);
            }
        }
Exemplo n.º 2
0
        private HttpWebResponse RetryGetRequest(ID2LUserContext opContext, string route)
        {
            var mainRequest = CreateGetRequestForRoute(opContext, route);

            try {
                return(mainRequest.GetResponse() as HttpWebResponse);
            } catch (WebException ex) {
                var exWrapper = new D2LWebException(ex);
                opContext.InterpretResult(exWrapper);
            }
            var retryRequest = CreateGetRequestForRoute(opContext, route);

            return(retryRequest.GetResponse() as HttpWebResponse);
        }
        public static void Perform(HttpWebRequest request,
                                   ID2LUserContext userContext,
                                   HandleResponse responseHandler,
                                   HandleError errorHandler,
                                   int retryAttempts = 0)
        {
            try {
                using (var response = (HttpWebResponse)request.GetResponse()) {
                    using (var stream = response.GetResponseStream()) {
                        using (var reader = new StreamReader(stream, Encoding.UTF8)) {
                            string responseBody = reader.ReadToEnd();
                            responseHandler(responseBody);
                        }
                    }
                }
            }
            catch (WebException we) {
                var exceptionWrapper = new D2LWebException(we);
                var result           = userContext.InterpretResult(exceptionWrapper);

                switch (result)
                {
                case RequestResult.RESULT_INVALID_TIMESTAMP:
                    if (retryAttempts > 0)
                    {
                        // re-create the request to ensure the new url accounts for calculated skew
                        Uri uri = userContext.CreateAuthenticatedUri(request.RequestUri.AbsoluteUri,
                                                                     request.Method);

                        request = (HttpWebRequest)WebRequest.Create(uri);

                        Perform(request, userContext, responseHandler, errorHandler, retryAttempts - 1);
                    }
                    break;
                }
                errorHandler(result, request.RequestUri);
            }
            catch (ArgumentException) {
                errorHandler(RequestResult.RESULT_UNKNOWN, request.RequestUri);
            }
        }
        public static void Perform( HttpWebRequest request,
            ID2LUserContext userContext,
            HandleResponse responseHandler,
            HandleError errorHandler,
            int retryAttempts = 0)
        {
            try {
                using (var response = (HttpWebResponse) request.GetResponse()) {
                    using (var stream = response.GetResponseStream()) {
                        using (var reader = new StreamReader( stream, Encoding.UTF8 )) {
                            string responseBody = reader.ReadToEnd();
                            responseHandler( responseBody );
                        }
                    }
                }
            }
            catch (WebException we) {
                var exceptionWrapper = new D2LWebException( we );
                var result = userContext.InterpretResult( exceptionWrapper );

                switch (result) {
                    case RequestResult.RESULT_INVALID_TIMESTAMP:
                        if (retryAttempts > 0) {
                            // re-create the request to ensure the new url accounts for calculated skew
                            Uri uri = userContext.CreateAuthenticatedUri( request.RequestUri.AbsoluteUri,
                                                                          request.Method );

                            request = (HttpWebRequest) WebRequest.Create( uri );

                            Perform( request, userContext, responseHandler, errorHandler, retryAttempts - 1 );
                        }
                        break;
                }
                errorHandler( result, request.RequestUri );
            }
            catch (ArgumentException) {
                errorHandler( RequestResult.RESULT_UNKNOWN, request.RequestUri );
            }
        }
        public void RetryAuthenticatedRequestWithCorrectedTimestamp_ResultCodeIs200()
        {
            ID2LAppContext  appContext  = CreateAppContextWithDelay(ConfigHelper.AppId, ConfigHelper.AppKey, TEST_TIME_DELAY);
            ID2LUserContext userContext = ContextProvider.UserContext(appContext);
            Uri             uri         = userContext.CreateAuthenticatedUri(RouteProvider.OrganizationInfoRoute, "GET");
            HttpWebRequest  request     = (HttpWebRequest)WebRequest.Create(uri);

            request.Method = "GET";

            try {
                using (request.GetResponse() as HttpWebResponse) { }
            } catch (WebException ex) {
                var exWrapper = new D2LWebException(ex);
                userContext.InterpretResult(exWrapper);
            }

            Uri            retryUri     = userContext.CreateAuthenticatedUri(RouteProvider.OrganizationInfoRoute, "GET");
            HttpWebRequest retryRequest = (HttpWebRequest)WebRequest.Create(retryUri);

            retryRequest.Method = "GET";

            Assert.DoesNotThrow(() => { using (HttpWebResponse response = retryRequest.GetResponse() as HttpWebResponse) { } });
        }
Exemplo n.º 6
0
    protected void CallGetVersions(ID2LUserContext userContext, int retryAttempts)
    {
        //in this case we are using an anonymous user
        Uri uri = userContext.CreateAuthenticatedUri(
            "/d2l/api/versions/", "GET");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";

        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        string responseBody = reader.ReadToEnd();

                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        ProductVersions[] versions = serializer.Deserialize<ProductVersions[]>(responseBody);
                        String output = "";
                        foreach (ProductVersions v in versions)
                        {
                            output += v.ProductCode + ": " + v.LatestVersion + "<br />";
                        }
                        resultBox.InnerHtml = output;

                    }
                }
            }
        }
        //catches status codes in the 4 and 5 hundred series
        catch (WebException we)
        {
            D2LWebException exceptionWrapper = new D2LWebException(we);

            RequestResult result = userContext.InterpretResult(exceptionWrapper);
            switch (result)
            {
                //if the timestamp is invalid and we haven't exceeded the retry limit then the call is made again with the adjusted timestamp
                case RequestResult.RESULT_INVALID_TIMESTAMP:
                    if (retryAttempts > 0)
                    {
                        CallWhoAmI(userContext, retryAttempts - 1);
                    }
                    break;

            }
        
            
        }
    }
Exemplo n.º 7
0
    protected void CallWhoAmI(ID2LUserContext userContext,int retryAttempts)
    {
         Uri uri = userContext.CreateAuthenticatedUri(
            "/d2l/api/lp/1.0/users/whoami?fakeparam=lol", "GET");

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.Method = "GET";
        request.AllowAutoRedirect = false;

        try
        {
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                    {
                        string responseBody = reader.ReadToEnd();
                        JavaScriptSerializer serializer = new JavaScriptSerializer();
                        WhoAmIUser user = serializer.Deserialize<WhoAmIUser>(responseBody);
                        resultBox.InnerHtml = "<div>First Name: " + user.FirstName + "</div><div>Last Name: " + user.LastName + "</div><div>D2LID: " + user.Identifier + "</div>"; ;
                    }
                }
            }
        }
        //catches status codes in the 4 and 5 hundred series
        catch (WebException we)
        {
            D2LWebException exceptionWrapper = new D2LWebException(we);
            RequestResult result = userContext.InterpretResult(exceptionWrapper);
            System.Diagnostics.Debug.WriteLine(result.ToString());
            switch (result)
            {
                //if there is no user or the current user doesn't have permission to perform the action
                case RequestResult.RESULT_INVALID_SIG:
                    resultBox.InnerHtml = "Error Must Authenticate as User With Permission";
                    break;
                //if the timestamp is invalid and we haven't exceeded the retry limit then the call is made again with the adjusted timestamp
                case RequestResult.RESULT_INVALID_TIMESTAMP:
                    if (retryAttempts > 0)
                    {
                        CallWhoAmI(userContext, retryAttempts - 1);
                    }
                    break;
            }
                   
        }
    }