Exemplo n.º 1
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="_s"></param>
        /// <param name="_file"></param>
        public static bool ProcessApiaryFile(Server _s, String _file)
        {
            // Read file to variable
            String content = "";

            try
            {
                using (StreamReader sr = new StreamReader(_file))
                {
                    content = sr.ReadToEnd();
                }
            }
            catch
            {
                MessageDialog.ShowError(Director.Properties.Resources.CannotReadFromFile);
                return(false);
            }

            // Parse content
            ApiaryServer s;

            try
            {
                s = JsonConvert.DeserializeObject <ApiaryServer>(content);
            }
            catch
            {
                MessageDialog.ShowError(Director.Properties.Resources.InvalidApiaryFile);
                return(false);
            }

            // Set URL
            String url = "";

            while (true)
            {
                var d = new Dialog();
                d.Title = Director.Properties.Resources.FillEndPointUrl;
                d.Width = 340;
                d.Icon  = Image.FromResource(DirectorImages.EDIT_ICON);


                VBox DialogContent = new VBox();
                DialogContent.PackStart(new Label(Director.Properties.Resources.FillEndPointUrl));
                TextEntry NewValidUrl = new TextEntry();
                if (url.Length == 0)
                {
                    NewValidUrl.Text = "http://example.com";
                }
                DialogContent.PackStart(NewValidUrl, true, true);

                d.Buttons.Add(new DialogButton(Director.Properties.Resources.ButtonOK, Command.Ok));
                d.Buttons.Add(new DialogButton(Director.Properties.Resources.ButtonStorno, Command.Cancel));
                d.Content = DialogContent;

                var c = d.Run();

                if (c == Command.Ok)
                {
                    Uri _newUri;

                    // Uri validation
                    if (Uri.TryCreate(NewValidUrl.Text, UriKind.Absolute, out _newUri))
                    {
                        url = _newUri.AbsoluteUri;
                        d.Dispose();
                        break;
                    }
                    else
                    {
                        MessageDialog.ShowError(Director.Properties.Resources.InvalidUrlError);
                    }
                }
                else
                {
                    d.Dispose();
                    return(false);
                }
                d.Dispose();
            }

            // Fill data
            if (_s.Name == null || _s.Name.Length == 0)
            {
                _s.Name = s.name;
            }

            if (_s.Url == null || _s.Url.Length == 0)
            {
                _s.Url = url;
            }

            // Z resources group vyrobit scenare
            foreach (var group in s.resourceGroups)
            {
                var scNew = _s.CreateNewScenario();
                scNew.Name = group.name;

                // Z akcí - example vyrobit requesty
                foreach (var resource in group.resources)
                {
                    foreach (var action in resource.actions)
                    {
                        var reqNew = scNew.CreateNewRequest();
                        reqNew.Name        = string.Format("{0}/{1}", resource.name, action.name);
                        reqNew.HTTP_METHOD = action.method;

                        // Create URL
                        if (resource.uriTemplate.StartsWith("/") && url.EndsWith("/"))
                        {
                            reqNew.Url = url.Substring(0, url.Length - 1) + resource.uriTemplate;
                        }
                        else
                        {
                            reqNew.Url = url + resource.uriTemplate;
                        }

                        // Get first request
                        var req = (action.examples.Count > 0 && action.examples[0].requests.Count > 0) ? action.examples[0].requests[0] : null;
                        var res = (action.examples.Count > 0 && action.examples[0].responses.Count > 0) ? action.examples[0].responses[0] : null;

                        if (req != null)
                        {
                            // Content type
                            reqNew.RequestTemplateType = ContentTypeUtils.GuessContentType(req.body);
                            reqNew.RequestTemplate     = req.body;

                            // Headers
                            foreach (var h in req.headers)
                            {
                                reqNew.Headers.Add(new Header()
                                {
                                    Name = h.name, Value = h.value
                                });
                            }
                        }

                        if (res != null)
                        {
                            // Content type
                            reqNew.ResponseTemplateType = ContentTypeUtils.GuessContentType(res.body);
                            // Set body
                            reqNew.ResponseTemplate = res.body;
                        }
                    }
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="_s"></param>
        /// <param name="_file"></param>
        public static bool ProcessPostmanFile(Server _s, String _file)
        {
            // Read file to variable
            String content = "";

            try
            {
                using (StreamReader sr = new StreamReader(_file))
                {
                    content = sr.ReadToEnd();
                }
            }
            catch
            {
                MessageDialog.ShowError(Director.Properties.Resources.CannotReadFromFile);
                return(false);
            }

            PostmanServer s = new PostmanServer();

            try
            {
                JObject o = JObject.Parse(content);
                s.name     = (String)o["name"];
                s.requests = new List <PostmanRequest>();

                foreach (JObject re in o["requests"])
                {
                    var tmp = new PostmanRequest();
                    tmp.name    = (String)re["name"];
                    tmp.method  = (String)re["method"];
                    tmp.url     = (String)re["url"];
                    tmp.headers = (String)re["headers"];

                    // Data
                    if (((String)re["dataMode"]) == "raw")
                    {
                        tmp.data = (String)re["data"];
                    }

                    s.requests.Add(tmp);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                MessageDialog.ShowError(Director.Properties.Resources.InvalidPostmanFile);
                return(false);
            }

            // Set server header?
            if (_s.Name == null || _s.Name.Length == 0)
            {
                _s.Name = s.name;
            }

            if (_s.Url == null || _s.Url.Length == 0)
            {
                _s.Url = s.requests.Count > 0 ? s.requests[0].url : "";
            }

            // Create scenario
            var scNew = _s.CreateNewScenario();

            scNew.Name = s.name;

            foreach (var pr in s.requests)
            {
                var tmp = scNew.CreateNewRequest();
                tmp.Name                = pr.name;
                tmp.HTTP_METHOD         = pr.method;
                tmp.RequestTemplateType = ContentTypeUtils.GuessContentType(pr.data);
                tmp.RequestTemplate     = pr.data;
                tmp.Url = pr.url;

                // Create headers
                foreach (var h in pr.HeaderList)
                {
                    tmp.Headers.Add(new Header()
                    {
                        Name = h.name, Value = h.value
                    });
                }
            }

            return(true);
        }