/// <summary> /// Copy code, headers and body from nested response /// </summary> /// <param name="context"></param> /// <param name="headerJsonRpcSerializer"></param> /// <returns></returns> public async Task Write(HandlingContext context, HeaderJsonRpcSerializer headerJsonRpcSerializer) { var sink = context.OriginalHttpContext.Response; sink.StatusCode = Source.StatusCode; sink.ContentLength = null; if (Source.Headers != null) { foreach (var header in Source.Headers) { sink.Headers.Append(header.Key, header.Value); } } // TODO is this sane? Maybe return body since we're already violating protocol? if (!context.WriteResponse) { return; } if (!(Source.Body is MemoryStream ms)) { throw new JsonRpcInternalException("Expected MemoryStream to read response"); } ms.Seek(0, SeekOrigin.Begin); await ms.CopyToAsync(sink.Body, 81920, context.OriginalHttpContext.RequestAborted); }
public RequestHandler(IJsonRpcErrorFactory errorFactory, HeaderJsonRpcSerializer headerJsonRpcSerializer, INestedContextFactory nestedContextFactory, IResponseReader responseReader, IOptions <JsonRpcOptions> options, ILogger <RequestHandler> log) { this.errorFactory = errorFactory; this.headerJsonRpcSerializer = headerJsonRpcSerializer; this.nestedContextFactory = nestedContextFactory; this.responseReader = responseReader; this.log = log; this.options = options.Value; }
protected internal JsonRpcClientBase(HttpClient client, IJsonRpcSerializer serializer, HeaderJsonRpcSerializer headerJsonRpcSerializer, JsonRpcClientOptionsBase options, IJsonRpcIdGenerator jsonRpcIdGenerator, ILogger log) { Client = client; Serializer = serializer; HeaderJsonRpcSerializer = headerJsonRpcSerializer; Options = options; this.JsonRpcIdGenerator = jsonRpcIdGenerator; this.log = log; InitializeClient(client, options); }
public BatchJsonRpcResult(IJsonRpcCallContext context, HeaderJsonRpcSerializer headerJsonRpcSerializer, IJsonRpcSerializer serializer) { this.context = context ?? throw new ArgumentNullException(nameof(context)); if (context.SingleResponse != null) { throw new ArgumentOutOfRangeException(nameof(context), "Expected batch response"); } this.responses = CreateDictionary(context.BatchResponse); this.headerJsonRpcSerializer = headerJsonRpcSerializer; this.serializer = serializer; }
public void Setup() { testEnvironment = new TestEnvironment(services => { }); var serializer = new HeaderJsonRpcSerializer(); var log = testEnvironment.ServiceProvider.GetRequiredService <ILogger <RequestReader> >(); requestReaderMock = new Mock <RequestReader>(serializer, log) { CallBase = true }; }
public void Setup() { testEnvironment = new TestEnvironment(services => { }); errorFactoryMock = new Mock <IJsonRpcErrorFactory>(); serializer = new HeaderJsonRpcSerializer(); var log = testEnvironment.ServiceProvider.GetRequiredService <ILogger <ResponseReader> >(); responseReaderMock = new Mock <ResponseReader>(errorFactoryMock.Object, serializer, log) { CallBase = true }; }
/// <summary> /// Set code 200, set content-type, add headers if not null, write json body if not null /// </summary> /// <returns></returns> public async Task Write(HandlingContext context, HeaderJsonRpcSerializer headerJsonRpcSerializer) { var sink = context.OriginalHttpContext.Response; if (Headers != null) { foreach (var header in Headers) { sink.Headers.Append(header.Key, header.Value); } } sink.StatusCode = 200; sink.ContentLength = null; var responseMediaType = new MediaTypeHeaderValue(JsonRpcConstants.ContentType) { Encoding = context.RequestEncoding }; sink.ContentType = responseMediaType.ToString(); if (!context.WriteResponse) { return; } await using (var writer = new HttpResponseStreamWriter(sink.Body, context.RequestEncoding)) { using var jsonWriter = new JsonTextWriter(writer) { CloseOutput = false, AutoCompleteOnClose = false, Formatting = headerJsonRpcSerializer.Settings.Formatting }; await Value.WriteToAsync(jsonWriter, context.OriginalHttpContext.RequestAborted); } SetResponseContextItem(context.OriginalHttpContext); }
/// <summary> /// Serialize Error as json response /// </summary> /// <param name="errorFactory"></param> /// <param name="value"></param> /// <param name="headerJsonRpcSerializer"></param> /// <returns></returns> public static JToken ConvertErrorToResponse(this IJsonRpcErrorFactory errorFactory, IError value, HeaderJsonRpcSerializer headerJsonRpcSerializer) { var error = new ErrorResponse <object> { Error = new Error <object> { Code = value.Code, Message = value.Message, Data = value.GetData() } }; return(JToken.FromObject(error, headerJsonRpcSerializer.Serializer)); }
/// <summary> /// Serialize Exception as json response /// </summary> /// <param name="errorFactory"></param> /// <param name="e"></param> /// <param name="headerJsonRpcSerializer"></param> /// <returns></returns> public static JToken ConvertExceptionToResponse(this IJsonRpcErrorFactory errorFactory, Exception e, HeaderJsonRpcSerializer headerJsonRpcSerializer) { var value = e is JsonRpcErrorResponseException responseException ? responseException.Error : errorFactory.Exception(e); return(errorFactory.ConvertErrorToResponse(value, headerJsonRpcSerializer)); }
public JsonRpcFormatter(HeaderJsonRpcSerializer headerJsonRpcSerializer, ArrayPool <char> charPool, IOptions <MvcOptions> mvcOptions, IOptions <MvcNewtonsoftJsonOptions> mvcNewtonsoftJsonOptions) : base(headerJsonRpcSerializer.Settings, charPool, mvcOptions.Value, mvcNewtonsoftJsonOptions.Value) { this.headerJsonRpcSerializer = headerJsonRpcSerializer; }
public JsonRpcFormatterPublic(HeaderJsonRpcSerializer headerJsonRpcSerializer, ArrayPool <char> charPool) : base(headerJsonRpcSerializer, charPool) { }
public JsonRpcFormatter(HeaderJsonRpcSerializer headerJsonRpcSerializer, ArrayPool <char> charPool) : base(headerJsonRpcSerializer.Settings, charPool) { this.headerJsonRpcSerializer = headerJsonRpcSerializer; }
public ResponseReader(IJsonRpcErrorFactory errorFactory, HeaderJsonRpcSerializer headerJsonRpcSerializer, ILogger <ResponseReader> log) { this.errorFactory = errorFactory; this.headerJsonRpcSerializer = headerJsonRpcSerializer; this.log = log; }
public TestClient(HttpClient client, IJsonRpcSerializer serializer, HeaderJsonRpcSerializer headerJsonRpcSerializer, IOptions <TestOptions> options, IJsonRpcIdGenerator jsonRpcIdGenerator, ILogger log) : base(client, serializer, headerJsonRpcSerializer, options.Value, jsonRpcIdGenerator, log) { }
public RequestReader(HeaderJsonRpcSerializer headerJsonRpcSerializer, ILogger <RequestReader> log) { this.headerJsonRpcSerializer = headerJsonRpcSerializer; this.log = log; }
public JsonRpcFormatterPublic(HeaderJsonRpcSerializer headerJsonRpcSerializer, ArrayPool <char> charPool, IOptions <MvcOptions> mvcOptions, IOptions <MvcNewtonsoftJsonOptions> mvcNewtonsoftJsonOptions) : base(headerJsonRpcSerializer, charPool, mvcOptions, mvcNewtonsoftJsonOptions) { }
/// <summary> /// Serialize Exception as json response /// </summary> /// <param name="errorFactory"></param> /// <param name="e"></param> /// <param name="headerJsonRpcSerializer"></param> /// <returns></returns> public static JToken ConvertExceptionToResponse(this IJsonRpcErrorFactory errorFactory, Exception e, HeaderJsonRpcSerializer headerJsonRpcSerializer) { return(errorFactory.ConvertErrorToResponse(errorFactory.Exception(e), headerJsonRpcSerializer)); }