protected override void RenderImplementation()
        {
            string httpMethodName = HttpMethod.ToString().ToLower();             //Method is always uppercase.
            //deal with parameters
            var parameters = ParameterDescriptions.Select(d =>
                                                          new CodeParameterDeclarationExpression(TypeMapper.MapCodeTypeReferenceToTsText(d.ParameterTypeReference), d.Name))
                             .ToList();

            var returnTypeText = TypeMapper.MapCodeTypeReferenceToTsText(ReturnTypeReference);

            if (returnTypeText == "any" || String.IsNullOrEmpty(returnTypeText) || returnTypeText == "response")
            {
                returnTypeText = "void";
            }
            var callbackTypeText = String.Format("(data : {0}) => any", returnTypeText);

            Debug.WriteLine("callback: " + callbackTypeText);
            var callbackTypeReference = new CodeSnipetTypeReference(callbackTypeText);

            parameters.Add(new CodeParameterDeclarationExpression(callbackTypeReference, "callback"));
            Method.Parameters.AddRange(parameters.ToArray());

            if (RequestBodyCodeTypeReference != null)
            {
                CodeParameterDeclarationExpression p = new CodeParameterDeclarationExpression(RequestBodyCodeTypeReference, "requestBody");
                Method.Parameters.Add(p);
            }

            if (settings.HandleHttpRequestHeaders)
            {
                Method.Parameters.Add(new CodeParameterDeclarationExpression(
                                          "() => {[header: string]: string}", "headersHandler?"));
            }

            var    jsUriQuery = UriQueryHelper.CreateUriQueryForTs(RelativePath, ParameterDescriptions);
            string uriText    = jsUriQuery == null ? $"this.baseUri + '{RelativePath}'" :
                                RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'");

            string headerHandlerCall = settings.HandleHttpRequestHeaders ? ", headersHandler" : String.Empty;

            if (httpMethodName == "get" || httpMethodName == "delete")
            {
                Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{httpMethodName}({uriText}, callback, this.error, this.statusCode{headerHandlerCall});"));
            }
            else if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
            {
                if (RequestBodyCodeTypeReference == null)                 // no body
                {
                    Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{httpMethodName}({uriText}, null, callback, this.error, this.statusCode, '{contentType}'{headerHandlerCall});"));
                }
                else
                {
                    Method.Statements.Add(new CodeSnippetStatement($"this.httpClient.{httpMethodName}({uriText}, requestBody, callback, this.error, this.statusCode, '{contentType}'{headerHandlerCall});"));
                }
            }
            else
            {
                Debug.Assert(false, $"How come with {httpMethodName}?");
            }
        }
Ejemplo n.º 2
0
        public ApiMethodModel(ApiDescription apiDescription)
        {
            Method     = apiDescription.HttpMethod.Method;
            MethodName = (apiDescription.ActionDescriptor as ReflectedHttpActionDescriptor)?.MethodInfo.Name;

            int urlLenth = apiDescription.RelativePath.IndexOf('?') == -1 ? apiDescription.RelativePath.Length : apiDescription.RelativePath.IndexOf('?');

            Url = apiDescription.RelativePath.Substring(0, urlLenth); //отсекли параметры

            ControllerName        = apiDescription.ActionDescriptor.ControllerDescriptor.ControllerName;
            ActionName            = apiDescription.ActionDescriptor.ActionName;
            ParameterDescriptions = apiDescription.ParameterDescriptions;

            var parList = new List <string>();

            foreach (var parameter in ParameterDescriptions.Where(p => p.Source == ApiParameterSource.FromUri))
            {
                parList.Add($"{parameter.Name}: {parameter.Name}");
            }
            if (parList.Any())
            {
                Params = "{" + $"{string.Join(",", parList)}" + "}";
            }
            ParametersString = string.Join(", ", ParameterDescriptions.Select(p => p.Name));

            var nonUriParam = ParameterDescriptions.FirstOrDefault(p => p.Source != ApiParameterSource.FromUri);

            if (nonUriParam != null)
            {
                //Определяем тип параметра, и если  это DataSourceRequest, то используем функцию dsRead(url, options, filter)
                if (typeof(IDataSourceRequest).IsAssignableFrom(nonUriParam.ParameterDescriptor.ParameterType))
                {
                    FunctionType = FunctionType.DsRead;
                }

                //Если тип возвращаемого параметра IHttpResponseFile, то загрузка файла методом core.dsDownload
                if (typeof(IHttpResponseFile).IsAssignableFrom(apiDescription.ResponseDescription.DeclaredType))
                {
                    FunctionType = FunctionType.DsDownload;
                }

                NonUriParam = nonUriParam.Name;
            }
        }
