コード例 #1
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            foreach (var modelObj in ExtractModels(context))
            {
                var modelType = (string)modelObj.Model.type;
                var isEnum    = "enum".Equals(modelType, StringComparison.InvariantCultureIgnoreCase);

                if (isEnum)
                {
                    var model = ParseEnumData(modelObj);
                    model.ApiNamespace   = context.ApiNamespace;
                    model.ModelNamespace = context.ModelNamespace;
                    ctx.NormalizationContext.Enums.Add(model);
                }
                else
                {
                    var model = ParseModelData(modelObj);
                    model.ApiNamespace   = context.ApiNamespace;
                    model.ModelNamespace = context.ModelNamespace;

                    foreach (var prop in model.Properties)
                    {
                        prop.ApiNamespace   = context.ApiNamespace;
                        prop.ModelNamespace = context.ModelNamespace;
                    }

                    ctx.NormalizationContext.Models.Add(model);
                }
            }

            foreach (var model in ctx.NormalizationContext.Models.Where(x => x.RawSubTypes.Any()))
            {
                foreach (var st in model.RawSubTypes)
                {
                    var sm = ctx.NormalizationContext.Models.FirstOrDefault(x => x.Name.Equals(st, StringComparison.InvariantCultureIgnoreCase));
                    if (sm == null)
                    {
                        continue;
                    }
                    model.SubTypes.Add(sm);
                }
            }

            var enumNames  = ctx.NormalizationContext.Enums.Select(x => x.Name).ToList();
            var modelNames = ctx.NormalizationContext.Models.Select(x => x.Name).ToList();

            // Ensure that Enum Properties are properly indicated
            ctx.NormalizationContext.Models
            .SelectMany(x => x.Properties)
            .Where(x => enumNames.Contains(x.TypeName) && !modelNames.Contains(x.TypeName))
            .ToList()
            .ForEach(x => x.IsEnum = true);

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
コード例 #2
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            foreach (var normalModel in context.NormalizationContext.Models)
            {
                var normalModelName = new NormalModelName(normalModel.ResourcePath, normalModel.Name);
                _modelNameMaps.Add(normalModelName);

                var modelDef = new ModelDefinition(normalModel.AsMetadata())
                {
                    ContextName  = normalModel.ResourceName,
                    Description  = normalModel.Description,
                    Name         = normalModel.Name.MapDataTypeName(),
                    ResourceName = normalModel.ResourcePath.ResourceNameFromPath()
                };
                normalModelName.New = modelDef.Name;

                modelDef.Properties.AddRange(ExtractModelProperties(normalModel.Properties));

                serviceDefinition.AddModel(modelDef);
            }

            var resourceModelLookup = _modelNameMaps.ToLookup(x => x.ResourcePath);

            // Ensure all Operations refer to the proper model names
            foreach (var op in context.NormalizationContext.Operations)
            {
                if (!resourceModelLookup.Contains(op.ResourcePath))
                {
                    continue;
                }

                var resourceModels = resourceModelLookup[op.ResourcePath].ToList();

                var returnItem = resourceModels.SingleOrDefault(x => op.Response.TypeName.Equals(x.Old));
                if (returnItem != null)
                {
                    op.Response.SetTypeName(returnItem.New);
                }

                foreach (var opParam in op.Parameters)
                {
                    var paramItem = resourceModels.SingleOrDefault(x => opParam.TypeName.Equals(x.Old));
                    if (paramItem != null)
                    {
                        opParam.SetTypeName(paramItem.New);
                    }
                }
            }


            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State             = LifecycleState.Continue
            };

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
コード例 #3
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            foreach (var normalOp in context.NormalizationContext.Operations)
            {
                var op = new OperationDefinition20(NormalizeOperationPath(normalOp), normalOp.AsMetadata())
                {
                    ConsumesOctetStream = normalOp.SupportsStreamingUpload,
                    Description         = normalOp.Description,
                    HttpMethod          = normalOp.HttpMethod,
                    Name = ExtractName(normalOp),
                    ProducesOctetStream = normalOp.SupportsStreamingDownload,
                    ResourceName        = normalOp.ResourcePath,
                    Response            = NormalizeResponseDefinition(normalOp)
                };

                normalOp.Parameters
                .Select(NormalizeParameterDefinition)
                .ToList()
                .ForEach(x => op.AddParameter(x));

                serviceDefinition.AddOperation(op);
            }


            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State             = LifecycleState.Continue
            };

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
コード例 #4
0
        public Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            var svcDef = context.ServiceDefinition;
            var enumWriter = context.EnumWriter;
            var modelWriter = context.ModelWriter;

            var sb = new StringBuilder();

            // Enums First
            var template = context.ApiEnumTemplate;
            foreach (var obj in svcDef.Enums.SelectMany(x => x))
            {
                using (var sw = new StringWriter(sb.Clear()))
                {
                    template(sw, obj);
                }

                enumWriter(obj.Name, sb.ToString(), obj);
            }

            // Models Second
            template = context.ApiModelTemplate;
            foreach (var obj in svcDef.Models.SelectMany(x => x))
            {
                using (var sw = new StringWriter(sb.Clear()))
                {
                    template(sw, obj);
                }

                modelWriter(obj.Name, sb.ToString(), obj);
            }

            return Task.FromResult(context);
        }
