예제 #1
0
        private async Task <DeviceConnectionStatus> DeviceCheckConnectionRequest(HttpClient client, string publicKey)
        {
            HttpResponseMessage response = null;

            Console.WriteLine("Checking for device connection request..");
            response = await client.PostAsJsonAsync(apiBaseAddress + "DeviceCheckRemoteConnectionRequest", publicKey);

            if (response.IsSuccessStatusCode)
            {
                bool authenticated = hmacResponseAuthenticator.IsResponseAuthenticated(response);
                if (authenticated)
                {
                    string responseString = await response.Content.ReadAsStringAsync();

                    DeviceConnectionStatus currentStatus = Newtonsoft.Json.JsonConvert.DeserializeObject <DeviceConnectionStatus>(responseString);
                    Console.WriteLine(responseString);

                    return(currentStatus);
                }
            }
            else
            {
                await PrintFailMessage(response);
            }

            return(null);
        }
예제 #2
0
        private async Task <string> DeveloperConnectionRequest(HttpClient client)
        {
            HttpResponseMessage response = null;

            string deviceName = Configuration.Instance.TargetDevice;

            response = await client.PostAsJsonAsync(apiBaseAddress + "DeveloperDeviceConnectionRequest", deviceName);

            if (response.IsSuccessStatusCode)
            {
                bool authenticated = hmacAuthenticator.IsResponseAuthenticated(response);
                if (authenticated)
                {
                    string responseString = await response.Content.ReadAsStringAsync();

                    return(responseString);
                }
            }
            else
            {
                await PrintFailMessage(response);
            }

            return(null);
        }
예제 #3
0
        static async Task RunAsync()
        {
            Console.WriteLine("Calling the back-end API");

            HMACDelegatingHandler customDelegatingHandler = new HMACDelegatingHandler(false);

            HttpClient client = HttpClientFactory.Create(customDelegatingHandler);

            ConsoleKey          key      = ConsoleKey.S;
            HttpResponseMessage response = null;

            HMACResponseAuthentication hmacResponseAuthenticator = new HMACResponseAuthentication(true, false);


            while (true)
            {
                // azioni developer usano un'api key developer
                if (key == ConsoleKey.S || key == ConsoleKey.P || key == ConsoleKey.O || key == ConsoleKey.I)
                {
                    HMACDelegatingHandler.ClientId  = "378ce77c-5b45-4126-9dfa-0371daa51563";
                    HMACDelegatingHandler.ClientKey = "anI4ICTj9bs+gNQRa3aBbbQmsYCGvNIKB1qTkWZoj/k=";
                }

                if (key == ConsoleKey.Q || key == ConsoleKey.W || key == ConsoleKey.E)
                {
                    // azioni del device usano un'api key device
                    HMACDelegatingHandler.ClientId  = "50148590-1b48-4cf5-a76d-8a7f9474a3de";
                    HMACDelegatingHandler.ClientKey = "U8a2xaaYz2sNhEGDO9T4Ms9Wf4AWMQv+gDpmYJx+YmI=";
                }


                if (key == ConsoleKey.S)
                {
                    response = await TestProtocol(client);
                }

                else if (key == ConsoleKey.P)
                {
                    response = await DeveloperConnectionRequest_FAIL(client);
                }

                else if (key == ConsoleKey.O)
                {
                    response = await DeveloperConnectionRequest_DONE(client);
                }

                else if (key == ConsoleKey.I)
                {
                    response = await DeveloperCheckDeviceConnectionState(client);
                }

                else if (key == ConsoleKey.Q)
                {
                    response = await DeviceCheckConnectionRequest(client);
                }

                else if (key == ConsoleKey.W)
                {
                    response = await DeviceSetActiveConnectionStatus(client);
                }

                if (response.IsSuccessStatusCode)
                {
                    bool authenticated = hmacResponseAuthenticator.IsResponseAuthenticated(response);
                    if (authenticated)
                    {
                        string responseString = await response.Content.ReadAsStringAsync();

                        Console.WriteLine(responseString);
                    }
                }
                else
                {
                    Console.WriteLine("Failed to call the API. HTTP Status: {0}, Reason {1}", response.StatusCode, response.ReasonPhrase);
                    string responseString = await response.Content.ReadAsStringAsync();

                    Console.WriteLine(responseString);
                }

                key = Console.ReadKey(true).Key;
                Console.WriteLine("-----------------------------------------------------------");
            }
        }