private void WriteOperation(CodeWriter writer, RestClientMethod operation, bool async) { using var methodScope = writer.AmbientScope(); CSharpType?bodyType = operation.ReturnType; CSharpType?headerModelType = operation.HeaderModel?.Type; CSharpType responseType = bodyType switch { null when headerModelType != null => new CSharpType(typeof(ResponseWithHeaders <>), headerModelType), { } when headerModelType == null => new CSharpType(typeof(Response <>), bodyType), { } => new CSharpType(typeof(ResponseWithHeaders <>), bodyType, headerModelType),
private void WriteRequestCreation(CodeWriter writer, RestClientMethod clientMethod) { using var methodScope = writer.AmbientScope(); var methodName = CreateRequestMethodName(clientMethod.Name); writer.Append($"internal {typeof(HttpMessage)} {methodName}("); var parameters = clientMethod.Parameters; foreach (Parameter clientParameter in parameters) { writer.Append($"{clientParameter.Type} {clientParameter.Name:D},"); } writer.RemoveTrailingComma(); writer.Line($")"); using (writer.Scope()) { var message = new CodeWriterDeclaration("message"); var request = new CodeWriterDeclaration("request"); var uri = new CodeWriterDeclaration("uri"); writer.Line($"var {message:D} = {PipelineField}.CreateMessage();"); writer.Line($"var {request:D} = {message}.Request;"); var method = clientMethod.Request.HttpMethod; writer.Line($"{request}.Method = {typeof(RequestMethod)}.{method.ToRequestMethodName()};"); writer.Line($"var {uri:D} = new RawRequestUriBuilder();"); foreach (var segment in clientMethod.Request.PathSegments) { if (!segment.Value.IsConstant && segment.Value.Reference.Name == "nextLink") { if (segment.IsRaw) { // Artificial nextLink needs additional logic for relative versus absolute links WritePathSegment(writer, uri, segment, "AppendRawNextLink"); } else { // Natural nextLink parameters need to use a different method to parse path and query elements WritePathSegment(writer, uri, segment, "AppendRaw"); } } else { WritePathSegment(writer, uri, segment); } } //TODO: Duplicate code between query and header parameter processing logic foreach (var queryParameter in clientMethod.Request.Query) { WriteQueryParameter(writer, uri, queryParameter); } writer.Line($"{request}.Uri = {uri};"); foreach (var header in clientMethod.Request.Headers) { WriteHeader(writer, request, header); } switch (clientMethod.Request.Body) { case SchemaRequestBody body: using (WriteValueNullCheck(writer, body.Value)) { WriteSerializeContent( writer, request, body.Serialization, w => WriteConstantOrParameter(w, body.Value, ignoreNullability: true)); } break; case BinaryRequestBody binaryBody: using (WriteValueNullCheck(writer, binaryBody.Value)) { writer.Append($"{request}.Content = {typeof(RequestContent)}.Create("); WriteConstantOrParameter(writer, binaryBody.Value); writer.Line($");"); } break; case TextRequestBody textBody: using (WriteValueNullCheck(writer, textBody.Value)) { writer.Append($"{request}.Content = new {typeof(StringRequestContent)}("); WriteConstantOrParameter(writer, textBody.Value); writer.Line($");"); } break; case FlattenedSchemaRequestBody flattenedSchemaRequestBody: var modelVariable = new CodeWriterDeclaration("model"); writer.Append($"var {modelVariable:D} = ") .WriteInitialization(flattenedSchemaRequestBody.ObjectType, flattenedSchemaRequestBody.Initializers) .Line($";"); WriteSerializeContent( writer, request, flattenedSchemaRequestBody.Serialization, w => w.Append(modelVariable)); break; case null: break; default: throw new NotImplementedException(clientMethod.Request.Body?.GetType().FullName); } writer.Line($"return {message};"); } writer.Line(); }