コード例 #5
0
        public Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            var json = context.ResourceListingJson;

            if (!json.ContainsKey("apiVersion") == null || string.IsNullOrWhiteSpace((string) json.apiVersion))
                throw new SwaseyException("apiVersion is required");

            if (!json.ContainsKey("swaggerVersion") == null || string.IsNullOrWhiteSpace((string) json.swaggerVersion))
                throw new SwaseyException("swaggerVersion is required");

            if (!json.ContainsKey("apis") == null)
                throw new SwaseyException("apis is required");

            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue,
                SwaggerVersion = (string) json.swaggerVersion
            };
            ctx.ServiceMetadata = new ServiceMetadata(ctx.ServiceMetadata)
            {
                ApiVersion = (string) json.apiVersion
            };

            foreach (var item in json.apis)
            {
                if (!item.ContainsKey("path") || string.IsNullOrWhiteSpace((string) item.path))
                    throw new SwaseyException("api.path is required");

                ctx.ApiPathJsonMapping.Add((string) item.path, null);
            }

            return Task.FromResult<ILifecycleContext>(ctx);
        }
コード例 #6
0
ファイル: LoadApiJsonCommand.cs プロジェクト: skrymsli/Swasey
        public async Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            var basePath = context.ResourceListingUri;

            foreach (var path in ctx.ApiPathJsonMapping.Keys.ToList())
            {
                var apiUriBuilder = new UriBuilder(basePath);
                apiUriBuilder.Path += new Uri(path, UriKind.Relative);
                var json = await context.Loader(apiUriBuilder.Uri);
                if (string.IsNullOrWhiteSpace(json))
                    throw new SwaseyException("Invalid JSON for api [{0}]: '{1}'", path, json);

                var obj = JSON.DeserializeDynamic(json);
                if (obj == null)
                    throw new SwaseyException("Unable to parse api definition JSON [{0}]: '{1}'", path, json);

                ctx.ApiPathJsonMapping[path] = obj;
            }

            return ctx;
        }
コード例 #7
0
        private IEnumerable <dynamic> ExtractApiOperations(ILifecycleContext context)
        {
            foreach (var apiKv in context.ApiPathJsonMapping)
            {
                var basePath = (string)context.ResourceListingJson.basePath;
                var opPath   = apiKv.Key;

                var ops = apiKv.Value;
                if (ops == null)
                {
                    continue;
                }

                foreach (var op in ops)
                {
                    yield return(new
                    {
                        BasePath = basePath,
                        OperationPath = opPath,
                        JObject = op
                    });
                }
//                }
            }
        }
コード例 #8
0
        public Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            OperationFilter = context.OperationFilter ?? Defaults.DefaultOperationFilter;
            OperationParameterFilter = context.OperationParameterFilter ?? Defaults.DefaultOperationParameterFilter;

            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            foreach (var apiOp in ExtractApiOperations(context))
            {
                if (!OperationFilter(apiOp.JObject)) continue;

                NormalizationApiOperation op = ParseOperationData(apiOp);
                op.ApiNamespace = context.ApiNamespace;
                op.ModelNamespace = context.ModelNamespace;

                op.Response.ApiNamespace = context.ApiNamespace;
                op.Response.ModelNamespace = context.ModelNamespace;

                foreach (var param in op.Parameters)
                {
                    param.ApiNamespace = context.ApiNamespace;
                    param.ModelNamespace = context.ModelNamespace;
                }

                ctx.NormalizationContext.Operations.Add(op);
            }

            return Task.FromResult<ILifecycleContext>(ctx);
        }
コード例 #9
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            var enums = context.NormalizationContext
                        .Enums
                        .Select(CreateDefinition)
                        .ToLookup(x => x.Name);

            if (!enums.Any())
            {
                goto ReturnResult;
            }

            foreach (var e in enums)
            {
                var x = e.FirstOrDefault();
                if (x != null)
                {
                    serviceDefinition.AddEnum(x);
                }
            }


