private void FillCommandParameters(IHypermediaClientCommand commandInstance, JObject jAction, string commandName)
        {
            commandInstance.Name       = commandName;
            commandInstance.CanExecute = true;

            var title = jAction["title"]?.Value <string>();

            if (title == null)
            {
                title = string.Empty;
            }
            commandInstance.Title = title;


            var uri = jAction["href"]?.Value <string>();

            if (uri == null)
            {
                throw new Exception($"Siren action without href: '{commandName}'");
            }
            commandInstance.Uri = new Uri(uri);

            var method = jAction["method"]?.Value <string>();

            if (method == null)
            {
                method = "GET";
            }
            commandInstance.Method = method;

            var jFields = jAction["fields"] as JArray;

            if (!commandInstance.HasParameters && jFields != null)
            {
                throw new Exception($"hypermedia Command '{commandName}' has no parameter but hypermedia document indicates parameters.");
            }
            if (jFields == null)
            {
                if (commandInstance.HasParameters)
                {
                    throw new Exception($"hypermedia Command '{commandName}' has parameter but hypermedia document has not.");
                }

                return;
            }

            foreach (var jField in jFields)
            {
                var parameterDescription = new ParameterDescription();
                parameterDescription.Name    = jField["name"].Value <string>();
                parameterDescription.Type    = jField["type"].Value <string>();
                parameterDescription.Classes = jField["class"]?.Values <string>().ToList(); // todo optional but not save, or check annotation on class

                commandInstance.ParameterDescriptions.Add(parameterDescription);
            }
        }
        public IHypermediaClientCommand Create(Type commandInterfaceType)
        {
            Type lookupType;
            IHypermediaClientCommand instance = null;

            var isGenericType = commandInterfaceType.GetTypeInfo().IsGenericType;

            if (isGenericType)
            {
                var genericTypeDefinition = commandInterfaceType.GetGenericTypeDefinition();
                var genericTypeArguments  = commandInterfaceType.GetGenericArguments();

                lookupType = genericTypeDefinition;
                Type commandType;
                if (!InterfaceImplementationLookup.TryGetValue(lookupType, out commandType))
                {
                    throw new Exception($"Requested command interface type not found '{commandInterfaceType.Name}' ");
                }

                var constructedType = commandType.MakeGenericType(genericTypeArguments);
                instance = (IHypermediaClientCommand)Activator.CreateInstance(constructedType);
            }
            else
            {
                lookupType = commandInterfaceType;
                Type commandType;
                if (!InterfaceImplementationLookup.TryGetValue(lookupType, out commandType))
                {
                    throw new Exception($"Requested command interface type not found '{commandInterfaceType.Name}' ");
                }

                instance = (IHypermediaClientCommand)Activator.CreateInstance(commandType);
            }

            return(instance);
        }
Beispiel #3
0
        private void FillCommandParameters(
            IHypermediaClientCommand commandInstance,
            IToken action,
            string commandName,
            IHypermediaResolver resolver)
        {
            commandInstance.Name       = commandName;
            commandInstance.CanExecute = true;
            commandInstance.Resolver   = resolver;

            var title = action["title"]?.ValueAsString();

            if (title == null)
            {
                title = string.Empty;
            }
            commandInstance.Title = title;


            var uri = action["href"]?.ValueAsString();

            if (uri == null)
            {
                throw new Exception($"Siren action without href: '{commandName}'");
            }
            commandInstance.Uri = new Uri(uri);

            var method = action["method"]?.ValueAsString();

            if (method == null)
            {
                method = "GET";
            }
            commandInstance.Method = method;

            var fields = action["fields"];

            if (!commandInstance.HasParameters && fields != null)
            {
                throw new Exception($"hypermedia Command '{commandName}' has no parameter but hypermedia document indicates parameters.");
            }
            if (fields == null)
            {
                if (commandInstance.HasParameters)
                {
                    throw new Exception($"hypermedia Command '{commandName}' has parameter but hypermedia document has not.");
                }

                return;
            }

            foreach (var field in fields)
            {
                var parameterDescription = new ParameterDescription
                {
                    Name    = field["name"].ValueAsString(),
                    Type    = field["type"].ValueAsString(),
                    Classes = field["class"]?.ChildrenAsStrings().ToList(),
                };
                // todo optional but not save, or check annotation on class

                commandInstance.ParameterDescriptions.Add(parameterDescription);
            }
        }