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 ); } }
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; } } }
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; } } }