ReturnResult:
            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State             = LifecycleState.Continue
            };

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
コード例 #10
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            OperationFilter          = context.OperationFilter ?? Defaults.DefaultOperationFilter;
            OperationParameterFilter = context.OperationParameterFilter ?? Defaults.DefaultOperationParameterFilter;

            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            foreach (var apiOp in ExtractApiOperations(context))
            {
                if (!OperationFilter(apiOp.JObject))
                {
                    continue;
                }

                NormalizationApiOperation op = ParseOperationData(apiOp);
                op.ApiNamespace   = context.ApiNamespace;
                op.ModelNamespace = context.ModelNamespace;

                op.Response.ApiNamespace   = context.ApiNamespace;
                op.Response.ModelNamespace = context.ModelNamespace;

                foreach (var param in op.Parameters)
                {
                    param.ApiNamespace   = context.ApiNamespace;
                    param.ModelNamespace = context.ModelNamespace;
                }

                ctx.NormalizationContext.Operations.Add(op);
            }

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
コード例 #11
0
        private IEnumerable<dynamic> ExtractApiOperations(ILifecycleContext context)
        {
            foreach (var apiKv in context.ApiPathJsonMapping)
            {
                var apiDef = apiKv.Value;
                var apiVersion = (string) apiDef.apiVersion;
                var basePath = (string) apiDef.basePath;
                var resourcePath = (string) apiDef.resourcePath;
                if (!apiDef.ContainsKey("apis")) { continue; }

                foreach (var api in apiDef.apis)
                {
                    if (api == null || !api.ContainsKey("path") || !api.ContainsKey("operations")) { continue; }
                    var opPath = (string) api.path;
                    foreach (var op in api.operations)
                    {
                        yield return new
                        {
                            ApiVersion = apiVersion,
                            BasePath = basePath,
                            OperationPath = opPath,
                            ResourcePath = resourcePath,
                            JObject = op
                        };
                    }
                }
            }
        }
コード例 #12
0
        private IEnumerable <dynamic> ExtractApiOperations(ILifecycleContext context)
        {
            foreach (var apiKv in context.ApiPathJsonMapping)
            {
                var apiDef       = apiKv.Value;
                var apiVersion   = (string)apiDef.apiVersion;
                var basePath     = (string)apiDef.basePath;
                var resourcePath = (string)apiDef.resourcePath;
                if (!apiDef.ContainsKey("apis"))
                {
                    continue;
                }

                foreach (var api in apiDef.apis)
                {
                    if (api == null || !api.ContainsKey("path") || !api.ContainsKey("operations"))
                    {
                        continue;
                    }
                    var opPath = (string)api.path;
                    foreach (var op in api.operations)
                    {
                        yield return(new
                        {
                            ApiVersion = apiVersion,
                            BasePath = basePath,
                            OperationPath = opPath,
                            ResourcePath = resourcePath,
                            JObject = op
                        });
                    }
                }
            }
        }
コード例 #13
0
        public async Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            var basePath = context.ResourceListingUri;

            foreach (var path in ctx.ApiPathJsonMapping.Keys.ToList())
            {
                var apiUriBuilder = new UriBuilder(basePath);
                apiUriBuilder.Path += new Uri(path, UriKind.Relative);
                var json = await context.Loader(apiUriBuilder.Uri);

                if (string.IsNullOrWhiteSpace(json))
                {
                    throw new SwaseyException("Invalid JSON for api [{0}]: '{1}'", path, json);
                }

                var obj = JSON.DeserializeDynamic(json);
                if (obj == null)
                {
                    throw new SwaseyException("Unable to parse api definition JSON [{0}]: '{1}'", path, json);
                }

                ctx.ApiPathJsonMapping[path] = obj;
            }

            return(ctx);
        }
コード例 #14
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var svcDef      = context.ServiceDefinition;
            var enumWriter  = context.EnumWriter;
            var modelWriter = context.ModelWriter;

            var sb = new StringBuilder();

            // Enums First
            var template = context.ApiEnumTemplate;

            foreach (var obj in svcDef.Enums.SelectMany(x => x))
            {
                using (var sw = new StringWriter(sb.Clear()))
                {
                    template(sw, obj);
                }

                enumWriter(obj.Name, sb.ToString(), obj);
            }

            // Models Second
            template = context.ApiModelTemplate;
            foreach (var obj in svcDef.Models.SelectMany(x => x))
            {
                using (var sw = new StringWriter(sb.Clear()))
                {
                    template(sw, obj);
                }

                modelWriter(obj.Name, sb.ToString(), obj);
            }

            return(Task.FromResult(context));
        }
