예제 #1
0
    private void Execute(FrameworkElement sender)
    {
      string url = Url;
      if (IsHRefTemplate)
      {
        try
        {
          JObject json = JsonConvert.DeserializeObject(Body) as JObject;
          if (json != null)
          {
            Resta.UriTemplates.UriTemplate t = new Resta.UriTemplates.UriTemplate(Url);
            IDictionary<string, object> parameters = new JsonObjectDictionary(json);
            url = t.Resolve(parameters);
          }
        }
        catch (Exception)
        {
          MessageBox.Show(GetOwnerWindow(), "Invalid URL template or template values");
          return;
        }
      }

      try
      {
        Uri tmp = new Uri(Url);
      }
      catch (Exception)
      {
        MessageBox.Show(GetOwnerWindow(), "Invalid URL");
        return;
      }

      if (string.IsNullOrEmpty(Method))
      {
        MessageBox.Show(GetOwnerWindow(), "Missing HTTP method");
        return;
      }

      ISession session = RamoneServiceManager.Session;

      Request req = session.Bind(url).Method(Method);

      if (Headers != null)
      {
        foreach (string line in Headers.Split('\n'))
        {
          int colonPos = line.IndexOf(':');
          if (colonPos > 0)
          {
            string header = line.Substring(0, colonPos).Trim();
            string value = line.Substring(colonPos + 1).Trim();
            if (!string.IsNullOrEmpty(header) && !string.IsNullOrEmpty(value))
            {
              if (header.Equals("accept", StringComparison.InvariantCultureIgnoreCase))
                req.Accept(value);
              else if (!header.Equals("content-type", StringComparison.InvariantCultureIgnoreCase))
                req.Header(header, value);
            }
          }
        }
      }

      if (SelectedType == MasonProperties.EncodingTypes.JSON && Body != null)
      {
        req.AsJson();
        req.Body(Body);
      }
      else if (SelectedType == MasonProperties.EncodingTypes.JSONFiles)
      {
        req.AsMultipartFormData();
        Hashtable files = new Hashtable();
        if (Body != null && !string.IsNullOrEmpty(JsonPartName))
          files[JsonPartName] = new Ramone.IO.StringFile { Filename = JsonPartName, ContentType = "application/json", Data = Body };
        foreach (ComposerFileViewModel file in Files)
        {
          if (!string.IsNullOrEmpty(file.Name) && !string.IsNullOrEmpty(file.Filename) && System.IO.File.Exists(file.Filename))
            files[file.Name] = new Ramone.IO.File(file.Filename);
        }
        req.Body(files);
      }

      Window w = Window.GetWindow(sender as DependencyObject);

      Publish(new ExecuteWebRequestEventArgs(session, req) { OnSuccess = (r => HandleSuccess(r, w)) });
    }
예제 #2
0
        private void Execute(FrameworkElement sender)
        {
            string url = Url;

            if (IsHRefTemplate)
            {
                try
                {
                    JObject json = JsonConvert.DeserializeObject(Body) as JObject;
                    if (json != null)
                    {
                        Resta.UriTemplates.UriTemplate t          = new Resta.UriTemplates.UriTemplate(Url);
                        IDictionary <string, object>   parameters = new JsonObjectDictionary(json);
                        url = t.Resolve(parameters);
                    }
                }
                catch (Exception)
                {
                    MessageBox.Show(GetOwnerWindow(), "Invalid URL template or template values");
                    return;
                }
            }

            try
            {
                Uri tmp = new Uri(Url);
            }
            catch (Exception)
            {
                MessageBox.Show(GetOwnerWindow(), "Invalid URL");
                return;
            }

            if (string.IsNullOrEmpty(Method))
            {
                MessageBox.Show(GetOwnerWindow(), "Missing HTTP method");
                return;
            }

            ISession session = RamoneServiceManager.Session;

            Request req = session.Bind(url).Method(Method);

            if (Headers != null)
            {
                foreach (string line in Headers.Split('\n'))
                {
                    int colonPos = line.IndexOf(':');
                    if (colonPos > 0)
                    {
                        string header = line.Substring(0, colonPos).Trim();
                        string value  = line.Substring(colonPos + 1).Trim();
                        if (!string.IsNullOrEmpty(header) && !string.IsNullOrEmpty(value))
                        {
                            if (header.Equals("accept", StringComparison.InvariantCultureIgnoreCase))
                            {
                                req.Accept(value);
                            }
                            else if (!header.Equals("content-type", StringComparison.InvariantCultureIgnoreCase))
                            {
                                req.Header(header, value);
                            }
                        }
                    }
                }
            }

            if (SelectedType == MasonProperties.EncodingTypes.JSON && Body != null)
            {
                req.AsJson();
                req.Body(Body);
            }
            else if (SelectedType == MasonProperties.EncodingTypes.JSONFiles)
            {
                req.AsMultipartFormData();
                Hashtable files = new Hashtable();
                if (Body != null && !string.IsNullOrEmpty(JsonPartName))
                {
                    files[JsonPartName] = new Ramone.IO.StringFile {
                        Filename = JsonPartName, ContentType = "application/json", Data = Body
                    }
                }
                ;
                foreach (ComposerFileViewModel file in Files)
                {
                    if (!string.IsNullOrEmpty(file.Name) && !string.IsNullOrEmpty(file.Filename) && System.IO.File.Exists(file.Filename))
                    {
                        files[file.Name] = new Ramone.IO.File(file.Filename);
                    }
                }
                req.Body(files);
            }

            Window w = Window.GetWindow(sender as DependencyObject);

            Publish(new ExecuteWebRequestEventArgs(session, req)
            {
                OnSuccess = (r => HandleSuccess(r, w))
            });
        }