Пример #1
0
            public void Run()
            {
                LaunchKeyRestClient client = new LaunchKeyRestClient(
                    this.appKey,
                    this.secretKey,
                    this.privateRsaKey
                    );

                var pingResponse = client.Ping();

                if (!pingResponse.Successful)
                {
                    Console.WriteLine("ERROR: Error getting ping response.");
                    return;
                }

                Console.WriteLine("SUCCESS: Received public key from API.");

                string username;

                do
                {
                    Console.Write("Enter username to authenticate as:");
                    username = Console.ReadLine();
                } while (string.IsNullOrWhiteSpace(username));

                var authResponse = client.Authenticate(username, AuthenticationType.Session);

                if (!authResponse.Successful)
                {
                    Console.WriteLine("ERROR: Error getting auth response. Error code: {0}", authResponse.StatusCode);
                    return;
                }
                Console.WriteLine("SUCCESS: Authentication request sent to device. Reference code: {0}", authResponse.AuthRequest);

                Console.WriteLine("Beginning poll-wait state");

                PollResponse pollResponse;

                do
                {
                    Thread.Sleep(1000);
                    pollResponse = client.Poll(authResponse.AuthRequest);
                    Console.WriteLine("Poll response: {0}: {1}", pollResponse.MessageCode, pollResponse.Message);
                } while (pollResponse.MessageCode == LaunchKeyResponseCode.PollErrorResponsePending);

                if (!client.IsAuthorized(authResponse.AuthRequest, pollResponse))
                {
                    // rejected!
                    Console.WriteLine("Authentication failed. ", pollResponse.Message);
                    return;
                }

                // Authentication success!
                Console.WriteLine("SUCCESS: {0} authenticated!", username);
                Console.WriteLine("Press enter to dorbit.");
                Console.ReadLine();

                Console.WriteLine("Deorbiting ... ");
                var logsResponse = client.Logout(authResponse.AuthRequest);

                Console.WriteLine("Logs API response: {0} - {1}", logsResponse.MessageCode, logsResponse.Message);
                if (!logsResponse.Successful)
                {
                    Console.WriteLine("Error deorbiting.");
                }
            }