private static string GetMethodDetails(Controller controller, Endpoint endpoint, bool async, bool raw) { var cancellationToken = endpoint.GetRequestModifiers().OfType <CancellationTokenModifier>().SingleOrDefault(); var clientDependency = new ClientDependency($"I{Settings.ClientInterfaceName}Wrapper"); var requestModifiers = endpoint.GetRequestModifiers().ToList(); var bodyParameter = endpoint.GetBodyParameter(); string bodyVariable = bodyParameter?.Name ?? "null"; var responseTypes = endpoint.GetResponseTypes(); var routeConstraints = endpoint.GetRouteConstraints(controller); return ($@"{string.Join(Environment.NewLine, routeConstraints.Select(WriteRouteConstraint).NotNull())} {GetEndpointInfoVariables(controller, endpoint)} string url = $@""{GetRoute(controller, endpoint)}""; HttpResponseMessage response = null; response = {GetAwait(async)}HttpOverride.GetResponseAsync({GetHttpMethod(endpoint.HttpType)}, url, null, {cancellationToken.Name}){GetAsyncEnding(async)}; if(response == null) {{ response = {GetAwait(async)}{clientDependency.GetDependencyName($"I{Settings.ClientInterfaceName}")}.{nameof(IClientWrapper.ClientWrapper)} .Request(url) {string.Join($" {Environment.NewLine}", requestModifiers.Select(WriteRequestModifiers).NotNull())} .AllowAnyHttpStatus() {GetHttpMethod(endpoint)} {GetAsyncEnding(async)}; {GetAwait(async)}HttpOverride.OnNonOverridedResponseAsync({GetHttpMethod(endpoint.HttpType)}, url, {bodyVariable}, response, {cancellationToken.Name}){GetAsyncEnding(async)}; }} {string.Join(Environment.NewLine, responseTypes.Select(x => WriteResponseType(x, async, raw)).NotNull())} {WriteActionResultReturn(endpoint, async, raw)}"); }
private static string WriteRequestModifiers(IRequestModifier modifier) { string tabs = $@" "; if (modifier is CookieModifier cm) { return($@"{tabs}.WithCookies({cm.Name})"); } else if (modifier is HeadersModifier hm) { return($@"{tabs}.WithHeaders({hm.Name})"); } else if (modifier is RequestModifierDependency rm) { return($@"{tabs}.WithRequestModifiers({rm.GetDependencyName($"I{Settings.ClientInterfaceName}")})"); } else if (modifier is SecurityModifier sm) { return($@"{tabs}.WithAuth({sm.Name})"); } else if (modifier is TimeoutModifier tm) { var clientDependency = new ClientDependency(null); return($@"{tabs}.WithTimeout({tm.Name} ?? {clientDependency.GetDependencyName($"I{Settings.ClientInterfaceName}")}.{nameof(IClientWrapper.Timeout)})"); } else if (modifier is ConstantHeader ch) { return($@"{tabs}.WithHeader(""{ch.Key}"", ""{ch.Value}"")"); } else if (modifier is ParameterHeader ph) { return($@"{tabs}.WithHeader(""{ph.Name}"", {ph.Name})"); } else if (modifier is CancellationTokenModifier ctm) { return(null); } else { return($@"#warning IRequestModifier of type {modifier.GetType().Name} is not supported"); } }