forked from loafle/openapi-generator-original
Petstore sample for NancyFX.
This commit is contained in:
parent
6404d47f97
commit
0d9a490c17
25
samples/server/petstore/nancyfx/IO.Swagger.sln
Normal file
25
samples/server/petstore/nancyfx/IO.Swagger.sln
Normal file
@ -0,0 +1,25 @@
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 2012
|
||||
VisualStudioVersion = 12.0.0.0
|
||||
MinimumVisualStudioVersion = 10.0.0.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IO.Swagger", "src\IO.Swagger\IO.Swagger.csproj", "{1DE2DD8D-1AFB-4BC2-9FB5-04DE7DCA1353}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{1DE2DD8D-1AFB-4BC2-9FB5-04DE7DCA1353}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{1DE2DD8D-1AFB-4BC2-9FB5-04DE7DCA1353}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1DE2DD8D-1AFB-4BC2-9FB5-04DE7DCA1353}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1DE2DD8D-1AFB-4BC2-9FB5-04DE7DCA1353}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{19F1DEBC-DE5E-4517-8062-F000CD499087}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{19F1DEBC-DE5E-4517-8062-F000CD499087}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{19F1DEBC-DE5E-4517-8062-F000CD499087}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{19F1DEBC-DE5E-4517-8062-F000CD499087}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
EndGlobal
|
@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{1DE2DD8D-1AFB-4BC2-9FB5-04DE7DCA1353}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>IO.Swagger.v2</RootNamespace>
|
||||
<AssemblyName></AssemblyName>
|
||||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>bin\Debug\IO.Swagger.XML</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
<DocumentationFile>bin\Release\IO.Swagger.XML</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Nancy, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Nancy.1.4.1\lib\net40\Nancy.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="NodaTime, Version=1.3.0.0, Culture=neutral, PublicKeyToken=4226afe0d9b296d1, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\NodaTime.1.3.1\lib\net35-Client\NodaTime.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="Sharpility, Version=1.2.1.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\Sharpility.1.2.1\lib\net45\Sharpility.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
<Reference Include="System.Collections.Immutable, Version=1.1.37.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\..\packages\System.Collections.Immutable.1.1.37\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference> <Reference Include="System"/>
|
||||
<Reference Include="System.Core"/>
|
||||
<Reference Include="System.Xml.Linq"/>
|
||||
<Reference Include="System.Data.DataSetExtensions"/>
|
||||
<Reference Include="Microsoft.CSharp"/>
|
||||
<Reference Include="System.Data"/>
|
||||
<Reference Include="System.Runtime.Serialization"/>
|
||||
<Reference Include="System.Xml"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="**\*.cs" Exclude="obj\**"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Content Include="packages.config"/>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MsBuildToolsPath)\Microsoft.CSharp.targets"/>
|
||||
</Project>
|
||||
|
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0"?>
|
||||
<package>
|
||||
<metadata>
|
||||
<id>IO.Swagger</id>
|
||||
<title>IO.Swagger</title>
|
||||
<version>1.0.0</version>
|
||||
<authors>swagger-codegen</authors>
|
||||
<owners>swagger-codegen</owners>
|
||||
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||
<description>NancyFx IO.Swagger API</description>
|
||||
<copyright>http://helloreverb.com/terms/</copyright>
|
||||
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0.html</licenseUrl>
|
||||
</metadata>
|
||||
</package>
|
@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Sharpility.Extensions;
|
||||
using NodaTime;
|
||||
|
||||
namespace IO.Swagger.v2.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Category
|
||||
/// </summary>
|
||||
public sealed class Category: IEquatable<Category>
|
||||
{
|
||||
/// <summary>
|
||||
/// Id
|
||||
/// </summary>
|
||||
public long? Id { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Name
|
||||
/// </summary>
|
||||
public string Name { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Empty constructor required by some serializers.
|
||||
/// Use Category.Builder() for instance creation instead.
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public Category()
|
||||
{
|
||||
}
|
||||
|
||||
private Category(long? Id, string Name)
|
||||
{
|
||||
|
||||
this.Id = Id;
|
||||
|
||||
this.Name = Name;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns builder of Category.
|
||||
/// </summary>
|
||||
/// <returns>CategoryBuilder</returns>
|
||||
public static CategoryBuilder Builder()
|
||||
{
|
||||
return new CategoryBuilder();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns CategoryBuilder with properties set.
|
||||
/// Use it to change properties.
|
||||
/// </summary>
|
||||
/// <returns>CategoryBuilder</returns>
|
||||
public CategoryBuilder With()
|
||||
{
|
||||
return Builder()
|
||||
.Id(Id)
|
||||
.Name(Name);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return this.PropertiesToString();
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return this.EqualsByProperties(obj);
|
||||
}
|
||||
|
||||
public bool Equals(Category other)
|
||||
{
|
||||
return Equals((object) other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.PropertiesHash();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of == operator for (Category.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (Category</param>
|
||||
/// <param name="right">Compared (Category</param>
|
||||
/// <returns>true if compared items are equals, false otherwise</returns>
|
||||
public static bool operator == (Category left, Category right)
|
||||
{
|
||||
return Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of != operator for (Category.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (Category</param>
|
||||
/// <param name="right">Compared (Category</param>
|
||||
/// <returns>true if compared items are not equals, false otherwise</returns>
|
||||
public static bool operator != (Category left, Category right)
|
||||
{
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builder of Category.
|
||||
/// </summary>
|
||||
public sealed class CategoryBuilder
|
||||
{
|
||||
private long? _Id;
|
||||
private string _Name;
|
||||
|
||||
internal CategoryBuilder()
|
||||
{
|
||||
SetupDefaults();
|
||||
}
|
||||
|
||||
private void SetupDefaults()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Category.Id property.
|
||||
/// </summary>
|
||||
/// <param name="value">Id</param>
|
||||
public CategoryBuilder Id(long? value)
|
||||
{
|
||||
_Id = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Category.Name property.
|
||||
/// </summary>
|
||||
/// <param name="value">Name</param>
|
||||
public CategoryBuilder Name(string value)
|
||||
{
|
||||
_Name = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Builds instance of Category.
|
||||
/// </summary>
|
||||
/// <returns>Category</returns>
|
||||
public Category Build()
|
||||
{
|
||||
Validate();
|
||||
return new Category(
|
||||
Id: _Id,
|
||||
Name: _Name
|
||||
);
|
||||
}
|
||||
|
||||
private void Validate()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
246
samples/server/petstore/nancyfx/src/IO.Swagger/Models/Order.cs
Normal file
246
samples/server/petstore/nancyfx/src/IO.Swagger/Models/Order.cs
Normal file
@ -0,0 +1,246 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Sharpility.Extensions;
|
||||
using NodaTime;
|
||||
|
||||
namespace IO.Swagger.v2.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Order
|
||||
/// </summary>
|
||||
public sealed class Order: IEquatable<Order>
|
||||
{
|
||||
/// <summary>
|
||||
/// Id
|
||||
/// </summary>
|
||||
public long? Id { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// PetId
|
||||
/// </summary>
|
||||
public long? PetId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Quantity
|
||||
/// </summary>
|
||||
public int? Quantity { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// ShipDate
|
||||
/// </summary>
|
||||
public ZonedDateTime? ShipDate { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Order Status
|
||||
/// </summary>
|
||||
public StatusEnum? Status { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Complete
|
||||
/// </summary>
|
||||
public bool? Complete { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Empty constructor required by some serializers.
|
||||
/// Use Order.Builder() for instance creation instead.
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public Order()
|
||||
{
|
||||
}
|
||||
|
||||
private Order(long? Id, long? PetId, int? Quantity, ZonedDateTime? ShipDate, StatusEnum? Status, bool? Complete)
|
||||
{
|
||||
|
||||
this.Id = Id;
|
||||
|
||||
this.PetId = PetId;
|
||||
|
||||
this.Quantity = Quantity;
|
||||
|
||||
this.ShipDate = ShipDate;
|
||||
|
||||
this.Status = Status;
|
||||
|
||||
this.Complete = Complete;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns builder of Order.
|
||||
/// </summary>
|
||||
/// <returns>OrderBuilder</returns>
|
||||
public static OrderBuilder Builder()
|
||||
{
|
||||
return new OrderBuilder();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns OrderBuilder with properties set.
|
||||
/// Use it to change properties.
|
||||
/// </summary>
|
||||
/// <returns>OrderBuilder</returns>
|
||||
public OrderBuilder With()
|
||||
{
|
||||
return Builder()
|
||||
.Id(Id)
|
||||
.PetId(PetId)
|
||||
.Quantity(Quantity)
|
||||
.ShipDate(ShipDate)
|
||||
.Status(Status)
|
||||
.Complete(Complete);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return this.PropertiesToString();
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return this.EqualsByProperties(obj);
|
||||
}
|
||||
|
||||
public bool Equals(Order other)
|
||||
{
|
||||
return Equals((object) other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.PropertiesHash();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of == operator for (Order.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (Order</param>
|
||||
/// <param name="right">Compared (Order</param>
|
||||
/// <returns>true if compared items are equals, false otherwise</returns>
|
||||
public static bool operator == (Order left, Order right)
|
||||
{
|
||||
return Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of != operator for (Order.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (Order</param>
|
||||
/// <param name="right">Compared (Order</param>
|
||||
/// <returns>true if compared items are not equals, false otherwise</returns>
|
||||
public static bool operator != (Order left, Order right)
|
||||
{
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builder of Order.
|
||||
/// </summary>
|
||||
public sealed class OrderBuilder
|
||||
{
|
||||
private long? _Id;
|
||||
private long? _PetId;
|
||||
private int? _Quantity;
|
||||
private ZonedDateTime? _ShipDate;
|
||||
private StatusEnum? _Status;
|
||||
private bool? _Complete;
|
||||
|
||||
internal OrderBuilder()
|
||||
{
|
||||
SetupDefaults();
|
||||
}
|
||||
|
||||
private void SetupDefaults()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Order.Id property.
|
||||
/// </summary>
|
||||
/// <param name="value">Id</param>
|
||||
public OrderBuilder Id(long? value)
|
||||
{
|
||||
_Id = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Order.PetId property.
|
||||
/// </summary>
|
||||
/// <param name="value">PetId</param>
|
||||
public OrderBuilder PetId(long? value)
|
||||
{
|
||||
_PetId = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Order.Quantity property.
|
||||
/// </summary>
|
||||
/// <param name="value">Quantity</param>
|
||||
public OrderBuilder Quantity(int? value)
|
||||
{
|
||||
_Quantity = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Order.ShipDate property.
|
||||
/// </summary>
|
||||
/// <param name="value">ShipDate</param>
|
||||
public OrderBuilder ShipDate(ZonedDateTime? value)
|
||||
{
|
||||
_ShipDate = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Order.Status property.
|
||||
/// </summary>
|
||||
/// <param name="value">Order Status</param>
|
||||
public OrderBuilder Status(StatusEnum? value)
|
||||
{
|
||||
_Status = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Order.Complete property.
|
||||
/// </summary>
|
||||
/// <param name="value">Complete</param>
|
||||
public OrderBuilder Complete(bool? value)
|
||||
{
|
||||
_Complete = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Builds instance of Order.
|
||||
/// </summary>
|
||||
/// <returns>Order</returns>
|
||||
public Order Build()
|
||||
{
|
||||
Validate();
|
||||
return new Order(
|
||||
Id: _Id,
|
||||
PetId: _PetId,
|
||||
Quantity: _Quantity,
|
||||
ShipDate: _ShipDate,
|
||||
Status: _Status,
|
||||
Complete: _Complete
|
||||
);
|
||||
}
|
||||
|
||||
private void Validate()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public enum StatusEnum { Placed, Approved, Delivered };
|
||||
}
|
||||
}
|
254
samples/server/petstore/nancyfx/src/IO.Swagger/Models/Pet.cs
Normal file
254
samples/server/petstore/nancyfx/src/IO.Swagger/Models/Pet.cs
Normal file
@ -0,0 +1,254 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Sharpility.Extensions;
|
||||
using NodaTime;
|
||||
|
||||
namespace IO.Swagger.v2.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Pet
|
||||
/// </summary>
|
||||
public sealed class Pet: IEquatable<Pet>
|
||||
{
|
||||
/// <summary>
|
||||
/// Id
|
||||
/// </summary>
|
||||
public long? Id { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Category
|
||||
/// </summary>
|
||||
public Category Category { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Name
|
||||
/// </summary>
|
||||
public string Name { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// PhotoUrls
|
||||
/// </summary>
|
||||
public List<string> PhotoUrls { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Tags
|
||||
/// </summary>
|
||||
public List<Tag> Tags { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// pet status in the store
|
||||
/// </summary>
|
||||
public StatusEnum? Status { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Empty constructor required by some serializers.
|
||||
/// Use Pet.Builder() for instance creation instead.
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public Pet()
|
||||
{
|
||||
}
|
||||
|
||||
private Pet(long? Id, Category Category, string Name, List<string> PhotoUrls, List<Tag> Tags, StatusEnum? Status)
|
||||
{
|
||||
|
||||
this.Id = Id;
|
||||
|
||||
this.Category = Category;
|
||||
|
||||
this.Name = Name;
|
||||
|
||||
this.PhotoUrls = PhotoUrls;
|
||||
|
||||
this.Tags = Tags;
|
||||
|
||||
this.Status = Status;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns builder of Pet.
|
||||
/// </summary>
|
||||
/// <returns>PetBuilder</returns>
|
||||
public static PetBuilder Builder()
|
||||
{
|
||||
return new PetBuilder();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns PetBuilder with properties set.
|
||||
/// Use it to change properties.
|
||||
/// </summary>
|
||||
/// <returns>PetBuilder</returns>
|
||||
public PetBuilder With()
|
||||
{
|
||||
return Builder()
|
||||
.Id(Id)
|
||||
.Category(Category)
|
||||
.Name(Name)
|
||||
.PhotoUrls(PhotoUrls)
|
||||
.Tags(Tags)
|
||||
.Status(Status);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return this.PropertiesToString();
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return this.EqualsByProperties(obj);
|
||||
}
|
||||
|
||||
public bool Equals(Pet other)
|
||||
{
|
||||
return Equals((object) other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.PropertiesHash();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of == operator for (Pet.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (Pet</param>
|
||||
/// <param name="right">Compared (Pet</param>
|
||||
/// <returns>true if compared items are equals, false otherwise</returns>
|
||||
public static bool operator == (Pet left, Pet right)
|
||||
{
|
||||
return Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of != operator for (Pet.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (Pet</param>
|
||||
/// <param name="right">Compared (Pet</param>
|
||||
/// <returns>true if compared items are not equals, false otherwise</returns>
|
||||
public static bool operator != (Pet left, Pet right)
|
||||
{
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builder of Pet.
|
||||
/// </summary>
|
||||
public sealed class PetBuilder
|
||||
{
|
||||
private long? _Id;
|
||||
private Category _Category;
|
||||
private string _Name;
|
||||
private List<string> _PhotoUrls;
|
||||
private List<Tag> _Tags;
|
||||
private StatusEnum? _Status;
|
||||
|
||||
internal PetBuilder()
|
||||
{
|
||||
SetupDefaults();
|
||||
}
|
||||
|
||||
private void SetupDefaults()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Pet.Id property.
|
||||
/// </summary>
|
||||
/// <param name="value">Id</param>
|
||||
public PetBuilder Id(long? value)
|
||||
{
|
||||
_Id = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Pet.Category property.
|
||||
/// </summary>
|
||||
/// <param name="value">Category</param>
|
||||
public PetBuilder Category(Category value)
|
||||
{
|
||||
_Category = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Pet.Name property.
|
||||
/// </summary>
|
||||
/// <param name="value">Name</param>
|
||||
public PetBuilder Name(string value)
|
||||
{
|
||||
_Name = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Pet.PhotoUrls property.
|
||||
/// </summary>
|
||||
/// <param name="value">PhotoUrls</param>
|
||||
public PetBuilder PhotoUrls(List<string> value)
|
||||
{
|
||||
_PhotoUrls = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Pet.Tags property.
|
||||
/// </summary>
|
||||
/// <param name="value">Tags</param>
|
||||
public PetBuilder Tags(List<Tag> value)
|
||||
{
|
||||
_Tags = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Pet.Status property.
|
||||
/// </summary>
|
||||
/// <param name="value">pet status in the store</param>
|
||||
public PetBuilder Status(StatusEnum? value)
|
||||
{
|
||||
_Status = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Builds instance of Pet.
|
||||
/// </summary>
|
||||
/// <returns>Pet</returns>
|
||||
public Pet Build()
|
||||
{
|
||||
Validate();
|
||||
return new Pet(
|
||||
Id: _Id,
|
||||
Category: _Category,
|
||||
Name: _Name,
|
||||
PhotoUrls: _PhotoUrls,
|
||||
Tags: _Tags,
|
||||
Status: _Status
|
||||
);
|
||||
}
|
||||
|
||||
private void Validate()
|
||||
{
|
||||
if (_Name == null)
|
||||
{
|
||||
throw new ArgumentException("Name is a required property for Pet and cannot be null");
|
||||
}
|
||||
if (_PhotoUrls == null)
|
||||
{
|
||||
throw new ArgumentException("PhotoUrls is a required property for Pet and cannot be null");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public enum StatusEnum { Available, Pending, Sold };
|
||||
}
|
||||
}
|
165
samples/server/petstore/nancyfx/src/IO.Swagger/Models/Tag.cs
Normal file
165
samples/server/petstore/nancyfx/src/IO.Swagger/Models/Tag.cs
Normal file
@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Sharpility.Extensions;
|
||||
using NodaTime;
|
||||
|
||||
namespace IO.Swagger.v2.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Tag
|
||||
/// </summary>
|
||||
public sealed class Tag: IEquatable<Tag>
|
||||
{
|
||||
/// <summary>
|
||||
/// Id
|
||||
/// </summary>
|
||||
public long? Id { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Name
|
||||
/// </summary>
|
||||
public string Name { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Empty constructor required by some serializers.
|
||||
/// Use Tag.Builder() for instance creation instead.
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public Tag()
|
||||
{
|
||||
}
|
||||
|
||||
private Tag(long? Id, string Name)
|
||||
{
|
||||
|
||||
this.Id = Id;
|
||||
|
||||
this.Name = Name;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns builder of Tag.
|
||||
/// </summary>
|
||||
/// <returns>TagBuilder</returns>
|
||||
public static TagBuilder Builder()
|
||||
{
|
||||
return new TagBuilder();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns TagBuilder with properties set.
|
||||
/// Use it to change properties.
|
||||
/// </summary>
|
||||
/// <returns>TagBuilder</returns>
|
||||
public TagBuilder With()
|
||||
{
|
||||
return Builder()
|
||||
.Id(Id)
|
||||
.Name(Name);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return this.PropertiesToString();
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return this.EqualsByProperties(obj);
|
||||
}
|
||||
|
||||
public bool Equals(Tag other)
|
||||
{
|
||||
return Equals((object) other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.PropertiesHash();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of == operator for (Tag.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (Tag</param>
|
||||
/// <param name="right">Compared (Tag</param>
|
||||
/// <returns>true if compared items are equals, false otherwise</returns>
|
||||
public static bool operator == (Tag left, Tag right)
|
||||
{
|
||||
return Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of != operator for (Tag.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (Tag</param>
|
||||
/// <param name="right">Compared (Tag</param>
|
||||
/// <returns>true if compared items are not equals, false otherwise</returns>
|
||||
public static bool operator != (Tag left, Tag right)
|
||||
{
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builder of Tag.
|
||||
/// </summary>
|
||||
public sealed class TagBuilder
|
||||
{
|
||||
private long? _Id;
|
||||
private string _Name;
|
||||
|
||||
internal TagBuilder()
|
||||
{
|
||||
SetupDefaults();
|
||||
}
|
||||
|
||||
private void SetupDefaults()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Tag.Id property.
|
||||
/// </summary>
|
||||
/// <param name="value">Id</param>
|
||||
public TagBuilder Id(long? value)
|
||||
{
|
||||
_Id = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for Tag.Name property.
|
||||
/// </summary>
|
||||
/// <param name="value">Name</param>
|
||||
public TagBuilder Name(string value)
|
||||
{
|
||||
_Name = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Builds instance of Tag.
|
||||
/// </summary>
|
||||
/// <returns>Tag</returns>
|
||||
public Tag Build()
|
||||
{
|
||||
Validate();
|
||||
return new Tag(
|
||||
Id: _Id,
|
||||
Name: _Name
|
||||
);
|
||||
}
|
||||
|
||||
private void Validate()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
285
samples/server/petstore/nancyfx/src/IO.Swagger/Models/User.cs
Normal file
285
samples/server/petstore/nancyfx/src/IO.Swagger/Models/User.cs
Normal file
@ -0,0 +1,285 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using Sharpility.Extensions;
|
||||
using NodaTime;
|
||||
|
||||
namespace IO.Swagger.v2.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// User
|
||||
/// </summary>
|
||||
public sealed class User: IEquatable<User>
|
||||
{
|
||||
/// <summary>
|
||||
/// Id
|
||||
/// </summary>
|
||||
public long? Id { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Username
|
||||
/// </summary>
|
||||
public string Username { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// FirstName
|
||||
/// </summary>
|
||||
public string FirstName { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// LastName
|
||||
/// </summary>
|
||||
public string LastName { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Email
|
||||
/// </summary>
|
||||
public string Email { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Password
|
||||
/// </summary>
|
||||
public string Password { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Phone
|
||||
/// </summary>
|
||||
public string Phone { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// User Status
|
||||
/// </summary>
|
||||
public int? UserStatus { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Empty constructor required by some serializers.
|
||||
/// Use User.Builder() for instance creation instead.
|
||||
/// </summary>
|
||||
[Obsolete]
|
||||
public User()
|
||||
{
|
||||
}
|
||||
|
||||
private User(long? Id, string Username, string FirstName, string LastName, string Email, string Password, string Phone, int? UserStatus)
|
||||
{
|
||||
|
||||
this.Id = Id;
|
||||
|
||||
this.Username = Username;
|
||||
|
||||
this.FirstName = FirstName;
|
||||
|
||||
this.LastName = LastName;
|
||||
|
||||
this.Email = Email;
|
||||
|
||||
this.Password = Password;
|
||||
|
||||
this.Phone = Phone;
|
||||
|
||||
this.UserStatus = UserStatus;
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns builder of User.
|
||||
/// </summary>
|
||||
/// <returns>UserBuilder</returns>
|
||||
public static UserBuilder Builder()
|
||||
{
|
||||
return new UserBuilder();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns UserBuilder with properties set.
|
||||
/// Use it to change properties.
|
||||
/// </summary>
|
||||
/// <returns>UserBuilder</returns>
|
||||
public UserBuilder With()
|
||||
{
|
||||
return Builder()
|
||||
.Id(Id)
|
||||
.Username(Username)
|
||||
.FirstName(FirstName)
|
||||
.LastName(LastName)
|
||||
.Email(Email)
|
||||
.Password(Password)
|
||||
.Phone(Phone)
|
||||
.UserStatus(UserStatus);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return this.PropertiesToString();
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return this.EqualsByProperties(obj);
|
||||
}
|
||||
|
||||
public bool Equals(User other)
|
||||
{
|
||||
return Equals((object) other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return this.PropertiesHash();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of == operator for (User.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (User</param>
|
||||
/// <param name="right">Compared (User</param>
|
||||
/// <returns>true if compared items are equals, false otherwise</returns>
|
||||
public static bool operator == (User left, User right)
|
||||
{
|
||||
return Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implementation of != operator for (User.
|
||||
/// </summary>
|
||||
/// <param name="left">Compared (User</param>
|
||||
/// <param name="right">Compared (User</param>
|
||||
/// <returns>true if compared items are not equals, false otherwise</returns>
|
||||
public static bool operator != (User left, User right)
|
||||
{
|
||||
return !Equals(left, right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builder of User.
|
||||
/// </summary>
|
||||
public sealed class UserBuilder
|
||||
{
|
||||
private long? _Id;
|
||||
private string _Username;
|
||||
private string _FirstName;
|
||||
private string _LastName;
|
||||
private string _Email;
|
||||
private string _Password;
|
||||
private string _Phone;
|
||||
private int? _UserStatus;
|
||||
|
||||
internal UserBuilder()
|
||||
{
|
||||
SetupDefaults();
|
||||
}
|
||||
|
||||
private void SetupDefaults()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for User.Id property.
|
||||
/// </summary>
|
||||
/// <param name="value">Id</param>
|
||||
public UserBuilder Id(long? value)
|
||||
{
|
||||
_Id = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for User.Username property.
|
||||
/// </summary>
|
||||
/// <param name="value">Username</param>
|
||||
public UserBuilder Username(string value)
|
||||
{
|
||||
_Username = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for User.FirstName property.
|
||||
/// </summary>
|
||||
/// <param name="value">FirstName</param>
|
||||
public UserBuilder FirstName(string value)
|
||||
{
|
||||
_FirstName = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for User.LastName property.
|
||||
/// </summary>
|
||||
/// <param name="value">LastName</param>
|
||||
public UserBuilder LastName(string value)
|
||||
{
|
||||
_LastName = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for User.Email property.
|
||||
/// </summary>
|
||||
/// <param name="value">Email</param>
|
||||
public UserBuilder Email(string value)
|
||||
{
|
||||
_Email = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for User.Password property.
|
||||
/// </summary>
|
||||
/// <param name="value">Password</param>
|
||||
public UserBuilder Password(string value)
|
||||
{
|
||||
_Password = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for User.Phone property.
|
||||
/// </summary>
|
||||
/// <param name="value">Phone</param>
|
||||
public UserBuilder Phone(string value)
|
||||
{
|
||||
_Phone = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets value for User.UserStatus property.
|
||||
/// </summary>
|
||||
/// <param name="value">User Status</param>
|
||||
public UserBuilder UserStatus(int? value)
|
||||
{
|
||||
_UserStatus = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Builds instance of User.
|
||||
/// </summary>
|
||||
/// <returns>User</returns>
|
||||
public User Build()
|
||||
{
|
||||
Validate();
|
||||
return new User(
|
||||
Id: _Id,
|
||||
Username: _Username,
|
||||
FirstName: _FirstName,
|
||||
LastName: _LastName,
|
||||
Email: _Email,
|
||||
Password: _Password,
|
||||
Phone: _Phone,
|
||||
UserStatus: _UserStatus
|
||||
);
|
||||
}
|
||||
|
||||
private void Validate()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,229 @@
|
||||
using System;
|
||||
using Nancy;
|
||||
using Nancy.ModelBinding;
|
||||
using System.Collections.Generic;
|
||||
using Sharpility.Base;
|
||||
using IO.Swagger.v2.Models;
|
||||
using IO.Swagger.v2.Utils;
|
||||
using NodaTime;
|
||||
|
||||
namespace IO.Swagger.v2.Modules
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Module processing requests of Pet domain.
|
||||
/// </summary>
|
||||
public sealed class PetModule : NancyModule
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets up HTTP methods mappings.
|
||||
/// </summary>
|
||||
/// <param name="service">Service handling requests</param>
|
||||
public PetModule(PetService service) : base("/v2")
|
||||
{
|
||||
Post["/pet"] = parameters =>
|
||||
{
|
||||
var body = this.Bind<Pet>();
|
||||
service.AddPet(Context, body);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Delete["/pet/{petId}"] = parameters =>
|
||||
{
|
||||
var petId = Parameters.ValueOf<long?>(parameters, Context.Request, "petId", ParameterType.Path);
|
||||
var apiKey = Parameters.ValueOf<string>(parameters, Context.Request, "apiKey", ParameterType.Header);
|
||||
Preconditions.IsNotNull(petId, "Required parameter: 'petId' is missing at 'DeletePet'");
|
||||
|
||||
service.DeletePet(Context, petId, apiKey);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Get["/pet/findByStatus"] = parameters =>
|
||||
{
|
||||
var status = Parameters.ValueOf<List<string>>(parameters, Context.Request, "status", ParameterType.Query);
|
||||
return service.FindPetsByStatus(Context, status);
|
||||
};
|
||||
|
||||
Get["/pet/findByTags"] = parameters =>
|
||||
{
|
||||
var tags = Parameters.ValueOf<List<string>>(parameters, Context.Request, "tags", ParameterType.Query);
|
||||
return service.FindPetsByTags(Context, tags);
|
||||
};
|
||||
|
||||
Get["/pet/{petId}"] = parameters =>
|
||||
{
|
||||
var petId = Parameters.ValueOf<long?>(parameters, Context.Request, "petId", ParameterType.Path);
|
||||
Preconditions.IsNotNull(petId, "Required parameter: 'petId' is missing at 'GetPetById'");
|
||||
|
||||
return service.GetPetById(Context, petId);
|
||||
};
|
||||
|
||||
Put["/pet"] = parameters =>
|
||||
{
|
||||
var body = this.Bind<Pet>();
|
||||
service.UpdatePet(Context, body);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Post["/pet/{petId}"] = parameters =>
|
||||
{
|
||||
var petId = Parameters.ValueOf<string>(parameters, Context.Request, "petId", ParameterType.Path);
|
||||
var name = Parameters.ValueOf<string>(parameters, Context.Request, "name", ParameterType.Undefined);
|
||||
var status = Parameters.ValueOf<string>(parameters, Context.Request, "status", ParameterType.Undefined);
|
||||
Preconditions.IsNotNull(petId, "Required parameter: 'petId' is missing at 'UpdatePetWithForm'");
|
||||
|
||||
service.UpdatePetWithForm(Context, petId, name, status);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Post["/pet/{petId}/uploadImage"] = parameters =>
|
||||
{
|
||||
var petId = Parameters.ValueOf<long?>(parameters, Context.Request, "petId", ParameterType.Path);
|
||||
var additionalMetadata = Parameters.ValueOf<string>(parameters, Context.Request, "additionalMetadata", ParameterType.Undefined);
|
||||
var file = Parameters.ValueOf<System.IO.Stream>(parameters, Context.Request, "file", ParameterType.Undefined);
|
||||
Preconditions.IsNotNull(petId, "Required parameter: 'petId' is missing at 'UploadFile'");
|
||||
|
||||
service.UploadFile(Context, petId, additionalMetadata, file);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Service handling Pet requests.
|
||||
/// </summary>
|
||||
public interface PetService
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="body">Pet object that needs to be added to the store (optional)</param>
|
||||
/// <returns></returns>
|
||||
void AddPet(NancyContext context, Pet body);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="petId">Pet id to delete</param>
|
||||
/// <param name="apiKey"> (optional)</param>
|
||||
/// <returns></returns>
|
||||
void DeletePet(NancyContext context, long? petId, string apiKey);
|
||||
|
||||
/// <summary>
|
||||
/// Multiple status values can be provided with comma seperated strings
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="status">Status values that need to be considered for filter (optional, default to available)</param>
|
||||
/// <returns>List<Pet></returns>
|
||||
List<Pet> FindPetsByStatus(NancyContext context, List<string> status);
|
||||
|
||||
/// <summary>
|
||||
/// Muliple tags can be provided with comma seperated strings. Use tag1, tag2, tag3 for testing.
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="tags">Tags to filter by (optional)</param>
|
||||
/// <returns>List<Pet></returns>
|
||||
List<Pet> FindPetsByTags(NancyContext context, List<string> tags);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="petId">ID of pet that needs to be fetched</param>
|
||||
/// <returns>Pet</returns>
|
||||
Pet GetPetById(NancyContext context, long? petId);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="body">Pet object that needs to be added to the store (optional)</param>
|
||||
/// <returns></returns>
|
||||
void UpdatePet(NancyContext context, Pet body);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="petId">ID of pet that needs to be updated</param>
|
||||
/// <param name="name">Updated name of the pet (optional)</param>
|
||||
/// <param name="status">Updated status of the pet (optional)</param>
|
||||
/// <returns></returns>
|
||||
void UpdatePetWithForm(NancyContext context, string petId, string name, string status);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="petId">ID of pet to update</param>
|
||||
/// <param name="additionalMetadata">Additional data to pass to server (optional)</param>
|
||||
/// <param name="file">file to upload (optional)</param>
|
||||
/// <returns></returns>
|
||||
void UploadFile(NancyContext context, long? petId, string additionalMetadata, System.IO.Stream file);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstraction of PetService.
|
||||
/// </summary>
|
||||
public abstract class AbstractPetService: PetService
|
||||
{
|
||||
public virtual void AddPet(NancyContext context, Pet body)
|
||||
{
|
||||
AddPet(body);
|
||||
}
|
||||
|
||||
public virtual void DeletePet(NancyContext context, long? petId, string apiKey)
|
||||
{
|
||||
DeletePet(petId, apiKey);
|
||||
}
|
||||
|
||||
public virtual List<Pet> FindPetsByStatus(NancyContext context, List<string> status)
|
||||
{
|
||||
return FindPetsByStatus(status);
|
||||
}
|
||||
|
||||
public virtual List<Pet> FindPetsByTags(NancyContext context, List<string> tags)
|
||||
{
|
||||
return FindPetsByTags(tags);
|
||||
}
|
||||
|
||||
public virtual Pet GetPetById(NancyContext context, long? petId)
|
||||
{
|
||||
return GetPetById(petId);
|
||||
}
|
||||
|
||||
public virtual void UpdatePet(NancyContext context, Pet body)
|
||||
{
|
||||
UpdatePet(body);
|
||||
}
|
||||
|
||||
public virtual void UpdatePetWithForm(NancyContext context, string petId, string name, string status)
|
||||
{
|
||||
UpdatePetWithForm(petId, name, status);
|
||||
}
|
||||
|
||||
public virtual void UploadFile(NancyContext context, long? petId, string additionalMetadata, System.IO.Stream file)
|
||||
{
|
||||
UploadFile(petId, additionalMetadata, file);
|
||||
}
|
||||
|
||||
protected abstract void AddPet(Pet body);
|
||||
|
||||
protected abstract void DeletePet(long? petId, string apiKey);
|
||||
|
||||
protected abstract List<Pet> FindPetsByStatus(List<string> status);
|
||||
|
||||
protected abstract List<Pet> FindPetsByTags(List<string> tags);
|
||||
|
||||
protected abstract Pet GetPetById(long? petId);
|
||||
|
||||
protected abstract void UpdatePet(Pet body);
|
||||
|
||||
protected abstract void UpdatePetWithForm(string petId, string name, string status);
|
||||
|
||||
protected abstract void UploadFile(long? petId, string additionalMetadata, System.IO.Stream file);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
using System;
|
||||
using Nancy;
|
||||
using Nancy.ModelBinding;
|
||||
using System.Collections.Generic;
|
||||
using Sharpility.Base;
|
||||
using IO.Swagger.v2.Models;
|
||||
using IO.Swagger.v2.Utils;
|
||||
using NodaTime;
|
||||
|
||||
namespace IO.Swagger.v2.Modules
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Module processing requests of Store domain.
|
||||
/// </summary>
|
||||
public sealed class StoreModule : NancyModule
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets up HTTP methods mappings.
|
||||
/// </summary>
|
||||
/// <param name="service">Service handling requests</param>
|
||||
public StoreModule(StoreService service) : base("/v2")
|
||||
{
|
||||
Delete["/store/order/{orderId}"] = parameters =>
|
||||
{
|
||||
var orderId = Parameters.ValueOf<string>(parameters, Context.Request, "orderId", ParameterType.Path);
|
||||
Preconditions.IsNotNull(orderId, "Required parameter: 'orderId' is missing at 'DeleteOrder'");
|
||||
|
||||
service.DeleteOrder(Context, orderId);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Get["/store/inventory"] = parameters =>
|
||||
{
|
||||
|
||||
return service.GetInventory(Context);
|
||||
};
|
||||
|
||||
Get["/store/order/{orderId}"] = parameters =>
|
||||
{
|
||||
var orderId = Parameters.ValueOf<string>(parameters, Context.Request, "orderId", ParameterType.Path);
|
||||
Preconditions.IsNotNull(orderId, "Required parameter: 'orderId' is missing at 'GetOrderById'");
|
||||
|
||||
return service.GetOrderById(Context, orderId);
|
||||
};
|
||||
|
||||
Post["/store/order"] = parameters =>
|
||||
{
|
||||
var body = this.Bind<Order>();
|
||||
return service.PlaceOrder(Context, body);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Service handling Store requests.
|
||||
/// </summary>
|
||||
public interface StoreService
|
||||
{
|
||||
/// <summary>
|
||||
/// For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="orderId">ID of the order that needs to be deleted</param>
|
||||
/// <returns></returns>
|
||||
void DeleteOrder(NancyContext context, string orderId);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a map of status codes to quantities
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <returns>Dictionary<string, int?></returns>
|
||||
Dictionary<string, int?> GetInventory(NancyContext context);
|
||||
|
||||
/// <summary>
|
||||
/// For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="orderId">ID of pet that needs to be fetched</param>
|
||||
/// <returns>Order</returns>
|
||||
Order GetOrderById(NancyContext context, string orderId);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="body">order placed for purchasing the pet (optional)</param>
|
||||
/// <returns>Order</returns>
|
||||
Order PlaceOrder(NancyContext context, Order body);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstraction of StoreService.
|
||||
/// </summary>
|
||||
public abstract class AbstractStoreService: StoreService
|
||||
{
|
||||
public virtual void DeleteOrder(NancyContext context, string orderId)
|
||||
{
|
||||
DeleteOrder(orderId);
|
||||
}
|
||||
|
||||
public virtual Dictionary<string, int?> GetInventory(NancyContext context)
|
||||
{
|
||||
return GetInventory();
|
||||
}
|
||||
|
||||
public virtual Order GetOrderById(NancyContext context, string orderId)
|
||||
{
|
||||
return GetOrderById(orderId);
|
||||
}
|
||||
|
||||
public virtual Order PlaceOrder(NancyContext context, Order body)
|
||||
{
|
||||
return PlaceOrder(body);
|
||||
}
|
||||
|
||||
protected abstract void DeleteOrder(string orderId);
|
||||
|
||||
protected abstract Dictionary<string, int?> GetInventory();
|
||||
|
||||
protected abstract Order GetOrderById(string orderId);
|
||||
|
||||
protected abstract Order PlaceOrder(Order body);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,221 @@
|
||||
using System;
|
||||
using Nancy;
|
||||
using Nancy.ModelBinding;
|
||||
using System.Collections.Generic;
|
||||
using Sharpility.Base;
|
||||
using IO.Swagger.v2.Models;
|
||||
using IO.Swagger.v2.Utils;
|
||||
using NodaTime;
|
||||
|
||||
namespace IO.Swagger.v2.Modules
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Module processing requests of User domain.
|
||||
/// </summary>
|
||||
public sealed class UserModule : NancyModule
|
||||
{
|
||||
/// <summary>
|
||||
/// Sets up HTTP methods mappings.
|
||||
/// </summary>
|
||||
/// <param name="service">Service handling requests</param>
|
||||
public UserModule(UserService service) : base("/v2")
|
||||
{
|
||||
Post["/user"] = parameters =>
|
||||
{
|
||||
var body = this.Bind<User>();
|
||||
service.CreateUser(Context, body);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Post["/user/createWithArray"] = parameters =>
|
||||
{
|
||||
var body = this.Bind<List<User>>();
|
||||
service.CreateUsersWithArrayInput(Context, body);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Post["/user/createWithList"] = parameters =>
|
||||
{
|
||||
var body = this.Bind<List<User>>();
|
||||
service.CreateUsersWithListInput(Context, body);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Delete["/user/{username}"] = parameters =>
|
||||
{
|
||||
var username = Parameters.ValueOf<string>(parameters, Context.Request, "username", ParameterType.Path);
|
||||
Preconditions.IsNotNull(username, "Required parameter: 'username' is missing at 'DeleteUser'");
|
||||
|
||||
service.DeleteUser(Context, username);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Get["/user/{username}"] = parameters =>
|
||||
{
|
||||
var username = Parameters.ValueOf<string>(parameters, Context.Request, "username", ParameterType.Path);
|
||||
Preconditions.IsNotNull(username, "Required parameter: 'username' is missing at 'GetUserByName'");
|
||||
|
||||
return service.GetUserByName(Context, username);
|
||||
};
|
||||
|
||||
Get["/user/login"] = parameters =>
|
||||
{
|
||||
var username = Parameters.ValueOf<string>(parameters, Context.Request, "username", ParameterType.Query);
|
||||
var password = Parameters.ValueOf<string>(parameters, Context.Request, "password", ParameterType.Query);
|
||||
return service.LoginUser(Context, username, password);
|
||||
};
|
||||
|
||||
Get["/user/logout"] = parameters =>
|
||||
{
|
||||
|
||||
service.LogoutUser(Context);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
|
||||
Put["/user/{username}"] = parameters =>
|
||||
{
|
||||
var username = Parameters.ValueOf<string>(parameters, Context.Request, "username", ParameterType.Path);
|
||||
var body = this.Bind<User>();
|
||||
Preconditions.IsNotNull(username, "Required parameter: 'username' is missing at 'UpdateUser'");
|
||||
|
||||
service.UpdateUser(Context, username, body);
|
||||
return new Response { ContentType = "application/json"};
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Service handling User requests.
|
||||
/// </summary>
|
||||
public interface UserService
|
||||
{
|
||||
/// <summary>
|
||||
/// This can only be done by the logged in user.
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="body">Created user object (optional)</param>
|
||||
/// <returns></returns>
|
||||
void CreateUser(NancyContext context, User body);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="body">List of user object (optional)</param>
|
||||
/// <returns></returns>
|
||||
void CreateUsersWithArrayInput(NancyContext context, List<User> body);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="body">List of user object (optional)</param>
|
||||
/// <returns></returns>
|
||||
void CreateUsersWithListInput(NancyContext context, List<User> body);
|
||||
|
||||
/// <summary>
|
||||
/// This can only be done by the logged in user.
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="username">The name that needs to be deleted</param>
|
||||
/// <returns></returns>
|
||||
void DeleteUser(NancyContext context, string username);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="username">The name that needs to be fetched. Use user1 for testing. </param>
|
||||
/// <returns>User</returns>
|
||||
User GetUserByName(NancyContext context, string username);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="username">The user name for login (optional)</param>
|
||||
/// <param name="password">The password for login in clear text (optional)</param>
|
||||
/// <returns>string</returns>
|
||||
string LoginUser(NancyContext context, string username, string password);
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <returns></returns>
|
||||
void LogoutUser(NancyContext context);
|
||||
|
||||
/// <summary>
|
||||
/// This can only be done by the logged in user.
|
||||
/// </summary>
|
||||
/// <param name="context">Context of request</param>
|
||||
/// <param name="username">name that need to be deleted</param>
|
||||
/// <param name="body">Updated user object (optional)</param>
|
||||
/// <returns></returns>
|
||||
void UpdateUser(NancyContext context, string username, User body);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstraction of UserService.
|
||||
/// </summary>
|
||||
public abstract class AbstractUserService: UserService
|
||||
{
|
||||
public virtual void CreateUser(NancyContext context, User body)
|
||||
{
|
||||
CreateUser(body);
|
||||
}
|
||||
|
||||
public virtual void CreateUsersWithArrayInput(NancyContext context, List<User> body)
|
||||
{
|
||||
CreateUsersWithArrayInput(body);
|
||||
}
|
||||
|
||||
public virtual void CreateUsersWithListInput(NancyContext context, List<User> body)
|
||||
{
|
||||
CreateUsersWithListInput(body);
|
||||
}
|
||||
|
||||
public virtual void DeleteUser(NancyContext context, string username)
|
||||
{
|
||||
DeleteUser(username);
|
||||
}
|
||||
|
||||
public virtual User GetUserByName(NancyContext context, string username)
|
||||
{
|
||||
return GetUserByName(username);
|
||||
}
|
||||
|
||||
public virtual string LoginUser(NancyContext context, string username, string password)
|
||||
{
|
||||
return LoginUser(username, password);
|
||||
}
|
||||
|
||||
public virtual void LogoutUser(NancyContext context)
|
||||
{
|
||||
LogoutUser();
|
||||
}
|
||||
|
||||
public virtual void UpdateUser(NancyContext context, string username, User body)
|
||||
{
|
||||
UpdateUser(username, body);
|
||||
}
|
||||
|
||||
protected abstract void CreateUser(User body);
|
||||
|
||||
protected abstract void CreateUsersWithArrayInput(List<User> body);
|
||||
|
||||
protected abstract void CreateUsersWithListInput(List<User> body);
|
||||
|
||||
protected abstract void DeleteUser(string username);
|
||||
|
||||
protected abstract User GetUserByName(string username);
|
||||
|
||||
protected abstract string LoginUser(string username, string password);
|
||||
|
||||
protected abstract void LogoutUser();
|
||||
|
||||
protected abstract void UpdateUser(string username, User body);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,414 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
using Nancy;
|
||||
using NodaTime;
|
||||
using NodaTime.Text;
|
||||
using Sharpility.Base;
|
||||
using Sharpility.Extensions;
|
||||
using Sharpility.Util;
|
||||
|
||||
namespace IO.Swagger.v2.Utils
|
||||
{
|
||||
internal static class Parameters
|
||||
{
|
||||
private static readonly IDictionary<Type, Func<Parameter, object>> Parsers = CreateParsers();
|
||||
|
||||
internal static TValue ValueOf<TValue>(dynamic parameters, Request request, string name, ParameterType parameterType)
|
||||
{
|
||||
var valueType = typeof(TValue);
|
||||
var valueUnderlyingType = Nullable.GetUnderlyingType(valueType);
|
||||
var isNullable = default(TValue) == null;
|
||||
string value = RawValueOf(parameters, request, name, parameterType);
|
||||
Preconditions.Evaluate(!string.IsNullOrEmpty(value) || isNullable, string.Format("Required parameter: '{0}' is missing", name));
|
||||
if (value == null && isNullable)
|
||||
{
|
||||
return default(TValue);
|
||||
}
|
||||
if (valueType.IsEnum || (valueUnderlyingType != null && valueUnderlyingType.IsEnum))
|
||||
{
|
||||
return EnumValueOf<TValue>(name, value);
|
||||
}
|
||||
return ValueOf<TValue>(parameters, name, value, valueType, request, parameterType);
|
||||
}
|
||||
|
||||
private static string RawValueOf(dynamic parameters, Request request, string name, ParameterType parameterType)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (parameterType)
|
||||
{
|
||||
case ParameterType.Query:
|
||||
string querValue = request.Query[name];
|
||||
return querValue;
|
||||
case ParameterType.Path:
|
||||
string pathValue = parameters[name];
|
||||
return pathValue;
|
||||
case ParameterType.Header:
|
||||
var headerValue = request.Headers[name];
|
||||
return headerValue != null ? string.Join(",", headerValue) : null;
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new InvalidOperationException(string.Format("Could not obtain value of '{0}' parameter", name), e);
|
||||
}
|
||||
throw new InvalidOperationException(string.Format("Parameter with type: {0} is not supported", parameterType));
|
||||
}
|
||||
|
||||
private static TValue EnumValueOf<TValue>(string name, string value)
|
||||
{
|
||||
var valueType = typeof(TValue);
|
||||
var enumType = valueType.IsEnum ? valueType : Nullable.GetUnderlyingType(valueType);
|
||||
Preconditions.IsNotNull(enumType, () => new InvalidOperationException(
|
||||
string.Format("Could not parse parameter: '{0}' to enum. Type {1} is not enum", name, valueType)));
|
||||
var values = Enum.GetValues(enumType);
|
||||
foreach (var entry in values)
|
||||
{
|
||||
if (entry.ToString().EqualsIgnoreCases(value)
|
||||
|| ((int)entry).ToString().EqualsIgnoreCases(value))
|
||||
{
|
||||
return (TValue)entry;
|
||||
}
|
||||
}
|
||||
throw new ArgumentException(string.Format("Parameter: '{0}' value: '{1}' is not supported. Expected one of: {2}",
|
||||
name, value, Strings.ToString(values)));
|
||||
}
|
||||
|
||||
private static TValue ValueOf<TValue>(dynamic parameters, string name, string value, Type valueType, Request request, ParameterType parameterType)
|
||||
{
|
||||
var parser = Parsers.GetIfPresent(valueType);
|
||||
if (parser != null)
|
||||
{
|
||||
return ParseValueUsing<TValue>(name, value, valueType, parser);
|
||||
}
|
||||
if (parameterType == ParameterType.Path)
|
||||
{
|
||||
return DynamicValueOf<TValue>(parameters, name);
|
||||
}
|
||||
if (parameterType == ParameterType.Query)
|
||||
{
|
||||
return DynamicValueOf<TValue>(request.Query, name);
|
||||
}
|
||||
throw new InvalidOperationException(string.Format("Could not get value for {0} with type {1}", name, valueType));
|
||||
}
|
||||
|
||||
private static TValue ParseValueUsing<TValue>(string name, string value, Type valueType, Func<Parameter, object> parser)
|
||||
{
|
||||
var result = parser(Parameter.Of(name, value));
|
||||
try
|
||||
{
|
||||
return (TValue)result;
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
string.Format("Could not parse parameter: '{0}' with value: '{1}'. " +
|
||||
"Received: '{2}', expected: '{3}'.",
|
||||
name, value, result.GetType(), valueType));
|
||||
}
|
||||
}
|
||||
|
||||
private static TValue DynamicValueOf<TValue>(dynamic parameters, string name)
|
||||
{
|
||||
string value = parameters[name];
|
||||
try
|
||||
{
|
||||
TValue result = parameters[name];
|
||||
return result;
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
throw new InvalidOperationException(Strings.Format("Parameter: '{0}' value: '{1}' could not be parsed. " +
|
||||
"Expected type: '{2}' is not supported",
|
||||
name, value, typeof(TValue)));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new InvalidOperationException(string.Format("Could not get '{0}' value of '{1}' type dynamicly",
|
||||
name, typeof(TValue)), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static IDictionary<Type, Func<Parameter, object>> CreateParsers()
|
||||
{
|
||||
var parsers = ImmutableDictionary.CreateBuilder<Type, Func<Parameter, object>>();
|
||||
parsers.Put(typeof(string), value => value);
|
||||
parsers.Put(typeof(bool), SafeParse(bool.Parse));
|
||||
parsers.Put(typeof(bool?), SafeParse(bool.Parse));
|
||||
parsers.Put(typeof(byte), SafeParse(byte.Parse));
|
||||
parsers.Put(typeof(sbyte?), SafeParse(sbyte.Parse));
|
||||
parsers.Put(typeof(short), SafeParse(short.Parse));
|
||||
parsers.Put(typeof(short?), SafeParse(short.Parse));
|
||||
parsers.Put(typeof(ushort), SafeParse(ushort.Parse));
|
||||
parsers.Put(typeof(ushort?), SafeParse(ushort.Parse));
|
||||
parsers.Put(typeof(int), SafeParse(int.Parse));
|
||||
parsers.Put(typeof(int?), SafeParse(int.Parse));
|
||||
parsers.Put(typeof(uint), SafeParse(uint.Parse));
|
||||
parsers.Put(typeof(uint?), SafeParse(uint.Parse));
|
||||
parsers.Put(typeof(long), SafeParse(long.Parse));
|
||||
parsers.Put(typeof(long?), SafeParse(long.Parse));
|
||||
parsers.Put(typeof(ulong), SafeParse(ulong.Parse));
|
||||
parsers.Put(typeof(ulong?), SafeParse(ulong.Parse));
|
||||
parsers.Put(typeof(float), SafeParse(float.Parse));
|
||||
parsers.Put(typeof(float?), SafeParse(float.Parse));
|
||||
parsers.Put(typeof(double), SafeParse(double.Parse));
|
||||
parsers.Put(typeof(double?), SafeParse(double.Parse));
|
||||
parsers.Put(typeof(decimal), SafeParse(decimal.Parse));
|
||||
parsers.Put(typeof(decimal?), SafeParse(decimal.Parse));
|
||||
parsers.Put(typeof(DateTime), SafeParse(DateTime.Parse));
|
||||
parsers.Put(typeof(DateTime?), SafeParse(DateTime.Parse));
|
||||
parsers.Put(typeof(TimeSpan), SafeParse(TimeSpan.Parse));
|
||||
parsers.Put(typeof(TimeSpan?), SafeParse(TimeSpan.Parse));
|
||||
parsers.Put(typeof(ZonedDateTime), SafeParse(ParseZonedDateTime));
|
||||
parsers.Put(typeof(ZonedDateTime?), SafeParse(ParseZonedDateTime));
|
||||
parsers.Put(typeof(LocalTime), SafeParse(ParseLocalTime));
|
||||
parsers.Put(typeof(LocalTime?), SafeParse(ParseLocalTime));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<string>), value => value);
|
||||
parsers.Put(typeof(ICollection<string>), value => value);
|
||||
parsers.Put(typeof(IList<string>), value => value);
|
||||
parsers.Put(typeof(List<string>), value => value);
|
||||
parsers.Put(typeof(ISet<string>), value => value);
|
||||
parsers.Put(typeof(HashSet<string>), value => value);
|
||||
|
||||
parsers.Put(typeof(IEnumerable<bool>), ImmutableListParse(bool.Parse));
|
||||
parsers.Put(typeof(ICollection<bool>), ImmutableListParse(bool.Parse));
|
||||
parsers.Put(typeof(IList<bool>), ImmutableListParse(bool.Parse));
|
||||
parsers.Put(typeof(List<bool>), ListParse(bool.Parse));
|
||||
parsers.Put(typeof(ISet<bool>), ImmutableSetParse(bool.Parse));
|
||||
parsers.Put(typeof(HashSet<bool>), SetParse(bool.Parse));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<byte>), ImmutableListParse(byte.Parse));
|
||||
parsers.Put(typeof(ICollection<byte>), ImmutableListParse(byte.Parse));
|
||||
parsers.Put(typeof(IList<byte>), ImmutableListParse(byte.Parse));
|
||||
parsers.Put(typeof(List<byte>), ListParse(byte.Parse));
|
||||
parsers.Put(typeof(ISet<byte>), ImmutableSetParse(byte.Parse));
|
||||
parsers.Put(typeof(HashSet<byte>), SetParse(byte.Parse));
|
||||
parsers.Put(typeof(IEnumerable<sbyte>), ImmutableListParse(sbyte.Parse));
|
||||
parsers.Put(typeof(ICollection<sbyte>), ImmutableListParse(sbyte.Parse));
|
||||
parsers.Put(typeof(IList<sbyte>), ImmutableListParse(sbyte.Parse));
|
||||
parsers.Put(typeof(List<sbyte>), ListParse(sbyte.Parse));
|
||||
parsers.Put(typeof(ISet<sbyte>), ImmutableSetParse(sbyte.Parse));
|
||||
parsers.Put(typeof(HashSet<sbyte>), SetParse(sbyte.Parse));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<short>), ImmutableListParse(short.Parse));
|
||||
parsers.Put(typeof(ICollection<short>), ImmutableListParse(short.Parse));
|
||||
parsers.Put(typeof(IList<short>), ImmutableListParse(short.Parse));
|
||||
parsers.Put(typeof(List<short>), ListParse(short.Parse));
|
||||
parsers.Put(typeof(ISet<short>), ImmutableSetParse(short.Parse));
|
||||
parsers.Put(typeof(HashSet<short>), SetParse(short.Parse));
|
||||
parsers.Put(typeof(IEnumerable<ushort>), ImmutableListParse(ushort.Parse));
|
||||
parsers.Put(typeof(ICollection<ushort>), ImmutableListParse(ushort.Parse));
|
||||
parsers.Put(typeof(IList<ushort>), ImmutableListParse(ushort.Parse));
|
||||
parsers.Put(typeof(List<ushort>), ListParse(ushort.Parse));
|
||||
parsers.Put(typeof(ISet<ushort>), ImmutableSetParse(ushort.Parse));
|
||||
parsers.Put(typeof(HashSet<ushort>), SetParse(ushort.Parse));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<int>), ImmutableListParse(int.Parse));
|
||||
parsers.Put(typeof(ICollection<int>), ImmutableListParse(int.Parse));
|
||||
parsers.Put(typeof(IList<int>), ImmutableListParse(int.Parse));
|
||||
parsers.Put(typeof(List<int>), ListParse(int.Parse));
|
||||
parsers.Put(typeof(ISet<int>), ImmutableSetParse(int.Parse));
|
||||
parsers.Put(typeof(HashSet<int>), SetParse(int.Parse));
|
||||
parsers.Put(typeof(IEnumerable<uint>), ImmutableListParse(uint.Parse));
|
||||
parsers.Put(typeof(ICollection<uint>), ImmutableListParse(uint.Parse));
|
||||
parsers.Put(typeof(IList<uint>), ImmutableListParse(uint.Parse));
|
||||
parsers.Put(typeof(List<uint>), ListParse(uint.Parse));
|
||||
parsers.Put(typeof(ISet<uint>), ImmutableSetParse(uint.Parse));
|
||||
parsers.Put(typeof(HashSet<uint>), SetParse(uint.Parse));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<long>), ImmutableListParse(long.Parse));
|
||||
parsers.Put(typeof(ICollection<long>), ImmutableListParse(long.Parse));
|
||||
parsers.Put(typeof(IList<long>), ImmutableListParse(long.Parse));
|
||||
parsers.Put(typeof(List<long>), ListParse(long.Parse));
|
||||
parsers.Put(typeof(ISet<long>), ImmutableSetParse(long.Parse));
|
||||
parsers.Put(typeof(HashSet<long>), SetParse(long.Parse));
|
||||
parsers.Put(typeof(IEnumerable<ulong>), ImmutableListParse(ulong.Parse));
|
||||
parsers.Put(typeof(ICollection<ulong>), ImmutableListParse(ulong.Parse));
|
||||
parsers.Put(typeof(IList<ulong>), ImmutableListParse(ulong.Parse));
|
||||
parsers.Put(typeof(List<ulong>), ListParse(ulong.Parse));
|
||||
parsers.Put(typeof(ISet<ulong>), ImmutableSetParse(ulong.Parse));
|
||||
parsers.Put(typeof(HashSet<ulong>), SetParse(ulong.Parse));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<float>), ImmutableListParse(float.Parse));
|
||||
parsers.Put(typeof(ICollection<float>), ImmutableListParse(float.Parse));
|
||||
parsers.Put(typeof(IList<float>), ImmutableListParse(float.Parse));
|
||||
parsers.Put(typeof(List<float>), ListParse(float.Parse));
|
||||
parsers.Put(typeof(ISet<float>), ImmutableSetParse(float.Parse));
|
||||
parsers.Put(typeof(HashSet<float>), SetParse(float.Parse));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<double>), ImmutableListParse(double.Parse));
|
||||
parsers.Put(typeof(ICollection<double>), ImmutableListParse(double.Parse));
|
||||
parsers.Put(typeof(IList<double>), ImmutableListParse(double.Parse));
|
||||
parsers.Put(typeof(List<double>), ListParse(double.Parse));
|
||||
parsers.Put(typeof(ISet<double>), ImmutableSetParse(double.Parse));
|
||||
parsers.Put(typeof(HashSet<double>), SetParse(double.Parse));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<decimal>), ImmutableListParse(decimal.Parse));
|
||||
parsers.Put(typeof(ICollection<decimal>), ImmutableListParse(decimal.Parse));
|
||||
parsers.Put(typeof(IList<decimal>), ImmutableListParse(decimal.Parse));
|
||||
parsers.Put(typeof(List<decimal>), ListParse(decimal.Parse));
|
||||
parsers.Put(typeof(ISet<decimal>), ImmutableSetParse(decimal.Parse));
|
||||
parsers.Put(typeof(HashSet<decimal>), SetParse(decimal.Parse));
|
||||
|
||||
|
||||
parsers.Put(typeof(IEnumerable<DateTime>), ImmutableListParse(DateTime.Parse));
|
||||
parsers.Put(typeof(ICollection<DateTime>), ImmutableListParse(DateTime.Parse));
|
||||
parsers.Put(typeof(IList<DateTime>), ImmutableListParse(DateTime.Parse));
|
||||
parsers.Put(typeof(List<DateTime>), ListParse(DateTime.Parse));
|
||||
parsers.Put(typeof(ISet<DateTime>), ImmutableSetParse(DateTime.Parse));
|
||||
parsers.Put(typeof(HashSet<DateTime>), SetParse(DateTime.Parse));
|
||||
|
||||
parsers.Put(typeof(IEnumerable<TimeSpan>), ImmutableListParse(TimeSpan.Parse));
|
||||
parsers.Put(typeof(ICollection<TimeSpan>), ImmutableListParse(TimeSpan.Parse));
|
||||
parsers.Put(typeof(IList<TimeSpan>), ImmutableListParse(TimeSpan.Parse));
|
||||
parsers.Put(typeof(List<TimeSpan>), ListParse(TimeSpan.Parse));
|
||||
parsers.Put(typeof(ISet<TimeSpan>), ImmutableSetParse(TimeSpan.Parse));
|
||||
parsers.Put(typeof(HashSet<TimeSpan>), SetParse(TimeSpan.Parse));
|
||||
|
||||
return parsers.ToImmutableDictionary();
|
||||
}
|
||||
|
||||
private static Func<Parameter, object> SafeParse<T>(Func<string, T> parse)
|
||||
{
|
||||
return parameter =>
|
||||
{
|
||||
try
|
||||
{
|
||||
return parse(parameter.Value);
|
||||
}
|
||||
catch (OverflowException)
|
||||
{
|
||||
throw ParameterOutOfRange(parameter, typeof(T));
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
throw InvalidParameterFormat(parameter, typeof(T));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new InvalidOperationException(Strings.Format("Unable to parse parameter: '{0}' with value: '{1}' to {2}",
|
||||
parameter.Name, parameter.Value, typeof(T)), e);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static Func<Parameter, object> ListParse<T>(Func<string, T> itemParser)
|
||||
{
|
||||
return parameter =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(parameter.Value))
|
||||
{
|
||||
return new List<T>();
|
||||
}
|
||||
var results = parameter.Value.Split(new[] { ',' }, StringSplitOptions.None)
|
||||
.Where(it => it != null)
|
||||
.Select(it => it.Trim())
|
||||
.Select(itemParser)
|
||||
.ToList();
|
||||
return results;
|
||||
};
|
||||
}
|
||||
|
||||
private static Func<Parameter, object> ImmutableListParse<T>(Func<string, T> itemParser)
|
||||
{
|
||||
return parameter =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(parameter.Value))
|
||||
{
|
||||
return Lists.EmptyList<T>();
|
||||
}
|
||||
var results = parameter.Value.Split(new[] { ',' }, StringSplitOptions.None)
|
||||
.Where(it => it != null)
|
||||
.Select(it => it.Trim())
|
||||
.Select(itemParser)
|
||||
.ToImmutableList();
|
||||
return results;
|
||||
};
|
||||
}
|
||||
|
||||
private static Func<Parameter, object> SetParse<T>(Func<string, T> itemParser)
|
||||
{
|
||||
return parameter =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(parameter.Value))
|
||||
{
|
||||
return new HashSet<T>();
|
||||
}
|
||||
var results = parameter.Value.Split(new[] { ',' }, StringSplitOptions.None)
|
||||
.Where(it => it != null)
|
||||
.Select(it => it.Trim())
|
||||
.Select(itemParser)
|
||||
.ToSet();
|
||||
return results;
|
||||
};
|
||||
}
|
||||
|
||||
private static Func<Parameter, object> ImmutableSetParse<T>(Func<string, T> itemParser)
|
||||
{
|
||||
return parameter =>
|
||||
{
|
||||
if (string.IsNullOrEmpty(parameter.Value))
|
||||
{
|
||||
return Sets.EmptySet<T>();
|
||||
}
|
||||
var results = parameter.Value.Split(new[] { ',' }, StringSplitOptions.None)
|
||||
.Where(it => it != null)
|
||||
.Select(it => it.Trim())
|
||||
.Select(itemParser)
|
||||
.ToImmutableHashSet();
|
||||
return results;
|
||||
};
|
||||
}
|
||||
|
||||
private static ZonedDateTime ParseZonedDateTime(string value)
|
||||
{
|
||||
var dateTime = DateTime.Parse(value);
|
||||
return new ZonedDateTime(Instant.FromDateTimeUtc(dateTime.ToUniversalTime()), DateTimeZone.Utc);
|
||||
}
|
||||
|
||||
private static LocalTime ParseLocalTime(string value)
|
||||
{
|
||||
return LocalTimePattern.ExtendedIsoPattern.Parse(value).Value;
|
||||
}
|
||||
|
||||
private static ArgumentException ParameterOutOfRange(Parameter parameter, Type type)
|
||||
{
|
||||
return new ArgumentException(Strings.Format("Query: '{0}' value: '{1}' is out of range for: '{2}'",
|
||||
parameter.Name, parameter.Value, type));
|
||||
}
|
||||
|
||||
private static ArgumentException InvalidParameterFormat(Parameter parameter, Type type)
|
||||
{
|
||||
return new ArgumentException(Strings.Format("Query '{0}' value: '{1}' format is invalid for: '{2}'",
|
||||
parameter.Name, parameter.Value, type));
|
||||
}
|
||||
|
||||
private class Parameter
|
||||
{
|
||||
internal string Name { get; private set; }
|
||||
internal string Value { get; private set; }
|
||||
|
||||
private Parameter(string name, string value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
internal static Parameter Of(string name, string value)
|
||||
{
|
||||
return new Parameter(name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal enum ParameterType
|
||||
{
|
||||
Undefined,
|
||||
Query,
|
||||
Path,
|
||||
Header
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Nancy" version="1.4.1" targetFramework="net45" />
|
||||
<package id="NodaTime" version="1.3.1" targetFramework="net45" />
|
||||
<package id="Sharpility" version="1.2.1" targetFramework="net45" />
|
||||
<package id="System.Collections.Immutable" version="1.1.37" targetFramework="net45" />
|
||||
</packages>
|
Loading…
x
Reference in New Issue
Block a user