Exemplo n.º 1
0
        private HttpRequestMessage CreateRequest()
        {
            foreach (var input in Inputs)
            {
                if (input.Value == null)
                {
                    input.Value = input.DefaultValue;
                }
            }

            var request        = new HttpRequestMessage(MapApiMethod(), Format(Model.Url));
            var contentHeaders = new List <KeyValuePair <string, string> >();

            foreach (var header in Headers)
            {
                var name  = Format(header.Name);
                var value = Format(header.Value);
                if (HttpHeaders.IsContentHeader(name))
                {
                    contentHeaders.Add(new KeyValuePair <string, string>(name, value));
                }
                else
                {
                    request.Headers.Add(name, value);
                }
            }

            if (Model.Body != null && Model.Method.IsBodyAllowed())
            {
                var contentType = Headers.SingleOrDefault(x => x.Name == ContentTypes.ContentType)?.Value;
                if (contentType != null && ContentTypes.IsText(contentType))
                {
                    var s = Model.Body;
                    s = Format(s);
                    var stringContent = new StringContent(s);
                    request.Content = stringContent;
                }
                else
                {
                    request.Content = new ByteArrayContent(BinaryBody);
                }
                foreach (var header in contentHeaders)
                {
                    if (header.Key == ContentTypes.ContentType)
                    {
                        request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(header.Value);
                    }
                }
            }

            return(request);
        }
Exemplo n.º 2
0
        public ApiModel(MainWindowModel mainWindow, ApiCollectionModel parent, Api api) : base(mainWindow, parent, api)
        {
            Inputs  = new RxList <ApiInputModel>();
            Outputs = new RxList <ApiOutputModel>();
            Headers = new RxList <ApiHeaderModel>();

            SubscribeForInputs(this.ObservePropertyChange(x => x.Model.Url), () => Model.Url, ApiInputType.Url);
            SubscribeForInputs(this.ObservePropertyChange(x => x.Model.Body).Where(x => ContentTypes.IsText(ContentType)), () => Model.Body, ApiInputType.Body);
            SubscribeForInputs(this.ObservePropertyChange(x => x.Headers).SelectMany(x => x.ObserveElementProperty(y => y.Name)).Merge(this.ObservePropertyChange(x => x.Headers).SelectMany(x => x.ObserveElementProperty(y => y.Value))), () => string.Join("\n", Headers.Select(x => x.Name + "=" + x.Value)), ApiInputType.Header);

            Model = api;
            if (api.Inputs != null)
            {
                Inputs.AddRange(api.Inputs.Select(x => new ApiInputModel
                {
                    Id           = x.Id,
                    Name         = x.Name,
                    DefaultValue = x.DefaultValue,
                    InputType    = x.InputType
                }));
            }
            if (api.Outputs != null)
            {
                Outputs.AddRange(api.Outputs.Select(x => new ApiOutputModel
                {
                    Id         = x.Id,
                    Name       = x.Name,
                    Expression = x.Expression,
                    Type       = x.Type
                }));
            }
            if (api.Headers != null)
            {
                Headers.AddRange(api.Headers.Select(x => new ApiHeaderModel
                {
                    Id    = x.Id,
                    Name  = x.Name,
                    Value = x.Value
                }));
            }
            Send  = RxCommand.CreateAsync(OnSend);
            Reset = RxCommand.Create(OnReset);

            Inputs.SetUpSync(
                MainWindow.Repository,
                Model.Inputs,
                x => new ApiInput {
                Name = x.Name, DefaultValue = "", InputType = x.InputType
            });
            Outputs.SetUpSync(
                MainWindow.Repository,
                Model.Outputs,
                x => new ApiOutput {
                Name = x.Name, Expression = ""
            });
            Headers.SetUpSync(
                MainWindow.Repository,
                Model.Headers,
                _ => new ApiHeader {
                Name = "", Value = ""
            });
        }
Exemplo n.º 3
0
 public static bool IsVisualizerVisible(ApiResponseModel response)
 {
     return(ContentTypes.IsText(response.ContentType));
 }
Exemplo n.º 4
0
 public ApiResponseModel()
 {
     stringResponse = new Lazy <string>(() => Response == null || !ContentTypes.IsText(ContentType) ? null : Encoding.UTF8.GetString(Response));
     jsonResponse   = new Lazy <JToken>(() => Response == null || ContentType != ContentTypes.ApplicationJson ? null : JToken.Parse(StringResponse));
 }