Class SimpleJsonService
A minimal framework for a JSON-based REST service.
Inheritance
Namespace: LaunchDarkly.TestHelpers.HttpTest
Assembly: LaunchDarkly.TestHelpers.dll
Syntax
public sealed class SimpleJsonService : Object
Remarks
This class builds on Handlers and SimpleRouter to provide a simplified structure for implementing a basic REST service that can use JSON-serializable types for request and response bodies. For very basic services that are used in a test scenario, this is much lighter-weight and more portable than using ASP.NET Core or ASP.NET.
The basic pattern is to call the Route
methods to map any number of endpoint
handlers. Each handler method takes an IRequestContext as the first
parameter, and can optionally take a value of some JSON-deserializable type as a second
parameter, which will be automatically parsed from the request body. Its return value is
either a SimpleResponse if there is no response body, or a
SimpleResponse<T> containing a value that will be serialized to JSON as a
response body.
For simple path parameters, you may inclue a regex containing capture groups in the path, and then call GetPathParam(Int32) to get the value. Any path containing parentheses is assumed to be a regex, otherwise it is taken as a literal.
This class uses System.Text.Json
for JSON conversions.
Constructors
SimpleJsonService()
Creates a new instance.
Declaration
public SimpleJsonService()
Fields
SerializerOptions
Options for System.Text.Json to enable the standard behavior of camelcasing property names.
Declaration
public static JsonSerializerOptions SerializerOptions
Field Value
Type | Description |
---|---|
System.Text.Json.JsonSerializerOptions |
Properties
Handler
Returns the stable Handler that is the external entry point to this
delegator. This is used implicitly if you use a SimpleJsonService
anywhere that
a Handler is expected.
Declaration
public Handler Handler { get; }
Property Value
Type | Description |
---|---|
Handler |
Methods
Route(HttpMethod, String, Func<IRequestContext, SimpleResponse>)
Adds a synchronous endpoint handler with no request body parameters and no response body.
Declaration
public void Route(HttpMethod method, string path, Func<IRequestContext, SimpleResponse> syncHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod | method | the HTTP method |
System.String | path | the endpoint path |
System.Func<IRequestContext, SimpleResponse> | syncHandler | the handler |
Route(HttpMethod, String, Func<IRequestContext, Task<SimpleResponse>>)
Adds an asynchronous endpoint handler with no request body parameters and no response body.
Declaration
public void Route(HttpMethod method, string path, Func<IRequestContext, Task<SimpleResponse>> asyncHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod | method | the HTTP method |
System.String | path | the endpoint path |
System.Func<IRequestContext, System.Threading.Tasks.Task<SimpleResponse>> | asyncHandler | the handler |
Route<TInput>(HttpMethod, String, Func<IRequestContext, TInput, SimpleResponse>)
Adds a synchronous endpoint handler with no request body parameters and a JSON response.
Declaration
public void Route<TInput>(HttpMethod method, string path, Func<IRequestContext, TInput, SimpleResponse> syncHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod | method | the HTTP method |
System.String | path | the endpoint path |
System.Func<IRequestContext, TInput, SimpleResponse> | syncHandler | the handler |
Type Parameters
Name | Description |
---|---|
TInput |
Route<TInput>(HttpMethod, String, Func<IRequestContext, TInput, Task<SimpleResponse>>)
Adds an asynchronous endpoint handler with a JSON request body parameter and no response body.
Declaration
public void Route<TInput>(HttpMethod method, string path, Func<IRequestContext, TInput, Task<SimpleResponse>> asyncHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod | method | the HTTP method |
System.String | path | the endpoint path |
System.Func<IRequestContext, TInput, System.Threading.Tasks.Task<SimpleResponse>> | asyncHandler | the handler |
Type Parameters
Name | Description |
---|---|
TInput |
Route<TOutput>(HttpMethod, String, Func<IRequestContext, SimpleResponse<TOutput>>)
Adds a synchronous endpoint handler with no request body parameters and a JSON response.
Declaration
public void Route<TOutput>(HttpMethod method, string path, Func<IRequestContext, SimpleResponse<TOutput>> syncHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod | method | the HTTP method |
System.String | path | the endpoint path |
System.Func<IRequestContext, SimpleResponse<TOutput>> | syncHandler | the handler |
Type Parameters
Name | Description |
---|---|
TOutput |
Route<TOutput>(HttpMethod, String, Func<IRequestContext, Task<SimpleResponse<TOutput>>>)
Adds an asynchronous endpoint handler with no request body parameters and a JSON response.
Declaration
public void Route<TOutput>(HttpMethod method, string path, Func<IRequestContext, Task<SimpleResponse<TOutput>>> asyncHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod | method | the HTTP method |
System.String | path | the endpoint path |
System.Func<IRequestContext, System.Threading.Tasks.Task<SimpleResponse<TOutput>>> | asyncHandler | the handler |
Type Parameters
Name | Description |
---|---|
TOutput |
Route<TInput, TOutput>(HttpMethod, String, Func<IRequestContext, TInput, SimpleResponse<TOutput>>)
Adds an asynchronous endpoint handler with a JSON request body parameter and a JSON response.
Declaration
public void Route<TInput, TOutput>(HttpMethod method, string path, Func<IRequestContext, TInput, SimpleResponse<TOutput>> syncHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod | method | the HTTP method |
System.String | path | the endpoint path |
System.Func<IRequestContext, TInput, SimpleResponse<TOutput>> | syncHandler | the handler |
Type Parameters
Name | Description |
---|---|
TInput | |
TOutput |
Route<TInput, TOutput>(HttpMethod, String, Func<IRequestContext, TInput, Task<SimpleResponse<TOutput>>>)
Adds an asynchronous endpoint handler with a JSON request body parameter and a JSON response.
Declaration
public void Route<TInput, TOutput>(HttpMethod method, string path, Func<IRequestContext, TInput, Task<SimpleResponse<TOutput>>> asyncHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Net.Http.HttpMethod | method | the HTTP method |
System.String | path | the endpoint path |
System.Func<IRequestContext, TInput, System.Threading.Tasks.Task<SimpleResponse<TOutput>>> | asyncHandler | the handler |
Type Parameters
Name | Description |
---|---|
TInput | |
TOutput |
Operators
Implicit(SimpleJsonService to Handler)
Declaration
public static implicit operator Handler(SimpleJsonService me)
Parameters
Type | Name | Description |
---|---|---|
SimpleJsonService | me |
Returns
Type | Description |
---|---|
Handler |