<commit_before>import msRestAzure = require('./azure-arm-common');
import tl = require('vsts-task-lib/task');
import fs = require('fs');
import util = require('util');
import webClient = require('./webClient');
import Q = require('q');
import { ToError } from './AzureServiceClient';
import { WebJob, SiteExtension } from './azureModels';
import { KUDU_DEPLOYMENT_CONSTANTS } from './constants';

export class KuduServiceManagementClient {
    private _scmUri;
    private _accesssToken: string;

    constructor(scmUri: string, accessToken: string) {
        this._accesssToken = accessToken;
        this._scmUri = scmUri;
    }

    public async beginRequest(request: webClient.WebRequest, reqOptions?: webClient.WebRequestOptions): Promise<webClient.WebResponse> {
        request.headers = request.headers || {};
        request.headers["Authorization"] = "Basic " + this._accesssToken;
        request.headers['Content-Type'] = 'application/json; charset=utf-8';
        
        try {
            let httpResponse = await webClient.sendRequest(request, reqOptions);
            return httpResponse;
        }
        catch(exception) {
            let exceptionString: string = exception.toString();
            if(exceptionString.indexOf("Hostname/IP doesn't match certificates's altnames") != -1
                || exceptionString.indexOf("unable to verify the first certificate") != -1
                || exceptionString.indexOf("unable to get local issuer certificate") != -1) {
                    tl.warning(tl.loc('ASE_SSLIssueRecommendation'));
            }

            throw new Error(exceptionString);
        }

    }

    public getRequestUri(uriFormat: string, queryParameters?: Array<string>) {
        uriFormat = uriFormat[0] == "/" ? uriFormat : "/" + uriFormat;

        if(queryParameters && queryParameters.length > 0) {
            uriFormat = uriFormat + '?' + queryParameters.join('&');
        }

        return this._scmUri + uriFormat;
    }

    public getScmUri(): string {
        return this._scmUri;
    }
}

export class Kudu {
    private _client: KuduServiceManagementClient;

    constructor(scmUri: string, username: string, password: string) {
        var base64EncodedCredential = (new Buffer(username + ':' + password).toString('base64'));<commit_msg>Retry for Kudu Request timedout (#7684)<commit_after>import msRestAzure = require('./azure-arm-common');
import tl = require('vsts-task-lib/task');
import fs = require('fs');
import util = require('util');
import webClient = require('./webClient');
import Q = require('q');
import { ToError } from './AzureServiceClient';
import { WebJob, SiteExtension } from './azureModels';
import { KUDU_DEPLOYMENT_CONSTANTS } from './constants';

export class KuduServiceManagementClient {
    private _scmUri;
    private _accesssToken: string;

    constructor(scmUri: string, accessToken: string) {
        this._accesssToken = accessToken;
        this._scmUri = scmUri;
    }

    public async beginRequest(request: webClient.WebRequest, reqOptions?: webClient.WebRequestOptions): Promise<webClient.WebResponse> {
        request.headers = request.headers || {};
        request.headers["Authorization"] = "Basic " + this._accesssToken;
        request.headers['Content-Type'] = 'application/json; charset=utf-8';
        
        let retryCount = reqOptions && reqOptions.retryCount ? reqOptions.retryCount : 5;
        while(retryCount >= 0) {
            try {
                let httpResponse = await webClient.sendRequest(request, reqOptions);
                return httpResponse;
            }
            catch(exception) {
                let exceptionString: string = exception.toString();
                if(exceptionString.indexOf("Hostname/IP doesn't match certificates's altnames") != -1
                    || exceptionString.indexOf("unable to verify the first certificate") != -1
                    || exceptionString.indexOf("unable to get local issuer certificate") != -1) {
                        tl.warning(tl.loc('ASE_SSLIssueRecommendation'));
                }

                if(retryCount > 0 && exceptionString.indexOf('Request timeout') != -1) {
                    tl.debug('encountered request timedou issue in Kudu. Retrying again');
                    retryCount -= 1;
                    continue;
                }

                throw new Error(exceptionString);
            }
        }

    }

    public getRequestUri(uriFormat: string, queryParameters?: Array<string>) {
        uriFormat = uriFormat[0] == "/" ? uriFormat : "/" + uriFormat;

        if(queryParameters && queryParameters.length > 0) {
            uriFormat = uriFormat + '?' + queryParameters.join('&');
        }

        return this._scmUri + uriFormat;
    }

    public getScmUri(): string {
        return this._scmUri;
    }
}

export class Kudu {
    private _client: KuduServiceManagementClient;

    constructor(scmUri: string, username: string, password: string) {
        var base64EncodedCredential = (new Buffer(username + ':' + password).toString('base64'));
{'fraction_non_alphanumeric': 0.12039408866995074, 'fraction_numerical': 0.005714285714285714, 'mean_word_length': 2.7132406730065837, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4438367', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1341, 'n_words': 306}