Browse Source

Max documents

pull/1/head
Sebastian 10 years ago
parent
commit
7d91da68ce
  1. 8
      src/Squidex.Read.MongoDb/Contents/Visitors/FindExtensions.cs
  2. 5
      src/Squidex/Config/Identity/IdentityServices.cs
  3. 6
      src/Squidex/Config/Swagger/XmlTagProcessor.cs
  4. 2
      src/Squidex/Controllers/ContentApi/ContentsController.cs
  5. 41
      src/Squidex/Controllers/ContentApi/Generator/SchemasSwaggerGenerator.cs
  6. 7
      src/Squidex/Docs/schemabody.md
  7. 11
      src/Squidex/Docs/schemaquery.md
  8. 12
      src/Squidex/Docs/security.md
  9. 104
      src/Squidex/Pipeline/Swagger/SwaggerHelper.cs
  10. 2
      src/Squidex/Squidex.csproj

8
src/Squidex.Read.MongoDb/Contents/Visitors/FindExtensions.cs

@ -6,12 +6,14 @@
// All rights reserved.
// ==========================================================================
using System;
using System.Collections.Generic;
using Microsoft.OData.Core.UriParser;
using MongoDB.Bson;
using MongoDB.Driver;
using Squidex.Core.Schemas;
// ReSharper disable ConvertIfStatementToConditionalTernaryExpression
// ReSharper disable RedundantIfElseBlock
namespace Squidex.Read.MongoDb.Contents.Visitors
@ -31,7 +33,11 @@ namespace Squidex.Read.MongoDb.Contents.Visitors
if (top.HasValue)
{
cursor = cursor.Limit((int)top.Value);
cursor = cursor.Limit(Math.Min((int)top.Value, 200));
}
else
{
cursor = cursor.Limit(20);
}
return cursor;

5
src/Squidex/Config/Identity/IdentityServices.cs

