<commit_before>/*
 * AvaTax Software Development Kit for Apex
 *
 * (c) 2004-2018 Avalara, Inc.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @@copyright  2004-2018 Avalara, Inc.
 * @@license    https://www.apache.org/licenses/LICENSE-2.0
 * @@version    @SwaggerModel.ApiVersion
 * @@link       https://github.com/avadev/AvaTax-REST-V2-Apex-SDK
 */

global class AvaTaxClient
{
        // Returns the version number of the API used to generate this class
        global static string API_VERSION { get { return '@SwaggerModel.ApiVersion' } }
    	private string credentials;
    	private string clientHeader;
    	private String envUri;

@foreach(var m in SwaggerModel.Methods) {<text>
        // @ApexComment(m.Summary, 8)
        // @ApexComment(m.Description, 8)
        // </text>
    foreach (var p in m.Params) {
        if (p.CleanParamName == "X-Avalara-Client") continue;
        WriteLine("        // <param name=\'" + p.CleanParamName + "\'>" + ApexComment(p.Comment, 8) + "</param>");
    }

    Write("        global " + m.parseBracket(m.ResponseTypeName) + " " + m.Name + "(");
    
    string fetchCleanName = m.parseBracket(m.ResponseTypeName);
    bool any = false;
    foreach (var p in m.Params) {
        if (p.CleanParamName == "X-Avalara-Client") continue;
	string typeName = p.ApexTypeName.Contains("<") ? fetchCleanName : p.ApexTypeName;

        Write(typeName + " " + p.CleanParamName + ", ");
        any = true;
    }
    if (any) {
        Backtrack(2);
    }

    WriteLine(")");
    WriteLine("        {");
    WriteLine("            AvaTaxPath path = new AvaTaxPath(\'" + m.URI + "\');");

    foreach (var p in m.Params) {
        if (p.ParameterLocation == ParameterLocationType.UriPath) {
            WriteLine("            path.ApplyField(\'{0}\', {1});", p.ParamName, p.CleanParamName);
        } else if (p.ParameterLocation == ParameterLocationType.QueryString) {
            WriteLine("            path.AddQuery(\'{0}\', {1});", p.ParamName, p.CleanParamName);
        }
    }
    
    if (m.ResponseType == "array"){
        WriteLine("            httpResponse res = restCallForList(\'" + m.HttpVerb.ToUpper() + "\', path.relativePath, " + (m.BodyParam == null ? "null" : "model") + ");");
        WriteLine("            " + fetchCleanName + " retVal = new " + fetchCleanName + "();");
        WriteLine("            if(res.getStatusCode() == 200 || res.getStatusCode() == 201)");
        WriteLine("            {");
        WriteLine("                retVal.result = (" + fetchCleanName + ")System.JSON.deserialize(res.getBody(), " + fetchCleanName + ".class);");
        WriteLine("            }");
        WriteLine("            else");
        WriteLine("            {");
        WriteLine("                retVal.error = ((ErrorResult)System.JSON.deserialize(res.getBody(), ErrorResult.class)).error;");
        WriteLine("            }");
        WriteLine("            return retVal;");
    } else if(m.ResponseTypeName == "FileResult"){
        WriteLine("            FileResultModel responseModel = null;");
        WriteLine("            responseModel = (FileResultModel) restCallForFile(\'" + m.HttpVerb.ToUpper() + "\', path.relativePath, " + (m.BodyParam == null ? "null" : "model") + ");");
        WriteLine("            return responseModel;");
    } else {
        WriteLine("            " + fetchCleanName + " responseModel = null;");
        WriteLine("            responseModel = restCall(\'" + m.HttpVerb.ToUpper() + "\', path.relativePath, " + (m.BodyParam == null ? "null" : "model") + ", " + fetchCleanName +".class.getName());");
        WriteLine("            if(responseModel.getStatusCode() != 200 && responseModel.getStatusCode() != 201)");
        WriteLine("            {");
        WriteLine("                responseModel.error = (ErrorResult)System.JSON.deserialize(JSON.serialize(responseModel), ErrorResult.class)).error;");
        WriteLine("            }");
        WriteLine("            return responseModel;");
    }
    WriteLine("        }");
    WriteLine("");
}
    global AvaTaxClient(string appName, string appVersion, string machineName, AvaTaxEnvironment environment)
    {
        WithClientIdentifier(appName, appVersion, machineName);

        if(environment==AvaTaxEnvironment.Sandbox) 
        {
            envUri = Constants.AVATAX_SANDBOX_URL;
        } else if(environment==AvaTaxEnvironment.Production)
        {
            envUri = Constants.AVATAX_PRODUCTION_URL;
        }         
    }
        
    global AvaTaxClient WithClientIdentifier(string appName, string appVersion, string machineName)
    {
        String templateString = '{0}; {1}; {2}; {3}; {4}';
        String[] arguments = new String[]{appName, appVersion, 'ApexRestClient', API_VERSION, machineName};
        clientHeader = String.Format(templateString,arguments);
        return this;
    }
    
    global AvaTaxClient(string appName, string appVersion, string machineName, String customEnvironment)
    {
        WithClientIdentifier(appName, appVersion, machineName);
        envUri = customEnvironment;
    }
    
    global AvaTaxClient WithSecurity(string headerString)
    {
        credentials = headerString;
        return this;
    }

    global AvaTaxClient WithSecurity(string username, string password)
    {
        String templateString = '{0}:{1}';
        String[] arguments = new String[]{username, password};
        String combined = String.Format(templateString,arguments);
        
        
        Blob Integers = Blob.valueOf(combined);
        String base64 = EncodingUtil.base64Encode(Integers);
        return WithSecurity('Basic ' + base64);
    }
    
    global AvaTaxClient WithSecurity(integer accountId, string licenseKey)
    {
        String templateString = '{0}:{1}';
        String[] arguments = new String[]{String.valueOf(accountId), licenseKey};
        String combined = String.Format(templateString,arguments);
        Blob Integers = Blob.valueOf(combined);
        String base64 = EncodingUtil.base64Encode(Integers);
        return WithSecurity('Basic ' + base64);
    }
    
    global AvaTaxClient WithBearerToken(string bearerToken)
    {
        WithSecurity('Bearer' + bearerToken);
        return this;
    }
    global Object restCall(String method, String relativePath, Object model,String type)
    {
        Http http = new Http();
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setEndpoint(envUri+relativePath);
        HttpRequest.setMethod(method);
        httpRequest.setHeader('Authorization', credentials);
        httpRequest.setHeader('X-Avalara-Client', clientHeader);
        httpRequest.setHeader('Accept', 'application/json');
        httpRequest.setHeader('Content-Type', 'application/json');
                
        string finalRequest = JSON.serialize(model);
        finalRequest = finalRequest.replace('documentDate', 'date');
        finalRequest =  finalRequest.replace('commitDocument', 'commit');
        finalRequest =  finalRequest.replace('commitTransaction', 'commit');
        finalRequest =  finalRequest.replace('lineNumber', 'number');
            
        
        if (model != null) 
        {
            httpRequest.setBody(finalRequest);
        }
            
        HttpResponse httpResponse = http.send(httpRequest);
        System.Type sType = System.Type.forName(type); 
        String finalHttpResponse = httpResponse.getBody();
        finalHttpResponse = finalHttpResponse.replace('AvaTax.Communications.TransactionType', 'TransactionType');
        finalHttpResponse = finalHttpResponse.replace('AvaTax.Communications.ServiceType', 'ServiceType');
        finalHttpResponse = finalHttpResponse.replace('@@recordsetCount', 'count');
        finalHttpResponse = finalHttpResponse.replace('@@nextLink', 'nextLink');
        finalHttpResponse = finalHttpResponse.replace('@@transactionDate', 'date');
           
        Object obj;
        if (finalHttpResponse != '')
        {
            obj = System.JSON.deserialize(finalHttpResponse, sType);
            ((ErrorResult)obj).statusCode=httpResponse.getStatusCode();
        }
            
        return obj;
    }
    
    global HttpResponse restCallForList(String method, String relativePath, Object model)
    {

        Http http = new Http();
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setEndpoint(envUri+relativePath);
        HttpRequest.setMethod(method);
        httpRequest.setHeader('Authorization', credentials);
        httpRequest.setHeader('X-Avalara-Client', clientHeader);
        httpRequest.setHeader('Accept', 'application/json');
        httpRequest.setHeader('Content-Type', 'application/json');
        if (model != null) 
        {
            httpRequest.setBody(JSON.serialize(model));
        }
            
        HttpResponse httpResponse = http.send(httpRequest);
        httpResponse.getBody().replace('AvaTax.Communications.TransactionType', 'TransactionType');
        httpResponse.getBody().replace('AvaTax.Communications.ServiceType', 'ServiceType');
            
        return httpResponse;
    }

    global FileResultModel restCallForFile(String method, String relativePath, Object model)
    {
        Http http = new Http();
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setEndpoint(envUri+relativePath);
        HttpRequest.setMethod(method);
        httpRequest.setHeader('Authorization', credentials);
        httpRequest.setHeader('X-Avalara-Client', clientHeader);
        httpRequest.setHeader('Accept', 'application/json');
        httpRequest.setHeader('Content-Type', 'application/json');
        
        string finalRequest = JSON.serialize(model);
        finalRequest = finalRequest.replace('documentDate', 'date');
        finalRequest =  finalRequest.replace('commitDocument', 'commit');
        finalRequest =  finalRequest.replace('commitTransaction', 'commit');
        finalRequest =  finalRequest.replace('lineNumber', 'number');
        
        if (model != null) {
            httpRequest.setBody(JSON.serialize(finalRequest));
        }
        
        HttpResponse httpResponse = http.send(httpRequest);
        
        FileResultModel frmodel = new FileResultModel();
        
        if(httpResponse.getStatusCode() == 200 || httpResponse.getStatusCode() == 201) {
            frmodel.Filename = GetDispositionFilename(httpResponse.getHeader('Content-Disposition'));
            frmodel.Data = httpResponse.getBodyasBlob();
            frmodel.ContentType = httpResponse.getHeader('Content-Type');
        }
        
        frmodel.statusCode = httpResponse.getStatusCode();
        return frmodel;
    }    
    
    private string GetDispositionFilename(string contentDisposition)
    {
        string filename = 'filename=';
        Integer index = contentDisposition.lastIndexOfIgnoreCase(filename);
        if (index > -1) {
            return contentDisposition.right(index+contentDisposition.length());
        }
        return contentDisposition;
    }

}<commit_msg>updated list type method error handling<commit_after>/*
 * AvaTax Software Development Kit for Apex
 *
 * (c) 2004-2018 Avalara, Inc.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @@copyright  2004-2018 Avalara, Inc.
 * @@license    https://www.apache.org/licenses/LICENSE-2.0
 * @@version    @SwaggerModel.ApiVersion
 * @@link       https://github.com/avadev/AvaTax-REST-V2-Apex-SDK
 */