Ejemplo n.º 3
0
        protected override void RenderImplementation()
        {
            string httpMethodName = HttpMethod.ToString().ToLower();             //Method is always uppercase.

            //deal with parameters
            CodeParameterDeclarationExpression[] parameters = ParameterDescriptions.Select(d =>
                                                                                           new CodeParameterDeclarationExpression(TypeMapper.MapCodeTypeReferenceToTsText(d.ParameterTypeReference), d.Name))
                                                              .ToArray();

            Method.Parameters.AddRange(parameters);

            if (RequestBodyCodeTypeReference != null)
            {
                CodeParameterDeclarationExpression p = new CodeParameterDeclarationExpression(RequestBodyCodeTypeReference, "requestBody");
                Method.Parameters.Add(p);
            }

            if (settings.HandleHttpRequestHeaders)
            {
                Method.Parameters.Add(new CodeParameterDeclarationExpression(
                                          "() => {[header: string]: string}", "headersHandler?"));
            }

            string jsUriQuery = UriQueryHelper.CreateUriQueryForTs(RelativePath, ParameterDescriptions);
            string uriText    = jsUriQuery == null ? $"this.baseUri + '{RelativePath}'" :
                                RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'");

            if (ReturnTypeReference != null && ReturnTypeReference.BaseType == "System.String" && ReturnTypeReference.ArrayElementType == null)            //stringAsString is for .NET Core Web API
            {
                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, {OptionsForString}).then(d => d.data);"));                     //todo: type cast is not really needed.
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
                {
                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, null, {OptionsForString}).then(d => d.data);"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, JSON.stringify(requestBody), {ContentOptionsForString}).then(d => d.data);"));
                    }

                    return;
                }
            }
            else if (returnTypeText == AxiosHttpStringResponse)            //translated from response to this
            {
                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, {OptionsForResponse});"));
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
                {
                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, null, {OptionsForResponse});"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, JSON.stringify(requestBody), {ContentOptionsForResponse});"));
                    }

                    return;
                }
            }
            else if (returnTypeText == AxiosHttpResponse)             // client should care about only status
            {
                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, {Options});"));
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
                {
                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, null, {Options});"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, JSON.stringify(requestBody), {ContentOptionsForString});"));
                    }

                    return;
                }
            }
            else
            {
                string returnTypeCast = returnTypeText == null ? String.Empty : $"<{returnTypeText}>";

                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    if (returnTypeText == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, {OptionsForResponse});"));                         //only http response needed
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}{returnTypeCast}({uriText}, {Options}).then(d => d.data);"));
                    }
                }
                else if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
                {
                    if (returnTypeText == null)                    //http response
                    {
                        if (RequestBodyCodeTypeReference == null)  //no content body
                        {
                            Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, null, {OptionsForResponse});"));
                        }
                        else
                        {
                            Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}({uriText}, JSON.stringify(requestBody), {ContentOptionsForResponse});"));
                        }
                    }
                    else                                          // type is returned
                    {
                        if (RequestBodyCodeTypeReference == null) // no body
                        {
                            Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}{returnTypeCast}({uriText}, null, {Options}).then(d => d.data);"));
                        }
                        else
                        {
                            Method.Statements.Add(new CodeSnippetStatement($"return Axios.{httpMethodName}{returnTypeCast}({uriText}, JSON.stringify(requestBody), {OptionsWithContent}).then(d => d.data);"));
                        }
                    }
                }
                else
                {
                    Debug.Assert(false, $"How come with {httpMethodName}?");
                }
            }
        }