@ -63,10 +63,9 @@ namespace Squidex.Config.Identity
{
X509Certificate2 certificate;
var assemblyName = new AssemblyName("Squidex");
var assemblyRef = Assembly.Load(assemblyName);
var assembly = typeof(IdentityServices).GetTypeInfo().Assembly;
using (var certStream = assemblyRef.GetManifestResourceStream("Squidex.Config.Identity.Cert.IdentityCert.pfx"))
using (var certStream = assembly.GetManifestResourceStream("Squidex.Config.Identity.Cert.IdentityCert.pfx"))
{
var certData = new byte[certStream.Length];

6
src/Squidex/Config/Swagger/XmlTagProcessor.cs

@ -23,8 +23,7 @@ namespace Squidex.Config.Swagger
{
foreach (var controllerType in context.ControllerTypes)
{
var tagAttribute =
controllerType.GetTypeInfo().GetCustomAttribute<SwaggerTagAttribute>();
var tagAttribute = controllerType.GetTypeInfo().GetCustomAttribute<SwaggerTagAttribute>();
if (tagAttribute != null)
{
@ -50,8 +49,7 @@ namespace Squidex.Config.Swagger
public Task<bool> ProcessAsync(OperationProcessorContext context)
{
var tagAttribute =
context.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttribute<SwaggerTagAttribute>();
var tagAttribute = context.MethodInfo.DeclaringType.GetTypeInfo().GetCustomAttribute<SwaggerTagAttribute>();
if (tagAttribute != null)
{

2
src/Squidex/Controllers/ContentApi/ContentsController.cs

@ -64,7 +64,7 @@ namespace Squidex.Controllers.ContentApi
var model = new ContentsDto
{
Total = taskForCount.Result,
Items = taskForContents.Result.Select(x =>
Items = taskForContents.Result.Take(200).Select(x =>
{
var itemModel = SimpleMapper.Map(x, new ContentDto());

41
src/Squidex/Controllers/ContentApi/Generator/SchemasSwaggerGenerator.cs

@ -34,19 +34,14 @@ namespace Squidex.Controllers.ContentApi.Generator
{
public sealed class SchemasSwaggerGenerator
{
private const string BodyDescription =
@"The data of the {0} to be created or updated.
Please note that each field is an object with one entry per language.
If the field is not localizable you must use iv (Invariant Language) as a key.
When you change the field to be localizable the value will become the value for the master language, depending what the master language is at this point of time.";
private readonly SwaggerJsonSchemaGenerator schemaGenerator;
private readonly SwaggerDocument document = new SwaggerDocument { Tags = new List<SwaggerTag>() };
private readonly HttpContext context;
private readonly JsonSchemaResolver schemaResolver;
private readonly SwaggerGenerator swaggerGenerator;
private readonly MyUrlsOptions urlOptions;
private readonly string schemaQueryDescription;
private readonly string schemaBodyDescription;
private HashSet<Language> languages;
private JsonSchema4 errorDtoSchema;
private JsonSchema4 entityCreatedDtoSchema;
@ -64,6 +59,8 @@ When you change the field to be localizable the value will become the value for
swaggerGenerator = new SwaggerGenerator(schemaGenerator, swaggerSettings, schemaResolver);
schemaBodyDescription = SwaggerHelper.LoadDocs("schemabody");
schemaQueryDescription = SwaggerHelper.LoadDocs("schemaquery");
}
public async Task<SwaggerDocument> Generate(IAppEntity appEntity, IEnumerable<ISchemaEntityWithSchema> schemas)
@ -72,10 +69,9 @@ When you change the field to be localizable the value will become the value for
languages = new HashSet<Language>(appEntity.Languages);
appBasePath = $"/content/{appEntity.Name}";
await GenerateBasicSchemas();
GenerateBasePath(appEntity);
GenerateTitle();
GenerateRequestInfo();
GenerateContentTypes();
@ -88,6 +84,11 @@ When you change the field to be localizable the value will become the value for
return document;
}
private void GenerateBasePath(IAppEntity appEntity)
{
appBasePath = $"/content/{appEntity.Name}";
}
private void GenerateSchemes()
{
document.Schemes.Add(context.Request.Scheme == "http" ? SwaggerSchema.Http : SwaggerSchema.Https);
@ -96,7 +97,6 @@ When you change the field to be localizable the value will become the value for
private void GenerateTitle()
{
document.Host = context.Request.Host.Value ?? string.Empty;
document.BasePath = "/api";
}
@ -201,11 +201,13 @@ When you change the field to be localizable the value will become the value for
}
}
private SwaggerOperations GenerateSchemaQueryOperation(Schema schema, string schemaName, JsonSchema4 dataSchem)
private SwaggerOperations GenerateSchemaQueryOperation(Schema schema, string schemaName, JsonSchema4 dataSchema)
{
return AddOperation(SwaggerOperationMethod.Get, null, $"{appBasePath}/{schema.Name}", operation =>
{
operation.Summary = $"Queries {schemaName} content.";
operation.Summary = $"Queries {schemaName} content.";
operation.Description = schemaQueryDescription;
operation.AddQueryParameter("$top", JsonObjectType.Number, "Optional number of contents to take.");
operation.AddQueryParameter("$skip", JsonObjectType.Number, "Optional number of contents to skip.");
@ -213,7 +215,7 @@ When you change the field to be localizable the value will become the value for
operation.AddQueryParameter("$search", JsonObjectType.String, "Optional OData full text search.");
operation.AddQueryParameter("orderby", JsonObjectType.String, "Optional OData order definition.");
var responseSchema = CreateContentsSchema(schemaName, schema.Name, dataSchem);
var responseSchema = CreateContentsSchema(schemaName, schema.Name, dataSchema);
operation.AddResponse("200", $"{schemaName} content retrieved.", responseSchema);
});
@ -237,7 +239,7 @@ When you change the field to be localizable the value will become the value for
{
operation.Summary = $"Create a {schemaName} content.";
operation.AddBodyParameter(dataSchema, "data", string.Format(BodyDescription, schemaName));
operation.AddBodyParameter(dataSchema, "data", schemaBodyDescription);
operation.AddResponse("201", $"{schemaName} created.", entityCreatedDtoSchema);
});
@ -249,7 +251,7 @@ When you change the field to be localizable the value will become the value for
{
operation.Summary = $"Update a {schemaName} content.";
operation.AddBodyParameter(dataSchema, "data", string.Format(BodyDescription, schemaName));
operation.AddBodyParameter(dataSchema, "data", schemaBodyDescription);
operation.AddResponse("204", $"{schemaName} element updated.");
});
@ -261,7 +263,7 @@ When you change the field to be localizable the value will become the value for
{
operation.Summary = $"Patchs a {schemaName} content.";
operation.AddBodyParameter(dataSchema, "data", string.Format(BodyDescription, schemaName));
operation.AddBodyParameter(dataSchema, "data", schemaBodyDescription);
operation.AddResponse("204", $"{schemaName} element updated.");
});
@ -344,9 +346,8 @@ When you change the field to be localizable the value will become the value for
var CreateProperty =
new Func<string, string, JsonProperty>((d, f) =>
new JsonProperty { Description = d, Format = f, IsRequired = true, Type = JsonObjectType.String });
var dataDescription = $"The data of the {schemaName} content.";
var dataProperty = new JsonProperty { Description = dataDescription, Type = JsonObjectType.Object, IsRequired = true, SchemaReference = dataSchema };
var dataProperty = new JsonProperty { Description = schemaBodyDescription, Type = JsonObjectType.Object, IsRequired = true, SchemaReference = dataSchema };
var schema = new JsonSchema4
{

7
src/Squidex/Docs/schemabody.md

@ -0,0 +1,7 @@
The data of the content to be created or updated.
Please note that each field is an object with one entry per language.
If the field is not localizable you must use iv (Invariant Language) as a key.
When you change the field to be localizable the value will become the value for the master language, depending what the master language is at this point of time.
Read more about it at: https://docs.squidex.io/04-guides/api.html

11
src/Squidex/Docs/schemaquery.md

@ -0,0 +1,11 @@
The squidex API the OData url convention to query data.
We support the following query options.
* **$top**: The $top query option requests the number of items in the queried collection to be included in the result. The default value is 20 and the maximum allowed value is 200.
* **$skip**: The $skip query option requests the number of items in the queried collection that are to be skipped and not included in the result. Use it together with $top to read the all your data page by page.
* **$search**: The $search query option allows clients to request entities matching a free-text search expression. We add the data of all fields for all languages to a single field in the database and use this combined field to implement the full text search.
* **$filter**: The $filter query option allows clients to filter a collection of resources that are addressed by a request URL.
* **$orderby**: The $orderby query option allows clients to request resources in a particular order.
Read more about it at: https://docs.squidex.io/04-guides/api.html

12
src/Squidex/Docs/security.md

@ -0,0 +1,12 @@
Squidex uses oauth2 client authentication. Read more about it at: https://oauth.net/2/ and https://tools.ietf.org/html/rfc6750.
To retrieve an access token, the client id must make a request to the token url. For example:
$ curl
-X POST '<TOKEN_URL>'
-H 'Content-Type: application/x-www-form-urlencoded'
-d 'grant_type=client_credentials&
client_id=[APP_NAME]:[CLIENT_ID]&
client_secret=[CLIENT_SECRET]'
[APP_NAME] is the name of your app. You have to create a client to generate an access token.

104
src/Squidex/Pipeline/Swagger/SwaggerHelper.cs

@ -6,33 +6,43 @@
// All rights reserved.
// ==========================================================================
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using NJsonSchema;
using NSwag;
using Squidex.Config;
using System.Reflection;
namespace Squidex.Pipeline.Swagger
{
public static class SwaggerHelper
{
private const string SecurityDescription =
@"To retrieve an access token, the client id must make a request to the token url. For example:
private static ConcurrentDictionary<string, string> docs = new ConcurrentDictionary<string, string>();
$ curl
-X POST '{0}'
-H 'Content-Type: application/x-www-form-urlencoded'
-d 'grant_type=client_credentials&
client_id=[APP_NAME]:[CLIENT_NAME]&
client_secret=[CLIENT_SECRET]'";
public static string LoadDocs(string name)
{
return docs.GetOrAdd(name, x =>
{
var assembly = typeof(SwaggerHelper).GetTypeInfo().Assembly;
using (var resourceStream = assembly.GetManifestResourceStream($"Squidex.Docs.{name}.md"))
{
var streamReader = new StreamReader(resourceStream);
return streamReader.ReadToEnd();
}
});
}
public static SwaggerSecurityScheme CreateOAuthSchema(MyUrlsOptions urlOptions)
{
var tokenUrl = urlOptions.BuildUrl($"{Constants.IdentityPrefix}/connect/token");
var description = string.Format(CultureInfo.InvariantCulture, SecurityDescription, tokenUrl);
var securityDocs = LoadDocs("security");
var securityDescription = securityDocs.Replace("<TOKEN_URL>", tokenUrl);
return
var result =
new SwaggerSecurityScheme
{
TokenUrl = tokenUrl,
@ -42,53 +52,61 @@ namespace Squidex.Pipeline.Swagger
{
{ Constants.ApiScope, "Read and write access to the API" }
},
Description = description
Description = securityDescription
};
return result;
}
public static void AddQueryParameter(this SwaggerOperation operation, string name, JsonObjectType type, string description)
public static void AddQueryParameter(this SwaggerOperation operation, string name, JsonObjectType type, string description = null)
{
operation.Parameters.Add(
new SwaggerParameter
{
Type = type,
Name = name,
Kind = SwaggerParameterKind.Query,
Description = description
});
var parameter = new SwaggerParameter { Type = type, Name = name, Kind = SwaggerParameterKind.Query };
if (!string.IsNullOrWhiteSpace(description))
{
parameter.Description = description;
}
operation.Parameters.Add(parameter);
}
public static void AddPathParameter(this SwaggerOperation operation, string name, JsonObjectType type, string description)
public static void AddPathParameter(this SwaggerOperation operation, string name, JsonObjectType type, string description = null)
{
operation.Parameters.Add(
new SwaggerParameter
{
Type = type,
Name = name,
Kind = SwaggerParameterKind.Path,
IsRequired = true,
IsNullableRaw = false,
Description = description
});
var parameter = new SwaggerParameter { Type = type, Name = name, Kind = SwaggerParameterKind.Path };
if (!string.IsNullOrWhiteSpace(description))
{
parameter.Description = description;
}
parameter.IsRequired = true;
parameter.IsNullableRaw = false;
operation.Parameters.Add(parameter);
operation.Parameters.Add(parameter);
}
public static void AddBodyParameter(this SwaggerOperation operation, JsonSchema4 schema, string name, string description)
{
operation.Parameters.Add(
new SwaggerParameter
{
Name = name,
Kind = SwaggerParameterKind.Body,
Schema = schema,
IsRequired = true,
IsNullableRaw = false,
Description = description
});
var parameter = new SwaggerParameter { Schema = schema, Name = name, Kind = SwaggerParameterKind.Body };
if (!string.IsNullOrWhiteSpace(description))
{
parameter.Description = description;
}
parameter.IsRequired = true;
parameter.IsNullableRaw = false;
operation.Parameters.Add(parameter);
}
public static void AddResponse(this SwaggerOperation operation, string statusCode, string description, JsonSchema4 schema = null)
{
operation.Responses.Add(statusCode, new SwaggerResponse { Description = description, Schema = schema });
var response = new SwaggerResponse { Description = description, Schema = schema };
operation.Responses.Add(statusCode, response);
}
}
}

2
src/Squidex/Squidex.csproj

@ -14,7 +14,7 @@
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="Config\Identity\Cert\*.*" />
<EmbeddedResource Include="Config\Identity\Cert\*.*;Docs\*.md" />
<None Update="dockerfile">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>

Loading…
Cancel
Save