コード例 #15
0
     private IEnumerable <dynamic> ExtractModels(ILifecycleContext context)
     {
         foreach (var apiKv in context.ApiPathJsonMapping)
         {
             var api          = apiKv.Value;
             var apiVersion   = (string)api.apiVersion;
             var resourcePath = (string)api.resourcePath;
             var resourceName = resourcePath.ResourceNameFromPath();
             if (api.ContainsKey("models"))
             {
                 foreach (var modelKv in api.models)
                 {
                     if (modelKv != null && modelKv.Value != null)
                     {
                         yield return new
                                {
                                    ApiVersion   = apiVersion,
                                    Model        = modelKv.Value,
                                    ResourceName = resourceName,
                                    ResourcePath = resourcePath
                                }
                     }
                     ;
                 }
             }
         }
     }
 }
コード例 #16
0
        public SingletonLifecycleTester()
        {
            theContext   = Substitute.For <ILifecycleContext>();
            theLifecycle = new SingletonLifecycle();

            theCache = Substitute.For <IObjectCache>();
            theContext.Singletons.Returns(theCache);
        }
コード例 #17
0
        public TransientLifecycleTester()
        {
            theContext = MockRepository.GenerateMock<ILifecycleContext>();
            theLifecycle = new TransientLifecycle();

            theCache = MockRepository.GenerateMock<ITransientTracking>();
            theContext.Stub(x => x.Transients).Return(theCache);
        }
コード例 #18
0
 public void EjectAll(ILifecycleContext context)
 {
     foreach (var kvp in _contextMap)
     {
         kvp.Value.DisposeAndClear();
     }
     _contextMap = new Dictionary <HttpContext, IObjectCache>();
 }
コード例 #19
0
        public void SetUp()
        {
            theContext   = MockRepository.GenerateMock <ILifecycleContext>();
            theLifecycle = new TransientLifecycle();

            theCache = MockRepository.GenerateMock <IObjectCache>();
            theContext.Stub(x => x.Transients).Return(theCache);
        }
コード例 #20
0
        public SingletonLifecycleTester()
        {
            theContext   = MockRepository.GenerateMock <ILifecycleContext>();
            theLifecycle = new SingletonLifecycle();

            theCache = MockRepository.GenerateMock <IObjectCache>();
            theContext.Stub(x => x.Singletons).Return(theCache);
        }
コード例 #21
0
        public TransientLifecycleTester()
        {
            theContext = Substitute.For<ILifecycleContext>();
            theLifecycle = new TransientLifecycle();

            theCache = Substitute.For<ITransientTracking>();
            theContext.Transients.Returns(theCache);
        }
コード例 #22
0
 public override void EjectAll(ILifecycleContext context)
 {
     if (HttpContextLifecycle.HasContext())
     {
         _http.EjectAll(context);
     }
     _nonHttp.EjectAll(context);
 }
コード例 #23
0
        public void SetUp()
        {
            theContext = MockRepository.GenerateMock<ILifecycleContext>();
            theLifecycle = new SingletonLifecycle();

            theCache = MockRepository.GenerateMock<IObjectCache>();
            theContext.Stub(x => x.Singletons).Return(theCache);
        }
コード例 #24
0
        public TransientLifecycleTester()
        {
            theContext   = MockRepository.GenerateMock <ILifecycleContext>();
            theLifecycle = new TransientLifecycle();

            theCache = MockRepository.GenerateMock <ITransientTracking>();
            theContext.Stub(x => x.Transients).Return(theCache);
        }
コード例 #25
0
        public void SetUp()
        {
            theContext = MockRepository.GenerateMock<ILifecycleContext>();
            theLifecycle = new TransientLifecycle();

            theCache = MockRepository.GenerateMock<IObjectCache>();
            theContext.Stub(x => x.Transients).Return(theCache);
        }
コード例 #26
0
        public SingletonLifecycleTester()
        {
            theContext = Substitute.For<ILifecycleContext>();
            theLifecycle = new SingletonLifecycle();

            theCache = Substitute.For<IObjectCache>();
            theContext.Singletons.Returns(theCache);
        }
コード例 #27
0
        public TransientLifecycleTester()
        {
            theContext   = Substitute.For <ILifecycleContext>();
            theLifecycle = new TransientLifecycle();

            theCache = Substitute.For <ITransientTracking>();
            theContext.Transients.Returns(theCache);
        }
