Inheritance: RestSharp.IAuthenticator
        private WhoAmIUser WhoAmI( )
        {
            if( m_valenceUserContext == null ) {
                throw new Exception( "This method can only be used for an authenticated user" );
            }

            var client = new RestClient( "https://" + LMS_URL );
            var authenticator = new ValenceAuthenticator( m_valenceUserContext );
            var request = new RestRequest( WHOAMI_ROUTE );
            authenticator.Authenticate( client, request );
            var response = client.Execute<WhoAmIUser>( request );

            return response.Data;
        }
Exemplo n.º 2
0
        public bool DeleteUser(int OrgUnitID, int UserID)
        {
            m_valenceUserContext = m_valenceAppContext.CreateUserContext(m_userId, m_userKey, m_valenceHost);
            Models.Enrollment.EnrollmentData rtnValue = new Models.Enrollment.EnrollmentData();

            var client = new RestClient("https://" + LMS_URL);
            var authenticator = new ValenceAuthenticator(m_valenceUserContext);

            string url = string.Format(USER_ENROLL_REMOVE, OrgUnitID.ToString(), UserID.ToString());
            var request = new RestRequest(url, Method.DELETE);

            authenticator.Authenticate(client, request);
            var response = client.Execute(request);

            return true;
        }
Exemplo n.º 3
0
        private static int Main( string[] args )
        {
            var options = new Options();
            try {
                if (!Parser.Default.ParseArguments( args, options )) {
                    return -1;
                }
            }
            catch (TargetInvocationException e) {
                Console.Error.Write( options.GetUsage() );
                Console.Error.WriteLine( e.InnerException.InnerException.Message );
                return -2;
            }

            var host = new Uri( options.Host );
            var appContextFactory = new D2LAppContextFactory();
            var appContext = appContextFactory.Create( options.AppId, options.AppKey );
            var userContext = appContext.CreateAnonymousUserContext( new HostSpec( host.Scheme, host.Host, host.Port ) );

            const int MAX_ATTEMPTS = 3;
            var attempts = 0;
            IRestResponse response;
            bool again;
            do {
                if (options.Verbose && attempts != 0) {
                    Console.WriteLine( "Making attempt #" + ( attempts + 1 ) );
                }
                var client = new RestClient( host.ToString() );
                var authenticator = new ValenceAuthenticator( userContext );
                var request = new RestRequest( VERSIONS_ROUTE, Method.GET );
                authenticator.Authenticate( client, request );

                response = client.Execute( request );

                again = false;
                if (response.StatusCode == HttpStatusCode.Forbidden && response.Content.StartsWith( "Timestamp out of range" )) {
                    var serverTime = 1000*long.Parse( new string( response.Content.SkipWhile( x => !char.IsNumber( x ) ).ToArray() ) );
                    userContext.ServerSkewMillis = serverTime - GetTime();
                    again = true;
                }

                attempts++;
            } while (again & attempts < MAX_ATTEMPTS);

            if (options.Verbose && attempts == MAX_ATTEMPTS) {
                Console.WriteLine( "Too much timestamp skew, giving up." );
            }

            if (response.StatusCode == HttpStatusCode.OK) {
                try {
                    JsonConvert.DeserializeObject<List<VersionsItem>>( response.Content );
                }
                catch (JsonSerializationException e) {
                    Console.Error.WriteLine( "Call succeeded but could not deserialize the response." );
                    Console.Error.Write( "Error: " );
                    Console.Error.WriteLine( Indent( e.Message ) );
                    if (!string.IsNullOrEmpty( response.Content )) {
                        Console.Error.WriteLine( "Response:" );
                        Console.Error.WriteLine( Indent( response.Content ) );
                    }
                    return -3;
                }
                Console.WriteLine( "Ok" );
                if (options.Verbose) {
                    Console.WriteLine( "Response headers:" );
                    PrintHeaders( Console.Out, response );
                    Console.WriteLine( "Response body:" );
                    Console.WriteLine( Indent( response.Content ) );
                }
                return 0;
            }

            if (options.Guess) {
                if (response.StatusCode == HttpStatusCode.Forbidden && response.Content == "Invalid token") {
                    Console.WriteLine( "App not synced to LMS or explicitly denied in Manage Extensibility." );
                }
                else if (response.StatusCode == HttpStatusCode.Forbidden && response.Content.StartsWith( "Timestamp out of range" )) {
                    Console.WriteLine( "Timestamp skew could not be rectified." );
                }
                else if (!string.IsNullOrEmpty( response.ErrorMessage )) {
                    Console.WriteLine( response.ErrorMessage );
                }
                else {
                    Console.WriteLine( "Unknown error" );
                }
            }
            else {
                Console.Error.WriteLine( "Failure!" );
                if (response.ErrorMessage != null) {
                    Console.Error.WriteLine( "Error: " );
                    Console.Error.WriteLine( Indent( response.ErrorMessage ) );
                }
                if (response.StatusDescription != null) {
                    Console.Error.WriteLine( "Response status: " );
                    Console.Error.WriteLine( Indent( response.StatusDescription ) );
                }
                if (response.Headers != null) {
                    PrintHeaders( Console.Error, response );
                }
                if (!String.IsNullOrEmpty( response.Content )) {
                    Console.Error.WriteLine( "Response: " );
                    Console.Error.WriteLine( Indent( response.Content ) );
                }
            }
            return -4;
        }