global class AvaTaxClient
{
        // Returns the version number of the API used to generate this class
        global static string API_VERSION { get { return '@SwaggerModel.ApiVersion' } }
    	private string credentials;
    	private string clientHeader;
    	private String envUri;

@foreach(var m in SwaggerModel.Methods) {<text>
        // @ApexComment(m.Summary, 8)
        // @ApexComment(m.Description, 8)
        // </text>
    foreach (var p in m.Params) {
        if (p.CleanParamName == "X-Avalara-Client") continue;
        WriteLine("        // <param name=\'" + p.CleanParamName + "\'>" + ApexComment(p.Comment, 8) + "</param>");
    }

    Write("        global " + m.parseBracket(m.ResponseTypeName) + " " + m.Name + "(");
    
    string fetchCleanName = m.parseBracket(m.ResponseTypeName);
    bool any = false;
    foreach (var p in m.Params) {
        if (p.CleanParamName == "X-Avalara-Client") continue;
	string typeName = p.ApexTypeName.Contains("<") ? fetchCleanName : p.ApexTypeName;

        Write(typeName + " " + p.CleanParamName + ", ");
        any = true;
    }
    if (any) {
        Backtrack(2);
    }

    WriteLine(")");
    WriteLine("        {");
    WriteLine("            AvaTaxPath path = new AvaTaxPath(\'" + m.URI + "\');");

    foreach (var p in m.Params) {
        if (p.ParameterLocation == ParameterLocationType.UriPath) {
            WriteLine("            path.ApplyField(\'{0}\', {1});", p.ParamName, p.CleanParamName);
        } else if (p.ParameterLocation == ParameterLocationType.QueryString) {
            WriteLine("            path.AddQuery(\'{0}\', {1});", p.ParamName, p.CleanParamName);
        }
    }
    
    if (m.ResponseType == "array"){
        WriteLine("            httpResponse res = restCallForList(\'" + m.HttpVerb.ToUpper() + "\', path.relativePath, " + (m.BodyParam == null ? "null" : "model") + ");");
        WriteLine("            " + fetchCleanName + " retVal = new " + fetchCleanName + "();");
        WriteLine("            if(res.getStatusCode() == 200 || res.getStatusCode() == 201)");
        WriteLine("            {");
        WriteLine("                retVal.result = (" + fetchCleanName + ")System.JSON.deserialize(res.getBody(), " + m.ResponseTypeName+ ".class);");
        WriteLine("            }");
        WriteLine("            else");
        WriteLine("            {");
        WriteLine("                retVal.error = ((ErrorResult)System.JSON.deserialize(res.getBody(), ErrorResult.class)).error;");
        WriteLine("            }");
        WriteLine("            return retVal;");
    } else if(m.ResponseTypeName == "FileResult"){
        WriteLine("            FileResultModel responseModel = null;");
        WriteLine("            responseModel = (FileResultModel) restCallForFile(\'" + m.HttpVerb.ToUpper() + "\', path.relativePath, " + (m.BodyParam == null ? "null" : "model") + ");");
        WriteLine("            return responseModel;");
    } else {
        WriteLine("            " + fetchCleanName + " responseModel = null;");
        WriteLine("            responseModel = restCall(\'" + m.HttpVerb.ToUpper() + "\', path.relativePath, " + (m.BodyParam == null ? "null" : "model") + ", " + fetchCleanName +".class.getName());");
        WriteLine("            if(responseModel.getStatusCode() != 200 && responseModel.getStatusCode() != 201)");
        WriteLine("            {");
        WriteLine("                responseModel.error = (ErrorResult)System.JSON.deserialize(JSON.serialize(responseModel), ErrorResult.class)).error;");
        WriteLine("            }");
        WriteLine("            return responseModel;");
    }
    WriteLine("        }");
    WriteLine("");
}
    global AvaTaxClient(string appName, string appVersion, string machineName, AvaTaxEnvironment environment)
    {
        WithClientIdentifier(appName, appVersion, machineName);

        if(environment==AvaTaxEnvironment.Sandbox) 
        {
            envUri = Constants.AVATAX_SANDBOX_URL;
        } else if(environment==AvaTaxEnvironment.Production)
        {
            envUri = Constants.AVATAX_PRODUCTION_URL;
        }         
    }
        
    global AvaTaxClient WithClientIdentifier(string appName, string appVersion, string machineName)
    {
        String templateString = '{0}; {1}; {2}; {3}; {4}';
        String[] arguments = new String[]{appName, appVersion, 'ApexRestClient', API_VERSION, machineName};
        clientHeader = String.Format(templateString,arguments);
        return this;
    }
    
    global AvaTaxClient(string appName, string appVersion, string machineName, String customEnvironment)
    {
        WithClientIdentifier(appName, appVersion, machineName);
        envUri = customEnvironment;
    }
    
    global AvaTaxClient WithSecurity(string headerString)
    {
        credentials = headerString;
        return this;
    }

    global AvaTaxClient WithSecurity(string username, string password)
    {
        String templateString = '{0}:{1}';
        String[] arguments = new String[]{username, password};
        String combined = String.Format(templateString,arguments);
        
        
        Blob Integers = Blob.valueOf(combined);
        String base64 = EncodingUtil.base64Encode(Integers);
        return WithSecurity('Basic ' + base64);
    }
    
    global AvaTaxClient WithSecurity(integer accountId, string licenseKey)
    {
        String templateString = '{0}:{1}';
        String[] arguments = new String[]{String.valueOf(accountId), licenseKey};
        String combined = String.Format(templateString,arguments);
        Blob Integers = Blob.valueOf(combined);
        String base64 = EncodingUtil.base64Encode(Integers);
        return WithSecurity('Basic ' + base64);
    }
    
    global AvaTaxClient WithBearerToken(string bearerToken)
    {
        WithSecurity('Bearer' + bearerToken);
        return this;
    }
    global Object restCall(String method, String relativePath, Object model,String type)
    {
        Http http = new Http();
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setEndpoint(envUri+relativePath);
        HttpRequest.setMethod(method);
        httpRequest.setHeader('Authorization', credentials);
        httpRequest.setHeader('X-Avalara-Client', clientHeader);
        httpRequest.setHeader('Accept', 'application/json');
        httpRequest.setHeader('Content-Type', 'application/json');
                
        string finalRequest = JSON.serialize(model);
        finalRequest = finalRequest.replace('documentDate', 'date');
        finalRequest =  finalRequest.replace('commitDocument', 'commit');
        finalRequest =  finalRequest.replace('commitTransaction', 'commit');
        finalRequest =  finalRequest.replace('lineNumber', 'number');
            
        
        if (model != null) 
        {
            httpRequest.setBody(finalRequest);
        }
            
        HttpResponse httpResponse = http.send(httpRequest);
        System.Type sType = System.Type.forName(type); 
        String finalHttpResponse = httpResponse.getBody();
        finalHttpResponse = finalHttpResponse.replace('AvaTax.Communications.TransactionType', 'TransactionType');
        finalHttpResponse = finalHttpResponse.replace('AvaTax.Communications.ServiceType', 'ServiceType');
        finalHttpResponse = finalHttpResponse.replace('@@recordsetCount', 'count');
        finalHttpResponse = finalHttpResponse.replace('@@nextLink', 'nextLink');
        finalHttpResponse = finalHttpResponse.replace('@@transactionDate', 'date');
           
        Object obj;
        if (finalHttpResponse != '')
        {
            obj = System.JSON.deserialize(finalHttpResponse, sType);
            ((ErrorResult)obj).statusCode=httpResponse.getStatusCode();
        }
            
        return obj;
    }
    
    global HttpResponse restCallForList(String method, String relativePath, Object model)
    {

        Http http = new Http();
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setEndpoint(envUri+relativePath);
        HttpRequest.setMethod(method);
        httpRequest.setHeader('Authorization', credentials);
        httpRequest.setHeader('X-Avalara-Client', clientHeader);
        httpRequest.setHeader('Accept', 'application/json');
        httpRequest.setHeader('Content-Type', 'application/json');
        if (model != null) 
        {
            httpRequest.setBody(JSON.serialize(model));
        }
            
        HttpResponse httpResponse = http.send(httpRequest);
        httpResponse.getBody().replace('AvaTax.Communications.TransactionType', 'TransactionType');
        httpResponse.getBody().replace('AvaTax.Communications.ServiceType', 'ServiceType');
            
        return httpResponse;
    }

    global FileResultModel restCallForFile(String method, String relativePath, Object model)
    {
        Http http = new Http();
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setEndpoint(envUri+relativePath);
        HttpRequest.setMethod(method);
        httpRequest.setHeader('Authorization', credentials);
        httpRequest.setHeader('X-Avalara-Client', clientHeader);
        httpRequest.setHeader('Accept', 'application/json');
        httpRequest.setHeader('Content-Type', 'application/json');
        
        string finalRequest = JSON.serialize(model);
        finalRequest = finalRequest.replace('documentDate', 'date');
        finalRequest =  finalRequest.replace('commitDocument', 'commit');
        finalRequest =  finalRequest.replace('commitTransaction', 'commit');
        finalRequest =  finalRequest.replace('lineNumber', 'number');
        
        if (model != null) {
            httpRequest.setBody(JSON.serialize(finalRequest));
        }
        
        HttpResponse httpResponse = http.send(httpRequest);
        
        FileResultModel frmodel = new FileResultModel();
        
        if(httpResponse.getStatusCode() == 200 || httpResponse.getStatusCode() == 201) {
            frmodel.Filename = GetDispositionFilename(httpResponse.getHeader('Content-Disposition'));
            frmodel.Data = httpResponse.getBodyasBlob();
            frmodel.ContentType = httpResponse.getHeader('Content-Type');
        }
        
        frmodel.statusCode = httpResponse.getStatusCode();
        return frmodel;
    }    
    
    private string GetDispositionFilename(string contentDisposition)
    {
        string filename = 'filename=';
        Integer index = contentDisposition.lastIndexOfIgnoreCase(filename);
        if (index > -1) {
            return contentDisposition.right(index+contentDisposition.length());
        }
        return contentDisposition;
    }

}
{'fraction_non_alphanumeric': 0.126248376550674, 'fraction_numerical': 0.006001164405033813, 'mean_word_length': 2.603938024531956, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1349600', 'n_tokens_mistral': 6054, 'n_tokens_neox': 5864, 'n_words': 1169}