Ejemplo n.º 4
0
        protected override void RenderImplementation()
        {
            var httpMethodName = HttpMethod.ToString().ToLower();             //Method is always uppercase.
            //deal with parameters
            var parameters = ParameterDescriptions.Select(d =>
                                                          new CodeParameterDeclarationExpression(Poco2TsGen.TranslateToClientTypeReference(d.ParameterDescriptor.ParameterType), d.Name))
                             .ToArray();

            //parameters.Add(new CodeParameterDeclarationExpression(callbackTypeReference, "callback"));

            Method.Parameters.AddRange(parameters);

            if (RequestBodyCodeTypeReference != null)
            {
                var p = new CodeParameterDeclarationExpression(RequestBodyCodeTypeReference, "requestBody");
                Method.Parameters.Add(p);
            }

            var jsUriQuery = UriQueryHelper.CreateUriQueryForTs(RelativePath, ParameterDescriptions);
            var uriText    = jsUriQuery == null ? $"this.baseUri + '{RelativePath}'" :
                             RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'");

            // var mapFunction = returnTypeText == NG2HttpResponse ? String.Empty : ".map(response=> response.json())";

            if (ReturnTypeReference != null && ReturnTypeReference.BaseType == "System.String" && ReturnTypeReference.ArrayElementType == null)            //stringAsString is for .NET Core Web API
            {
                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, {{ responseType: 'text' }});"));
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put")
                {
                    if (String.IsNullOrEmpty(contentType))
                    {
                        contentType = "application/json;charset=UTF-8";
                    }

                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, null, {{headers: {{ 'Content-Type': '{contentType}' }}, responseType: 'text' }});"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, JSON.stringify(requestBody), {{ headers: {{ 'Content-Type': '{contentType}' }}, responseType: 'text' }});"));
                    }

                    return;
                }
            }
            else if (returnTypeText == NG2HttpBlobResponse)            //translated from blobresponse to this
            {
                const string optionForStream = "{ observe: 'response', responseType: 'blob' }";

                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, {optionForStream});"));
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put")
                {
                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, null, {optionForStream});"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, JSON.stringify(requestBody), {optionForStream});"));
                    }

                    return;
                }
            }
            else if (returnTypeText == NG2HttpStringResponse)            //translated from response to this
            {
                const string optionForActionResult = "{ observe: 'response', responseType: 'text' }";

                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, {optionForActionResult});"));
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put")
                {
                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, null, {optionForActionResult});"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}({uriText}, JSON.stringify(requestBody), {{ headers: {{ 'Content-Type': '{contentType}' }}, observe: 'response', responseType: 'text' }});"));
                    }

                    return;
                }
            }
            else
            {
                var typeCast = returnTypeText == null ? "<Response>" : $"<{returnTypeText}>";
                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}{typeCast}({uriText});"));
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put")
                {
                    if (String.IsNullOrEmpty(contentType))
                    {
                        contentType = "application/json;charset=UTF-8";
                    }

                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}{typeCast}({uriText}, null, {{ headers: {{ 'Content-Type': '{contentType}' }} }});"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return this.http.{httpMethodName}{typeCast}({uriText}, JSON.stringify(requestBody), {{ headers: {{ 'Content-Type': '{contentType}' }} }});"));
                    }

                    return;
                }
            }

            Debug.Assert(false, "How come?");
        }