コード例 #28
0
        public Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            var ctx = new LifecycleContext(context)
            {
                State = LifecycleState.Continue
            };

            foreach (var modelObj in ExtractModels(context))
            {
                var modelType = (string) modelObj.Model.type;
                var isEnum = "enum".Equals(modelType, StringComparison.InvariantCultureIgnoreCase);

                if (isEnum)
                {
                    var model = ParseEnumData(modelObj);
                    model.ApiNamespace = context.ApiNamespace;
                    model.ModelNamespace = context.ModelNamespace;
                    ctx.NormalizationContext.Enums.Add(model);
                }
                else
                {
                    var model = ParseModelData(modelObj);
                    model.ApiNamespace = context.ApiNamespace;
                    model.ModelNamespace = context.ModelNamespace;

                    foreach (var prop in model.Properties)
                    {
                        prop.ApiNamespace = context.ApiNamespace;
                        prop.ModelNamespace = context.ModelNamespace;
                    }

                    ctx.NormalizationContext.Models.Add(model);
                }
            }

            foreach (var model in ctx.NormalizationContext.Models.Where(x => x.RawSubTypes.Any()))
            {
                foreach (var st in model.RawSubTypes)
                {
                    var sm = ctx.NormalizationContext.Models.FirstOrDefault(x => x.Name.Equals(st, StringComparison.InvariantCultureIgnoreCase));
                    if (sm == null) continue;
                    model.SubTypes.Add(sm);
                }
            }

            var enumNames = ctx.NormalizationContext.Enums.Select(x => x.Name).ToList();
            var modelNames = ctx.NormalizationContext.Models.Select(x => x.Name).ToList();

            // Ensure that Enum Properties are properly indicated
            ctx.NormalizationContext.Models
                .SelectMany(x => x.Properties)
                .Where(x => enumNames.Contains(x.TypeName) && !modelNames.Contains(x.TypeName))
                .ToList()
                .ForEach(x => x.IsEnum = true);

            return Task.FromResult<ILifecycleContext>(ctx);
        }
コード例 #29
0
        public IObjectCache FindCache(ILifecycleContext context)
        {
            if (DateTime.Now.AddSeconds(-_secondsToExpire) >= _lastExpired)
            {
                Expire();
            }

            return(_cache);
        }
コード例 #30
0
        public IObjectCache FindCache(ILifecycleContext context)
        {
            if (this.scopeIsExpired)
            {
                this.EjectAll(context);
                this.scopeIsExpired = false;
            }

            return(this.Cache);
        }
コード例 #31
0
        public Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            foreach (var normalModel in context.NormalizationContext.Models)
            {
                var normalModelName = new NormalModelName(normalModel.ResourcePath, normalModel.Name);
                _modelNameMaps.Add(normalModelName);

                var modelDef = new ModelDefinition(normalModel.AsMetadata())
                {
                    ContextName = normalModel.ResourceName,
                    Description = normalModel.Description,
                    Name = normalModel.Name.MapDataTypeName(),
                    ResourceName = normalModel.ResourcePath.ResourceNameFromPath()
                };
                normalModelName.New = modelDef.Name;

                modelDef.Properties.AddRange(ExtractModelProperties(normalModel.Properties));

                serviceDefinition.AddModel(modelDef);
            }

            var resourceModelLookup = _modelNameMaps.ToLookup(x => x.ResourcePath);

            // Ensure all Operations refer to the proper model names
            foreach (var op in context.NormalizationContext.Operations)
            {
                if (!resourceModelLookup.Contains(op.ResourcePath)) { continue; }

                var resourceModels = resourceModelLookup[op.ResourcePath].ToList();

                var returnItem = resourceModels.SingleOrDefault(x => op.Response.TypeName.Equals(x.Old));
                if (returnItem != null)
                {
                    op.Response.SetTypeName(returnItem.New);
                }

                foreach (var opParam in op.Parameters)
                {
                    var paramItem = resourceModels.SingleOrDefault(x => opParam.TypeName.Equals(x.Old));
                    if (paramItem != null)
                    {
                        opParam.SetTypeName(paramItem.New);
                    }
                }
            }

            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State = LifecycleState.Continue
            };
            return Task.FromResult<ILifecycleContext>(ctx);
        }
コード例 #32
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            foreach (var kv in context.ApiPathJsonMapping)
            {
                if (kv.Value == null)
                {
                    throw new SwaseyException("No JSON parsed for api '{0}'", kv.Key);
                }

                var json = kv.Value;

                if (!json.ContainsKey("apiVersion") || string.IsNullOrWhiteSpace((string)json.apiVersion))
                {
                    throw new SwaseyException("apiVersion is required for api: '{0}'", kv.Key);
                }

                if (!json.ContainsKey("swaggerVersion") || string.IsNullOrWhiteSpace((string)json.swaggerVersion))
                {
                    throw new SwaseyException("swaggerVersion is required for api: '{0}'", kv.Key);
                }

                if (!json.ContainsKey("basePath") || string.IsNullOrWhiteSpace((string)json.basePath))
                {
                    throw new SwaseyException("basePath is required for api: '{0}'", kv.Key);
                }

                if (!json.ContainsKey("resourcePath") || string.IsNullOrWhiteSpace((string)json.resourcePath))
                {
                    throw new SwaseyException("resourcePath is required for api: '{0}'", kv.Key);
                }

                if (json.ContainsKey("models") && json.models.Length > 0)
                {
                    foreach (var modelKv in json.models)
                    {
                        if (modelKv.Key == null)
                        {
                            continue;
                        }
                        if (modelKv.Value == null)
                        {
                            throw new SwaseyException("listed model has no model definition [{0}]: '{1}'", modelKv.Key, modelKv.Value);
                        }

                        if (!modelKv.Value.ContainsKey("id") || string.IsNullOrWhiteSpace((string)modelKv.Value.id))
                        {
                            throw new SwaseyException("id is required for api model: '{0}'", modelKv.Key);
                        }
                    }
                }
            }

            return(Task.FromResult(context));
        }