Exemplo n.º 4
0
        public Models.Enrollment.EnrollmentData EnrollUser(int OrgUnitID, int UserID, int RoleId)
        {
            m_valenceUserContext = m_valenceAppContext.CreateUserContext(m_userId, m_userKey, m_valenceHost);
            Models.Enrollment.EnrollmentData rtnValue = new Models.Enrollment.EnrollmentData();

            Models.Enrollment.CreateEnrollmentData model =
                new Models.Enrollment.CreateEnrollmentData(OrgUnitID, UserID, RoleId );

            var client = new RestClient("https://" + LMS_URL);
            var authenticator = new ValenceAuthenticator(m_valenceUserContext);

            var request = new RestRequest( USER_CREATE_ENROLLMENT, Method.POST);

            RestSharp.Parameter p = new Parameter ();
            p.Type = ParameterType.RequestBody;
            p.Name = "CreateEnrollmentData";
            p.Value = SimpleJson.SerializeObject(model);

            request.AddParameter(p);

            authenticator.Authenticate(client, request);
            var response = client.Execute (request);

            return null;
        }
Exemplo n.º 5
0
        public bool UpdateCourseActivation(string orgUnitId, bool Activation)
        {
            // Get Course Object
            Models.Course.CourseOffering course = GetCourseOffering(orgUnitId);

            Models.Course.CourseOfferingInfo model = new Models.Course.CourseOfferingInfo(course);

            model.IsActive = Activation.ToString();

            m_valenceUserContext = m_valenceAppContext.CreateUserContext(m_userId, m_userKey, m_valenceHost);

            var client = new RestClient("https://" + LMS_URL);
            var authenticator = new ValenceAuthenticator(m_valenceUserContext);

            var request = new RestRequest(string.Format(COURSE_UPDATE,orgUnitId), Method.PUT );

            RestSharp.Parameter p = new Parameter();
            p.Type = ParameterType.RequestBody;
            p.Name = "CourseOfferingInfo";
            p.Value = SimpleJson.SerializeObject(model);

            request.AddParameter(p);

            authenticator.Authenticate(client, request);
            var response = client.Execute(request);

            return true;
        }
Exemplo n.º 6
0
        public object GetUserEnrollments(string userName)
        {
            int userOrgID = GetUserData(userName).UserId;
            var client = new RestClient("https://" + LMS_URL);
            var authenticator = new ValenceAuthenticator(m_valenceUserContext);

            string bookmark = "";
            List<Models.Enrollment.UserOrgUnit > enrollments = new List<Models.Enrollment.UserOrgUnit> ();

            do
            {
                string reqStr = string.Format(USER_ENROLLMENTS, userOrgID.ToString());
                if (bookmark != "")
                    reqStr += "?bookmark=" + bookmark;

                var request = new RestRequest(reqStr);
                authenticator.Authenticate(client, request);

                var response = client.Execute<Dictionary<string, string>>(request);

                Models.API.PagingInfo pageInfo = SimpleJson.DeserializeObject<Models.API.PagingInfo>(response.Data["PagingInfo"]);
                List<Models.Enrollment.UserOrgUnit> items = SimpleJson.DeserializeObject<List<Models.Enrollment.UserOrgUnit>>(response.Data["Items"]);
                enrollments.AddRange(items);
                if (pageInfo.HasMoreItems)
                    bookmark = pageInfo.Bookmark;
                else bookmark = "";

            } while (bookmark != "");

            return enrollments ;
        }
Exemplo n.º 7
0
        public Models.User.UserData GetUserData(string userName)
        {
            var client = new RestClient("https://" + LMS_URL);
            var authenticator = new ValenceAuthenticator(m_valenceUserContext);

            var request = new RestRequest(string.Format(USERLOOKUP_USERNAME_ROUTE, userName));
            authenticator.Authenticate(client, request);
            var response = client.Execute<Models.User.UserData>(request);

            return response.Data;
        }
Exemplo n.º 8
0
        public string GetFinalGrade(int OrgUnitID, int UserId)
        {
            var client = new RestClient("https://" + LMS_URL);
            var authenticator = new ValenceAuthenticator(m_valenceUserContext);

            var request = new RestRequest(string.Format(USER_GRADE_FINAL , OrgUnitID .ToString (),UserId.ToString ()));
            authenticator.Authenticate(client, request);
            var response = client.Execute<Models.Grade .GradeValue>(request);

            return response.Data.DisplayedGrade;
        }
Exemplo n.º 9
0
        public Models.Course.CourseOffering GetCourseOffering(string orgUnitID)
        {
            var client = new RestClient("https://" + LMS_URL);
            var authenticator = new ValenceAuthenticator(m_valenceUserContext);

            var request = new RestRequest(string.Format(COURSE_LOOKUP, orgUnitID));
            authenticator.Authenticate(client, request);
            var response = client.Execute<Models.Course.CourseOffering>(request);

            return response.Data;
        }