Ejemplo n.º 1
0
        public void Base64Encode_and_Base64Decode_should_generate_correct_Base64_encoding_and_decoding()
        {
            var input = "this is a simple hash: d6991/d5506f*d(d}d1]34?\\89&f949$09@c24!d6c3";

            var encoded = StringUtility.Base64Encode(input);
            var decoded = StringUtility.Base64Decode(encoded);

            Assert.AreEqual(input, decoded);
        }
Ejemplo n.º 2
0
        public static async Task <ProCampaignResponse> PostParticipationAsync(
            string participationData,
            ProCampaignSettings settings,
            bool requestConsumerId = false)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings), "ProCampaign settings cannot be null");
            }

            var apiUrl = new Uri(settings.ConsumerBaseUrl, settings.ParticipationPath);

            using (var client = new HttpClient(httpHandler, false))
            {
                client.BaseAddress = apiUrl;
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(
                    new MediaTypeWithQualityHeaderValue("application/json"));

                if (requestConsumerId)
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                                                                                               $"{StringUtility.Base64Encode($"{settings.ApiKey}:{settings.ApiSecret}")}");
                }

                string query;
                var    queryStrings = requestConsumerId ?
                                      new Dictionary <string, string>
                {
                    { "apiKey", settings.ApiKey },
                    { "requestConsumerId", "true" }
                } :
                new Dictionary <string, string>
                {
                    { "apiKey", settings.ApiKey }
                };

                using (var content = new FormUrlEncodedContent(queryStrings))
                {
                    query = content.ReadAsStringAsync().Result;
                }

                var    data = new StringContent(participationData, Encoding.UTF8, "application/json");
                string result;

                using (var response = await client.PostAsync($"{apiUrl}?{query}", data))
                {
                    using (var content = response.Content)
                    {
                        result = await content.ReadAsStringAsync();
                    }
                }

                return(JsonConvert.DeserializeObject <ProCampaignResponse>(result));
            }
        }