コード例 #33
0
    public IObjectCache FindCache(ILifecycleContext context)
    {
        IHttpContextAccessor accessor = _container.GetInstance <IHttpContextAccessor>();

        if (!_contextMap.ContainsKey(accessor.HttpContext))
        {
            _contextMap.Add(accessor.HttpContext, new LifecycleObjectCache());
        }

        return(_contextMap[accessor.HttpContext]);
    }
コード例 #34
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var json = context.ResourceListingJson;

            if (json.ContainsKey("definitions") && json.definitions.length <= 0)
            {
                throw new SwaseyException("definitions object is empty");
            }

            return(Task.FromResult(context));
        }
コード例 #35
0
        public override void EjectAll(ILifecycleContext context)
        {
            lock (mapLock)
            {
                foreach (var kvp in contextMap)
                {
                    kvp.Value.ObjectCahe.DisposeAndClear();
                }

                contextMap = new Dictionary <string, SessionObject>();
            }
        }
コード例 #36
0
    public IObjectCache FindCache(ILifecycleContext context)
    {
        var accessor = container.GetInstance <IHttpContextAccessor>();

        lock (mapLock) {
            if (!contextMap.ContainsKey(accessor.HttpContext))
            {
                contextMap.Add(accessor.HttpContext, new LifecycleObjectCache());
            }
            return(contextMap[accessor.HttpContext]);
        }
    }
コード例 #37
0
        public override void EjectAll(ILifecycleContext context)
        {
            lock (mapLock)
            {
                foreach (var kvp in contextMap)
                {
                    kvp.Value.DisposeAndClear();
                }

                contextMap = new Dictionary <HttpContext, IObjectCache>();
            }
        }
コード例 #38
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var json = context.ResourceListingJson;

            if (!json.ContainsKey("swagger") == null || string.IsNullOrWhiteSpace((string)json.swagger))
            {
                throw new SwaseyException("swagger is required");
            }

            //swagger, info, info.version, info.title, path are required
            if (!json.ContainsKey("info") == null)
            {
                throw new SwaseyException("version is required");
            }

            if (!json.info.ContainsKey("version") == null || string.IsNullOrWhiteSpace((string)json.info.version))
            {
                throw new SwaseyException("version is required");
            }

            if (!json.info.ContainsKey("title") == null || string.IsNullOrWhiteSpace((string)json.info.title))
            {
                throw new SwaseyException("title is required");
            }

            if (!json.ContainsKey("paths") == null)
            {
                throw new SwaseyException("paths is required");
            }

            var ctx = new LifecycleContext(context)
            {
                State          = LifecycleState.Continue,
                SwaggerVersion = (string)json.swagger
            };

            ctx.ServiceMetadata = new ServiceMetadata(ctx.ServiceMetadata)
            {
                ApiVersion = (string)json.version
            };

            foreach (var path in json.paths)
            {
                ctx.ApiPathJsonMapping.Add((string)path.Key, path.Value);
            }

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
コード例 #39
0
        public async Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            while (_commands.Count > 0 && context.State == LifecycleState.Continue)
            {
                var cmd = _commands.Dequeue();

                context = await cmd.Execute(context);
            }

            return context;
        }
コード例 #40
0
        public async Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            while (_commands.Count > 0 && context.State == LifecycleState.Continue)
            {
                var cmd = _commands.Dequeue();

                context = await cmd.Execute(context);
            }

            return(context);
        }
コード例 #41
0
        public async Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            var json = await context.Loader(context.ResourceListingUri);
            if (string.IsNullOrWhiteSpace(json))
                throw new SwaseyException("Invalid Resource Listing JSON: '{0}'", json);

            var rl = JSON.DeserializeDynamic(json);
            if (rl == null)
                throw new SwaseyException("Unable to parse Resource Listing JSON: '{0}'", context.ResourceListingJson);

            return new LifecycleContext(context)
            {
                State = LifecycleState.Continue,
                ResourceListingJson = rl
            };
        }
