Esempio n. 1
0
        /// <summary>
        /// Sign OVH API REQUEST
        /// </summary>
        public static async Task SignRequestAsync(HttpRequestMessage request, IOvhApiCredentials credentials)
        {
            try
            {
                long   currentTimestamp  = OvhApiTools.Time(); //1608291240  //1608294594
                string requestdatastring = "";
                if (request.Content != null)
                {
                    requestdatastring = await request.Content.ReadAsStringAsync();
                }

                string target = request.RequestUri.ToString();
                if (!target.StartsWith("http"))
                {
                    target = target.TrimStart('/'); target = credentials.Endpoint.ToString() + target;
                }
                string signature = GenerateSignature(credentials.ApplicationSecret, credentials.ConsumerKey, currentTimestamp, request.Method.ToString().ToUpper(), target, requestdatastring);


                //request.Headers.Add("X-UID", "");
                request.Headers.Add(SHARED.OvhConstants.OVH_APP_HEADER, credentials.ApplicationKey);
                request.Headers.Add(SHARED.OvhConstants.OVH_CONSUMER_HEADER, credentials.ConsumerKey);
                request.Headers.Add(SHARED.OvhConstants.OVH_TIME_HEADER, currentTimestamp.ToString());
                request.Headers.Add(SHARED.OvhConstants.OVH_SIGNATURE_HEADER, signature);
                // headers.Add(OVH_BATCH_HEADER, ParameterSeparator.ToString());
            }
            catch (Exception ex)
            {
                throw new Exception("OVH SignRequestAsync " + ex.Message, ex);
            }
        }
Esempio n. 2
0
        public static async Task <TResponseModel> ExecuteWithModelAsync <TResponseModel>(this HttpClient client, IOvhApiCredentials credential, HttpMethod method, string urlPart, Dictionary <string, object> formdata = null, object model = null)
        {
            try
            {
                HttpRequestMessage req = new HttpRequestMessage(method, urlPart);
                if (model != null)
                {
                    req.Content = OvhApiTools.JsonContent(model);
                }
                await OvhApiTools.SignRequestAsync(req, credential);

                HttpResponseMessage resp = await client.SendAsync(req);

                OvhApiTools.Validate(resp);
                TResponseModel retour = await OvhApiTools.ReadWithModelAsync <TResponseModel>(resp);

                return(retour);
            }
            catch (Exception ex)
            {
                throw;
            }
        }