Beispiel #1
0
 public RunInstancesResponse RunInstances(RunInstancesRequest request)
 {
     return Util.RetryMethod<RunInstancesResponse>(() => DoRunInstances(request), RetryCount);
 }
Beispiel #2
0
        public RunInstancesResponse DoRunInstances(RunInstancesRequest request)
        {
            List<string> lParams = new List<string>();

            lParams.Add(string.Format("AWSAccessKeyId={0}", Util.UrlEncode(AccessKey)));
            lParams.Add(string.Format("Action={0}", "RunInstances"));
            lParams.Add(string.Format("SignatureMethod={0}", "HmacSHA256"));
            lParams.Add(string.Format("SignatureVersion={0}", "2"));
            lParams.Add(string.Format("Timestamp={0}", Util.UrlEncode(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"))));
            lParams.Add(string.Format("Version={0}", "2012-07-20"));

            var propValues = typeof(RunInstancesRequest).GetProperties();

            for (int i = 0; i < propValues.Length; i++)
            {
                if (propValues[i].PropertyType.IsValueType || propValues[i].PropertyType == typeof(string))
                {
                    var value = propValues[i].GetValue(request);

                    if (propValues[i].PropertyType == typeof(bool))
                        value = value.ToString().ToLower();

                    if (value != null)
                    {
                        FieldInfo info = propValues[i].PropertyType.GetField(value.ToString());

                        if (info != null && info.IsDefined(typeof(XmlEnumAttribute), false))
                        {
                            object[] o = info.GetCustomAttributes(typeof(XmlEnumAttribute), false);
                            XmlEnumAttribute att = (XmlEnumAttribute)o[0];
                            value = att.Name;
                        }

                        lParams.Add(string.Format("{0}={1}", propValues[i].Name, Util.UrlEncode(value.ToString())));
                    }
                }
                else if (propValues[i].PropertyType == typeof(List<string>))
                {
                    var value = (List<string>)propValues[i].GetValue(request);

                    for (int a = 0; a < value.Count; a++)
                    {
                        lParams.Add(string.Format("{0}.{1}={2}", propValues[i].Name, a + 1, Util.UrlEncode(value[a])));
                    }
                }
            }

            lParams.Sort(StringComparer.Ordinal);

            var parameters = string.Join("&", lParams);

            string sig = Util.GetSignature(URL, "GET", parameters, SecretKey);
            parameters = string.Format("{0}&Signature={1}", parameters, Util.UrlEncode(sig));

            var wRequest = WebRequest.Create(string.Format("{0}?{1}", URL, parameters)) as HttpWebRequest;
            wRequest.Method = "GET";
            wRequest.ContentType = "application/x-www-form-urlencoded";
            wRequest.KeepAlive = false;

            using (var response = wRequest.GetResponse() as HttpWebResponse)
            using (var stream = response.GetResponseStream())
            using (var reader = new StreamReader(stream))
            {
                var body = reader.ReadToEnd();
                var element = XElement.Parse(body);

                XmlSerializer serializer = new XmlSerializer(typeof(RunInstancesResponse));
                var runInstancesResponse = (RunInstancesResponse)serializer.Deserialize(element.CreateReader());
                return runInstancesResponse;
            }
        }