コード例 #42
0
        public override IObjectCache FindCache(ILifecycleContext context)
        {
            var accessor = container.GetInstance <IHttpContextAccessor>();

            lock (mapLock)
            {
                if (!contextMap.ContainsKey(accessor.HttpContext.Session.Id))
                {
                    accessor.HttpContext.Session.Set("I", new byte[] { 1 });
                    contextMap.Add(accessor.HttpContext.Session.Id, new SessionObject {
                        TimeOut = 0, ObjectCahe = new LifecycleObjectCache()
                    });
                }

                return(contextMap[accessor.HttpContext.Session.Id].ObjectCahe);
            }
        }
コード例 #43
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var json = context.ResourceListingJson;

            if (!json.ContainsKey("apiVersion") == null || string.IsNullOrWhiteSpace((string)json.apiVersion))
            {
                throw new SwaseyException("apiVersion is required");
            }

            if (!json.ContainsKey("swaggerVersion") == null || string.IsNullOrWhiteSpace((string)json.swaggerVersion))
            {
                throw new SwaseyException("swaggerVersion is required");
            }

            if (!json.ContainsKey("apis") == null)
            {
                throw new SwaseyException("apis is required");
            }

            var ctx = new LifecycleContext(context)
            {
                State          = LifecycleState.Continue,
                SwaggerVersion = (string)json.swaggerVersion
            };

            ctx.ServiceMetadata = new ServiceMetadata(ctx.ServiceMetadata)
            {
                ApiVersion = (string)json.apiVersion
            };

            foreach (var item in json.apis)
            {
                if (!item.ContainsKey("path") || string.IsNullOrWhiteSpace((string)item.path))
                {
                    throw new SwaseyException("api.path is required");
                }

                ctx.ApiPathJsonMapping.Add((string)item.path, null);
            }

            ctx.ApiPathJsonMapping.Add((string)json.paths, null);

            return(Task.FromResult <ILifecycleContext>(ctx));
        }
コード例 #44
0
        public Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            var svcDef = context.ServiceDefinition;
            var template = context.ApiOperationTemplate;
            var swaseyWriter = context.OperationWriter;

            var sb = new StringBuilder();

            foreach (var op in svcDef.ResourceOperations.SelectMany(x => x))
            {
                using (var sw = new StringWriter(sb.Clear()))
                {
                    template(sw, op);
                }
                swaseyWriter(op.Name, sb.ToString(), op);
            }

            return Task.FromResult(context);
        }
コード例 #45
0
        public Task <ILifecycleContext> Execute(ILifecycleContext context)
        {
            var svcDef       = context.ServiceDefinition;
            var template     = context.ApiOperationTemplate;
            var swaseyWriter = context.OperationWriter;

            var sb = new StringBuilder();

            foreach (var op in svcDef.ResourceOperations20.SelectMany(x => x))
            {
                using (var sw = new StringWriter(sb.Clear()))
                {
                    template(sw, op);
                }
                swaseyWriter(op.Name, sb.ToString(), op);
            }

            return(Task.FromResult(context));
        }
コード例 #46
0
        public override IObjectCache FindCache(ILifecycleContext context)
        {
            IDictionary items = findHttpDictionary();

            if (!items.Contains(ITEM_NAME))
            {
                lock (items.SyncRoot)
                {
                    if (!items.Contains(ITEM_NAME))
                    {
                        var cache = new LifecycleObjectCache();
                        items.Add(ITEM_NAME, cache);

                        return cache;
                    }
                }
            }

            return (IObjectCache) items[ITEM_NAME];
        }
コード例 #47
0
        public IObjectCache FindCache(ILifecycleContext context)
        {
            IDictionary items = InteractionScope.Items;

            if (!items.Contains(StructureMapInstancesDictionaryKey))
            {
                lock (items.SyncRoot)
                {
                    if (!items.Contains(StructureMapInstancesDictionaryKey))
                    {
                        var cache = new LifecycleObjectCache();
                        items.Add(StructureMapInstancesDictionaryKey, cache);

                        return cache;
                    }
                }
            }

            return (IObjectCache)items[StructureMapInstancesDictionaryKey];
        }
