Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Creating a login request for enviso.");

            Console.WriteLine($"Please fill in your APIKEY: {Environment.NewLine} eg:{Environment.NewLine}{defaultApiKey}");
            var apikey = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(apikey))
            {
                apikey = defaultApiKey;
            }
            ;

            Console.WriteLine($"Please fill in your Public RSA Key: {Environment.NewLine}eg: {Environment.NewLine}{defaultPublicRsaKey}");
            var rsaKey = Console.ReadLine();

            if (string.IsNullOrWhiteSpace(rsaKey))
            {
                rsaKey = defaultPublicRsaKey;
            }

            Library.LoginGenerator generator = new Library.LoginGenerator();
            var loginRequest = generator.GenerateLogin(apikey, rsaKey);

            Console.WriteLine("The Loginrequest is : ");
            var serializedRequest = Newtonsoft.Json.JsonConvert.SerializeObject(loginRequest);

            Console.WriteLine(serializedRequest);

            using (var httpClient = new HttpClient())
            {
                var content = new StringContent(serializedRequest, Encoding.UTF8, "application/json");
                Console.WriteLine("Executing login: . . . ");
                var postTask = Task.Run(async() => await httpClient.PostAsync("https://api.staging-enviso.io/resellingapi/v1/apis/login", content));
                postTask.Wait(TimeSpan.FromSeconds(3));
                if (postTask.IsCompletedSuccessfully)
                {
                    var httpResponseMessage = postTask.Result;
                    Console.WriteLine($"{httpResponseMessage.StatusCode.ToString()} - {httpResponseMessage.Content.ReadAsStringAsync().Result}");
                }
                else
                {
                    Console.WriteLine($"An error occured: {postTask.Exception.ToString()}");
                }
            }

            Console.WriteLine("Press any key to close . . .");
            Console.ReadLine();
        }
        /// <summary>
        /// Initializing the client will login into Enviso.
        /// </summary>
        public async Task Initialize()
        {
            Library.LoginGenerator generator = new Library.LoginGenerator();
            var loginRequest = generator.GenerateLogin(ApiKey, RsaKey);

            var serializedRequest = Newtonsoft.Json.JsonConvert.SerializeObject(loginRequest);

            // we shouldn't create a HTTPclient for each call but share this accross different calls.
            // For simplicity of the sample code this  is kept as an optimization.
            using (var httpClient = new HttpClient())
            {
                var content       = new StringContent(serializedRequest, Encoding.UTF8, "application/json");
                var loginResponse = await httpClient.PostAsync(URI.LOGIN, content);

                var loginResponseDTO = await ReadResponseAndDeserialize <LoginResponseDTO>(loginResponse);

                this._authToken = loginResponseDTO.AuthToken;
            }
            _isInitialized = true;
        }