Ejemplo n.º 5
0
        protected override void RenderImplementation()
        {
            var httpMethodName = HttpMethod.ToString().ToLower();

            string contentOptionsWithHeadersHandlerForString = $"{{ method: '{httpMethodName}', headers: headersHandler ? Object.assign(headersHandler(), {{ 'Content-Type': '{contentType}' }}): {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}";
            var    ContentOptionsForString = settings.HandleHttpRequestHeaders ? contentOptionsWithHeadersHandlerForString : $"{{ method: '{httpMethodName}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}";

            string contentOptionsWithHeadersHandlerForResponse = $"{{ method: '{httpMethodName}', headers: headersHandler ? Object.assign(headersHandler(), {{ 'Content-Type': '{contentType}' }}): {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}";
            var    ContentOptionsForResponse = settings.HandleHttpRequestHeaders ? contentOptionsWithHeadersHandlerForResponse : $"{{ method: '{httpMethodName}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}";

            string optionsWithHeadersHandlerAndContent = $"{{ method: '{httpMethodName}', headers: headersHandler ? Object.assign(headersHandler(), {{ 'Content-Type': '{contentType}' }}): {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}";
            var    OptionsWithContent = settings.HandleHttpRequestHeaders ? optionsWithHeadersHandlerAndContent : $"{{ method: '{httpMethodName}', headers: {{ 'Content-Type': '{contentType}' }}, body: JSON.stringify(requestBody) }}";

            string optionsWithHeadersHandlerForString = $"{{ method: '{httpMethodName}', headers: headersHandler ? headersHandler() : undefined }}";
            var    OptionsForString = settings.HandleHttpRequestHeaders ? optionsWithHeadersHandlerForString : $"{{ method: '{httpMethodName}' }}";

            string optionsWithHeadersHandlerForResponse = $"{{ method: '{httpMethodName}', headers: headersHandler ? headersHandler() : undefined }}";
            var    OptionsForResponse = settings.HandleHttpRequestHeaders ? optionsWithHeadersHandlerForResponse : $"{{ method: '{httpMethodName}' }}";

            string optionsWithHeadersHandler = $"{{ method: '{httpMethodName}', headers: headersHandler ? headersHandler() : undefined }}";
            var    Options = settings.HandleHttpRequestHeaders ? optionsWithHeadersHandler : $"{{ method: '{httpMethodName}' }}";

            CodeParameterDeclarationExpression[] parameters = ParameterDescriptions.Select(d =>
                                                                                           new CodeParameterDeclarationExpression(TypeMapper.MapCodeTypeReferenceToTsText(d.ParameterTypeReference), d.Name))
                                                              .ToArray();

            Method.Parameters.AddRange(parameters);

            if (RequestBodyCodeTypeReference != null)
            {
                CodeParameterDeclarationExpression p = new CodeParameterDeclarationExpression(RequestBodyCodeTypeReference, "requestBody");
                Method.Parameters.Add(p);
            }

            if (settings.HandleHttpRequestHeaders)
            {
                Method.Parameters.Add(new CodeParameterDeclarationExpression(
                                          "() => {[header: string]: string}", "headersHandler?"));
            }

            string jsUriQuery = UriQueryHelper.CreateUriQueryForTs(RelativePath, ParameterDescriptions);
            string uriText    = jsUriQuery == null ? $"this.baseUri + '{RelativePath}'" :
                                RemoveTrialEmptyString($"this.baseUri + '{jsUriQuery}'");

            if (ReturnTypeReference != null && ReturnTypeReference.BaseType == "System.String" && ReturnTypeReference.ArrayElementType == null)            //stringAsString is for .NET Core Web API
            {
                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForString}).then(d => d.text());"));                     //todo: type cast is not really needed.
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
                {
                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForString}).then(d => d.text());"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {ContentOptionsForString}).then(d => d.text());"));
                    }

                    return;
                }
            }
            //else if (returnTypeText == FetchHttpStringResponse)//translated from response to this
            //{
            //	if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
            //	{
            //		Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForResponse}).then(d => d.text());"));
            //		return;
            //	}

            //	if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
            //	{
            //		if (RequestBodyCodeTypeReference == null)
            //		{
            //			Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, null, {OptionsForResponse}).then(d => d.text());"));
            //		}
            //		else
            //		{
            //			Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, JSON.stringify(requestBody), {ContentOptionsForResponse});"));
            //		}

            //		return;
            //	}

            //}
            else if (returnTypeText == FetchHttpResponse)             // client should care about only status
            {
                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {Options});"));
                    return;
                }

                if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
                {
                    if (RequestBodyCodeTypeReference == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {Options});"));
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsWithContent});"));
                    }

                    return;
                }
            }
            else
            {
                string returnTypeCast = returnTypeText == null ? String.Empty : $"<{returnTypeText}>";

                if (httpMethodName == "get" || httpMethodName == "delete")
                {
                    if (returnTypeText == null)
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForResponse});"));                         //only http response needed
                    }
                    else
                    {
                        Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {Options}).then(d => d.json());"));
                    }
                }
                else if (httpMethodName == "post" || httpMethodName == "put" || httpMethodName == "patch")
                {
                    if (returnTypeText == null)                    //http response
                    {
                        if (RequestBodyCodeTypeReference == null)  //no content body
                        {
                            Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsForResponse});"));
                        }
                        else
                        {
                            Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {ContentOptionsForResponse});"));
                        }
                    }
                    else                                          // type is returned
                    {
                        if (RequestBodyCodeTypeReference == null) // no body
                        {
                            Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {Options}).then(d => d.json());"));
                        }
                        else
                        {
                            Method.Statements.Add(new CodeSnippetStatement($"return fetch({uriText}, {OptionsWithContent}).then(d => d.json());"));
                        }
                    }
                }
                else
                {
                    Debug.Assert(false, $"How come with {httpMethodName}?");
                }
            }
        }