コード例 #48
0
ファイル: LifecycleContext.cs プロジェクト: skrymsli/Swasey
        internal LifecycleContext(ILifecycleContext copyFrom)
            : this(copyFrom.ApiNamespace, copyFrom.ModelNamespace, copyFrom.Loader, copyFrom.OperationWriter, copyFrom.EnumWriter, copyFrom.ModelWriter)
        {
            State = copyFrom.State;
            ResourceListingUri = copyFrom.ResourceListingUri;

            ApiEnumTemplate = copyFrom.ApiEnumTemplate;
            ApiModelTemplate = copyFrom.ApiModelTemplate;
            ApiOperationTemplate = copyFrom.ApiOperationTemplate;

            SwaggerVersion = copyFrom.SwaggerVersion;
            ResourceListingJson = copyFrom.ResourceListingJson;

            OperationFilter = copyFrom.OperationFilter ?? Defaults.DefaultOperationFilter;
            OperationParameterFilter = copyFrom.OperationParameterFilter ?? Defaults.DefaultOperationParameterFilter;

            NormalizationContext = new NormalizationContext(copyFrom.NormalizationContext);

            ServiceDefinition = new ServiceDefinition(copyFrom.ServiceDefinition);

            copyFrom.ApiPathJsonMapping.ToList().ForEach(x => ApiPathJsonMapping.Add(x.Key, x.Value));
        }
コード例 #49
0
        public Task<ILifecycleContext> Execute(ILifecycleContext context)
        {
            var serviceDefinition = new ServiceDefinition(context.ServiceDefinition);

            foreach (var normalOp in context.NormalizationContext.Operations)
            {
                var op = new OperationDefinition(NormalizeOperationPath(normalOp), normalOp.AsMetadata())
                {
                    ConsumesOctetStream = normalOp.SupportsStreaming,
                    Description = normalOp.Description,
                    HttpMethod = normalOp.HttpMethod,
                    Name = ExtractName(normalOp),
                    ResourceName = normalOp.ResourcePath.ResourceNameFromPath(),
                    Response = NormalizeResponseDefinition(normalOp)
                };

                normalOp.Parameters
                    .Select(NormalizeParameterDefinition)
                    .ToList()
                    .ForEach(x => op.AddParameter(x));

                if (serviceDefinition.Operations.Any(x => x.ResourceName == op.ResourceName && x.Name == op.Name))
                {
                    var pathParams = op.Parameters.Where(x => x.Type == ParameterType.Path && x.IsRequired).OrderBy(x => x.Name);
                    op.Name += string.Join("And", pathParams.Select(x => "By" + x.Name.UCFirst()));
                }

                serviceDefinition.AddOperation(op);
            }

            var ctx = new LifecycleContext(context)
            {
                ServiceDefinition = serviceDefinition,
                State = LifecycleState.Continue
            };
            return Task.FromResult<ILifecycleContext>(ctx);
        }
コード例 #50
0
 private IEnumerable<dynamic> ExtractModels(ILifecycleContext context)
 {
     foreach (var apiKv in context.ApiPathJsonMapping)
     {
         var api = apiKv.Value;
         var apiVersion = (string) api.apiVersion;
         var resourcePath = (string) api.resourcePath;
         var resourceName = resourcePath.ResourceNameFromPath();
         if (api.ContainsKey("models"))
         {
             foreach (var modelKv in api.models)
             {
                 if (modelKv != null && modelKv.Value != null)
                     yield return new
                     {
                         ApiVersion = apiVersion,
                         Model = modelKv.Value,
                         ResourceName = resourceName,
                         ResourcePath = resourcePath
                     };
             }
         }
     }
 }
コード例 #51
0
 public IObjectCache FindCache(ILifecycleContext context)
 {
     throw new NotImplementedException();
 }
コード例 #52
0
 public IObjectCache FindCache(ILifecycleContext context)
 {
     guaranteeHashExists();
     return _cache;
 }
コード例 #53
0
 public void EjectAll(ILifecycleContext context)
 {
     FindCache(context).DisposeAndClear();
 }
コード例 #54
0
 public override void EjectAll(ILifecycleContext context)
 {
 }
コード例 #55
0
ファイル: ILifecycle.cs プロジェクト: rhyss/structuremap
 public override IObjectCache FindCache(ILifecycleContext context)
 {
     return context.Transients;
 }
コード例 #56
0
ファイル: ILifecycle.cs プロジェクト: rhyss/structuremap
 public abstract IObjectCache FindCache(ILifecycleContext context);
コード例 #57
0
ファイル: ILifecycle.cs プロジェクト: rhyss/structuremap
 public abstract void EjectAll(ILifecycleContext context);
コード例 #58
0
 /// <inheritdoc />
 public override IObjectCache FindCache(ILifecycleContext context)
 {
     return Cache.Value;
 }
コード例 #59
0
 public override IObjectCache FindCache(ILifecycleContext context)
 {
     throw new NotSupportedException("Should never be called");
 }
コード例 #60
0
 public void EjectAll(ILifecycleContext context)
 {
     throw new NotImplementedException();
 }