public object ConcludeCourse(string termino) { string ambientPrefix = WebConfigurationManager.AppSettings["AMBIENT_PREFIX"]; string tokenString = SessionController.GetToken(); string action = "conclude"; List <uniCanvasCurso> courseToConcludeList = CourseService.GetCourseToConcludeList(termino); foreach (uniCanvasCurso courseToConclude in courseToConcludeList) { WebRequest request = WebRequest.Create(WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + @"/api/lms/v1/courses/sis_course_id:" + courseToConclude.IDAcademico + "?event=" + action); request.Method = "DELETE"; request.Headers.Add(HttpRequestHeader.Authorization, tokenString); Stream dataStream = request.GetRequestStream(); WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); object rtn = JsonConvert.DeserializeObject <CourseReturn>(reader.ReadToEnd()); if (rtn != null) { logger.Info("CourseController/ConcludeCourse - Task 'Create course' STARTED"); CourseService.UpdateConcludedCourseCanvasData(courseToConclude); } } return(null); }
public object Get(int course_id) { string url = WebConfigurationManager.AppSettings["BASE_URL"] + "api/lms/v1/courses/" + course_id; try { HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(string.Format(url)); webReq.Method = "GET"; webReq.Headers.Add(HttpRequestHeader.Authorization, HttpContext.Current.Request.Headers["Authorization"]); HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse(); Stream answer = webResponse.GetResponseStream(); StreamReader _recivedAnswer = new StreamReader(answer); return(JsonConvert.DeserializeObject <CourseDTO>(_recivedAnswer.ReadToEnd())); } catch (Exception e) { if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { HttpContext.Current.Request.Headers.Remove("Authorization"); HttpContext.Current.Request.Headers.Add("Authorization", SessionController.GetToken()); return(Get(course_id)); } return(null); } }
public object GetBySectionId(string sis_section_id, Nullable <int> section_id = 0) { logger.Info("InscriptionService/InactivateInscription - Task 'Get inscription by section Id' STARTED"); if (section_id != 0 || sis_section_id != null) { string url = string.Empty; string ambientPrefix = WebConfigurationManager.AppSettings["AMBIENT_PREFIX"]; if (section_id != 0 && (sis_section_id == "null" || sis_section_id == null)) { url = WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + "/api/lms/v1/sections/" + section_id + "/enrollments"; } if (sis_section_id != null && (section_id == 0 || section_id == null)) { url = WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + "/api/lms/v1/sections/sis_section_id:" + sis_section_id + "/enrollments"; } try { HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(string.Format(url)); webReq.Method = "GET"; webReq.Headers.Add(HttpRequestHeader.Authorization, SessionController.GetToken()); webResponse = (HttpWebResponse)webReq.GetResponse(); Stream answer = webResponse.GetResponseStream(); StreamReader _recivedAnswer = new StreamReader(answer); logger.Info("InscriptionService/InactivateInscription - Task 'Get inscription by section Id' FINISHED"); return(JsonConvert.DeserializeObject <List <InscriptionDTO> >(_recivedAnswer.ReadToEnd())); } catch (Exception e) { logger.Error("InscriptionService/InactivateInscription - Task 'Get inscription by section Id' FINISHED WITH ERROR: \n " + " Message: " + e.Message + "\nInner Exception: " + e.InnerException); if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { HttpContext.Current.Request.Headers.Remove("Authorization"); HttpContext.Current.Request.Headers.Add("Authorization", SessionController.GetToken()); return(GetBySectionId(sis_section_id, section_id)); } return(e.ToString()); } } logger.Info("InscriptionService/InactivateInscription - Task 'Get inscription by section Id' FINISHED"); return(null); }
public object GetAll(int per_page = 0) { logger.Info("UserController/GetAll - Task 'Get all users' STARTED"); string url = string.Empty; if (per_page == 0) { url = WebConfigurationManager.AppSettings["BASE_URL"] + "api/lms/v1/users?per_page=" + per_page; } else { url = WebConfigurationManager.AppSettings["BASE_URL"] + "api/lms/v1/users"; } try { HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(string.Format(url)); webReq.Method = "GET"; webReq.Headers.Add(HttpRequestHeader.Authorization, SessionController.GetToken()); HttpWebResponse webResponse = (HttpWebResponse)webReq.GetResponse(); Stream answer = webResponse.GetResponseStream(); StreamReader _recivedAnswer = new StreamReader(answer); logger.Info("UserController/GetAll - Task 'Get all users' FINISHED"); return(JsonConvert.DeserializeObject <List <UserDTO> >(_recivedAnswer.ReadToEnd())); } catch (Exception e) { logger.Error("UserController/GetAll - Task 'Get all users' FINISHED WITH ERROR: \n" + " Message: " + e.Message + "\nInner Exception: " + e.InnerException); if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { HttpContext.Current.Request.Headers.Remove("Authorization"); HttpContext.Current.Request.Headers.Add("Authorization", SessionController.GetToken()); return(GetAll(per_page)); } return(e.ToString()); } }
public object Create([FromBody] User userDTO) { logger.Info("UserController/Create - Task 'Create user' STARTED"); try { if (userDTO != null) { string ambientPrefix = WebConfigurationManager.AppSettings["AMBIENT_PREFIX"]; WebRequest request = WebRequest.Create(WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + "/api/lms/v1/users"); request.Method = "POST"; request.Headers.Add(HttpRequestHeader.Authorization, SessionController.GetToken()); string postData = "{" + "\"user\":" + "{" + "\"short_name\" : \"" + userDTO.user.full_name + "\"," + "\"sortable_name\" : \"" + userDTO.user.full_name + "\"," + "\"full_name\" : \"" + userDTO.user.full_name + "\"," + "\"login\": \"" + userDTO.user.login + "\"," + "\"email\":\"" + userDTO.user.email + "\"," + "\"sis_user_id\": \"" + userDTO.user.sis_user_id + "\"" + "}" + "}"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/json"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); object rtn = JsonConvert.DeserializeObject <UserReturn>(reader.ReadToEnd()); if (rtn != null) { return(rtn); } rtn = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (rtn != null) { ErrorMessage errorMessageDto = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (errorMessageDto != null) { return(new UserReturn() { error_message = errorMessageDto.errors.First().message }); } logger.Info("UserController/Create - Task 'Create user' FINISHED"); return(null); } logger.Info("UserController/Create - Task 'Create user' FINISHED"); return(null); } logger.Info("UserController/Create - Task 'Create user' FINISHED"); } catch (WebException e) { logger.Error("UserController/Create - Task 'Create user' FINISHED WITH ERROR: \n " + " Message: " + e.Message + "\nInner Exception: " + e.InnerException); if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { HttpContext.Current.Request.Headers.Remove("Authorization"); HttpContext.Current.Request.Headers.Add("Authorization", SessionController.GetToken()); return(Create(userDTO)); } if (e.Message.Contains(HttpStatusCode.BadRequest.ToString())) { var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd(); JObject obj = JsonConvert.DeserializeObject <JObject>(resp); if (obj["errors"]["pseudonym"] != null) { foreach (var x in obj["errors"]["pseudonym"].First) { if (x.First["message"] != null) { return new UserReturn() { error_message = x.First["message"].ToString() } } ; } } return(new UserReturn() { error_message = e.ToString() }); } throw e; } logger.Info("UserController/Create - Task 'Create user' FINISHED"); return(null); }
public object CreateBySectionId([FromBody] Inscription inscriptionDTO, string sis_section_id, Nullable <int> section_id = 0) { logger.Info("InscriptionService/CreateBySectionId - Task 'Create inscription' STARTED"); string ambientPrefix = WebConfigurationManager.AppSettings["AMBIENT_PREFIX"]; ServicePointManager.Expect100Continue = false; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback = delegate { return(true); }; if (section_id != 0 || sis_section_id != null) { string url = string.Empty; url = WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + @"/api/lms/v1/sections/sis_section_id:" + sis_section_id + "/enrollments"; try { string jsonStr = "{" + "\"enrollment\": " + "{" + "\"user_id\":" + inscriptionDTO.enrollment.user_id + "," + "\"type\":\"" + inscriptionDTO.enrollment.type + "\"," + "\"sis_section_id\":" + inscriptionDTO.enrollment.sis_section_id + "," + "\"state\":\"" + "active" + "\"," + "\"send_notification\":" + "false" + "," + "\"limit_interaction\":" + (inscriptionDTO.enrollment.group_id != null ? "true" : "false") + (inscriptionDTO.enrollment.group_id != null ? "," + "\"group_id\":" + inscriptionDTO.enrollment.group_id : "") + "}" + "}"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Headers.Add(HttpRequestHeader.Authorization, SessionController.GetToken()); byte[] formData = UTF8Encoding.UTF8.GetBytes(jsonStr); request.ContentLength = formData.Length; request.ContentType = "application/json"; using (Stream post = request.GetRequestStream()) { post.Write(formData, 0, formData.Length); } HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream resStream = response.GetResponseStream(); StreamReader reader = new StreamReader(resStream); string contents = reader.ReadToEnd(); object rtn = JsonConvert.DeserializeObject <InscriptionReturn>(contents); if (rtn != null) { logger.Info("InscriptionService/CreateBySectionId - Task 'Create inscription' FINISHED"); return(rtn); } rtn = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (rtn != null) { ErrorMessage errorMessageDto = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (errorMessageDto != null) { logger.Info("InscriptionService/CreateBySectionId - Task 'Create inscription' FINISHED"); return(new InscriptionReturn() { error_message = errorMessageDto.errors.First().message }); } logger.Info("InscriptionService/CreateBySectionId - Task 'Create inscription' FINISHED"); return(null); } logger.Info("InscriptionService/CreateBySectionId - Task 'Create inscription' FINISHED"); return(null); } catch (WebException e) { logger.Error("InscriptionService/CreateBySectionId - Task 'Create inscription' FINISHED WITH ERROR: \n " + " Message: " + e.Message + "\nInner Exception: " + e.InnerException); if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { HttpContext.Current.Request.Headers.Remove("Authorization"); HttpContext.Current.Request.Headers.Add("Authorization", SessionController.GetToken()); return(CreateBySectionId(inscriptionDTO, sis_section_id, section_id)); } string errorMessage = Extensions.SearchValueInJObject((JObject)e.Data, "message"); if (errorMessage != null) { return new InscriptionReturn() { error_message = errorMessage } } ; else { return new InscriptionReturn() { error_message = e.ToString() } }; } } logger.Info("InscriptionService/CreateBySectionId - Task 'Create inscription' FINISHED"); return(null); }
public InscriptionReturn ConcludeInscription(string iDAcademicoCurso, int?iDCanvasEnrolamiento) { logger.Info("InscriptionService/InactivateInscription - Task 'Inactivate inscription' STARTED"); if (iDAcademicoCurso != null) { string ambientPrefix = WebConfigurationManager.AppSettings["AMBIENT_PREFIX"]; ServicePointManager.Expect100Continue = false; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; string url = WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + "/api/lms/v1/courses/sis_course_id:" + iDAcademicoCurso + "/enrollments/" + iDCanvasEnrolamiento; try { WebRequest request = WebRequest.Create(url); request.Method = "DELETE"; request.Headers.Add(HttpRequestHeader.Authorization, SessionController.GetToken()); string postJSONString = "{" + "\"action\" : \"" + "conclude" + "\"," + "\"enrollment:id\":" + iDCanvasEnrolamiento + "," + "\"sis_course_id\":\"" + iDAcademicoCurso + "\"" + "}"; string postData = new JavaScriptSerializer().Serialize(postJSONString); byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/json"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); // StreamReader reader = new StreamReader(dataStream); object rtn = JsonConvert.DeserializeObject <InscriptionReturn>(reader.ReadToEnd()); if (rtn != null) { logger.Info("InscriptionService/InactivateInscription - Task 'Inactivate inscription' FINISHED"); return((InscriptionReturn)rtn); } rtn = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (rtn != null) { ErrorMessage errorMessageDto = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (errorMessageDto != null) { logger.Info("InscriptionService/InactivateInscription - Task 'Inactivate inscription' FINISHED"); return(new InscriptionReturn() { error_message = errorMessageDto.errors.First().message }); } logger.Info("InscriptionService/InactivateInscription - Task 'Inactivate inscription' FINISHED"); return(null); } logger.Info("InscriptionService/InactivateInscription - Task 'Inactivate inscription' FINISHED"); return(null); } catch (WebException e) { logger.Error("InscriptionService/InactivateInscription - Task 'Inactivate inscription' FINISHED WITH ERROR: \n " + " Message: " + e.Message + "\nInner Exception: " + e.InnerException); if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { HttpContext.Current.Request.Headers.Remove("Authorization"); HttpContext.Current.Request.Headers.Add("Authorization", SessionController.GetToken()); return(ConcludeInscription(iDAcademicoCurso, iDCanvasEnrolamiento)); } if (e.Message.Contains(HttpStatusCode.BadRequest.ToString())) { var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd(); JObject obj = JsonConvert.DeserializeObject <JObject>(resp); if (obj["errors"]["pseudonym"] != null) { foreach (var x in obj["errors"]["pseudonym"].First) { if (x.First["message"] != null) { return new InscriptionReturn() { error_message = x.First["message"].ToString() } } ; } } return(new InscriptionReturn() { error_message = e.ToString() }); } } } logger.Info("InscriptionService/InactivateInscription - Task 'Inactivate inscription' FINISHED"); return(null); }
public object Create([FromBody] Section sectionDTO, string sis_course_id, Nullable <int> course_id = 0) { logger.Info("SectionController/Create - Task 'Create section' STARTED"); ServicePointManager.Expect100Continue = false; if (sectionDTO != null) { string url = string.Empty; string ambientPrefix = WebConfigurationManager.AppSettings["AMBIENT_PREFIX"]; if (course_id != 0 && sis_course_id == null) { url = WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + "/api/lms/v1/courses/" + course_id + "/sections"; } if (sis_course_id != null && (course_id == 0 || course_id == null)) { url = WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + "/api/lms/v1/courses/sis_course_id:" + sis_course_id + "/sections"; } try { WebRequest request = WebRequest.Create(url); request.Method = "POST"; request.Timeout = 240000; request.Headers.Add(HttpRequestHeader.Authorization, SessionController.GetToken()); string isolate_section = sectionDTO.course_section.isolate_section.HasValue ? sectionDTO.course_section.isolate_section.Value.ToString().ToLower() : "true"; string postData = "{" + "\"course_section\": " + "{" + "\"name\":\"" + sectionDTO.course_section.name + "\"" + "," + "\"sis_section_id\":\"" + sectionDTO.course_section.sis_section_id + "\"," + "\"isolate_section\":" + isolate_section + "," + "\"restrict_to_dates\":" + "false" + "}" + "}"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/json"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); object rtn = JsonConvert.DeserializeObject <SectionDTO>(reader.ReadToEnd()); if (rtn != null) { logger.Info("SectionController/Create - Task 'Create section' FINISHED"); return(rtn); } rtn = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (rtn != null) { ErrorMessage errorMessageDto = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (errorMessageDto != null) { logger.Info("SectionController/Create - Task 'Create section' FINISHED"); return(new SectionDTO() { error_message = errorMessageDto.errors.First().message }); } logger.Info("SectionController/Create - Task 'Create section' FINISHED"); return(null); } logger.Info("SectionController/Create - Task 'Create section' FINISHED"); return(null); } catch (WebException e) { logger.Error("SectionController/Create - Task 'Create section' FINISHED WITH ERROR: \n " + " Message: " + e.Message + "\nInner Exception: " + e.InnerException); if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { HttpContext.Current.Request.Headers.Remove("Authorization"); HttpContext.Current.Request.Headers.Add("Authorization", SessionController.GetToken()); return(Create(sectionDTO, sis_course_id, course_id)); } if (e.Message.Contains(HttpStatusCode.BadRequest.GetHashCode().ToString())) { var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd(); JObject obj = JsonConvert.DeserializeObject <JObject>(resp); if (obj["errors"]["pseudonym"] != null) { foreach (var x in obj["errors"].First) { if (x.First["message"] != null) { return new SectionDTO() { error_message = x.First["message"].ToString() } } ; } } return(new SectionDTO() { error_message = e.ToString() }); } return(new SectionDTO() { error_message = e.ToString() }); } } logger.Info("SectionController/Create - Task 'Create section' FINISHED"); return(null); }
public object Create([FromBody] Course courseDTO) { logger.Info("CourseController/Create - Task 'Create course' STARTED"); if (courseDTO != null) { try { string ambientPrefix = WebConfigurationManager.AppSettings["AMBIENT_PREFIX"]; WebRequest request = WebRequest.Create(WebConfigurationManager.AppSettings[ambientPrefix + "_SERVICE_BASE_URL"] + @"/api/lms/v1/courses"); request.Method = "POST"; request.Headers.Add(HttpRequestHeader.Authorization, SessionController.GetToken()); bool import_content = courseDTO.course.import_content.HasValue ? courseDTO.course.import_content.Value : false; string postData = "{" + "\"course\": " + "{" + "\"sis_course_id\":\"" + courseDTO.course.sis_course_id + "\"," + "\"account_id\":" + (courseDTO.course.account_id.HasValue ? courseDTO.course.account_id.Value : 1) + "," + "\"name\":\"" + courseDTO.course.name + "\"," + "\"code\":\"" + courseDTO.course.code + "\"," + "\"end_at\":" + (courseDTO.course.end_at != null ? ("\"" + courseDTO.course.end_at + "\"") : "null") + "," + "\"start_at\":" + (courseDTO.course.start_at != null ? ("\"" + courseDTO.course.start_at + "\"") : "null") + "," + "\"restrict_to_dates\":" + "true" + (courseDTO.course.sis_master_id != null ? ("," + "\"sis_master_id\":\"" + courseDTO.course.sis_master_id + "\",") : "") + (courseDTO.course.term_id != null ? ("\"sis_term_id\":\"" + courseDTO.course.term_id + "\"") : "") + "}," + "\"import_content\":" + import_content.ToString().ToLower() + "," + "\"publish\":" + "true" + "}"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "application/json"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); object rtn = JsonConvert.DeserializeObject <CourseReturn>(reader.ReadToEnd()); if (rtn != null) { logger.Info("CourseController/Create - Task 'Create course' STARTED"); return(rtn); } rtn = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (rtn != null) { ErrorMessage errorMessageDto = JsonConvert.DeserializeObject <ErrorMessage>(reader.ReadToEnd()); if (errorMessageDto != null) { logger.Info("CourseController/Create - Task 'Create course' FINISHED"); return(new CourseReturn() { error_message = errorMessageDto.errors.First().message }); } logger.Info("CourseController/Create - Task 'Create course' FINISHED"); return(null); } logger.Info("CourseController/Create - Task 'Create course' FINISHED"); return(null); } catch (WebException e) { logger.Error("CourseController/Create - Task 'Create course' FINISHED WITH ERROR: \n " + " Message: " + e.Message + "\nInner Exception: " + e.InnerException); if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { HttpContext.Current.Request.Headers.Remove("Authorization"); HttpContext.Current.Request.Headers.Add("Authorization", SessionController.GetToken()); return(Create(courseDTO)); } if (e.Message.Contains(HttpStatusCode.Unauthorized.ToString())) { var resp = new StreamReader(e.Response.GetResponseStream()).ReadToEnd(); JObject obj = JsonConvert.DeserializeObject <JObject>(resp); if (obj["errors"]["pseudonym"] != null) { foreach (var x in obj["errors"]["pseudonym"].First) { if (x.First["message"] != null) { return new CourseReturn() { error_message = x.First["message"].ToString() } } ; } } return(new CourseReturn() { error_message = e.ToString() }); } throw e; } } logger.Info("CourseController/Create - Task 'Create course' FINISHED"); return(null); }