<commit_before>#ifndef UTILS_H
#define UTILS_H

#include <qjson/parser.h>

#include <QStringList>
#include <QHash>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QDebug>
#include <QUrl>

namespace Utils {

    static inline QHash<QString, QString> parseURLParameters(const QString& url) {
        QHash<QString, QString> dict;

        foreach (const QString kvs, url.split("&")) {
            if (!kvs.contains("=")) {
                continue;
            }

            QStringList parts = kvs.split("=", QString::KeepEmptyParts);
            if (parts.size() != 2) {
                continue;
            }

            dict.insert(QUrl::fromPercentEncoding(parts[0].toUtf8()), QUrl::fromPercentEncoding(parts[1].toUtf8()));

        }
        return dict;
    }

    static inline QString parametersDictToString(const QHash<QString, QString>& parameters) {
        QStringList parameterList;
        foreach (const QString key, parameters.keys()) {
            parameterList.append(QString("%1=%2").arg(QString::fromUtf8(QUrl::toPercentEncoding(key)))
                                                 .arg(QString::fromUtf8(QUrl::toPercentEncoding(parameters[key]))));
        }
        return parameterList.join("&");
    }

    // Does a SYNCHRONOUS HTTP request for the base url
    static inline QString baseUrlForEmail(QNetworkAccessManager *nam, const QString& email, bool *requestSuccessful) {
        QString fetchURL = QString("https://api.zulip.com/v1/deployments/endpoints?email=%1").arg(email);

        bool createdNam = false;
        if (!nam) {
            createdNam = true;
            nam = new QNetworkAccessManager();
        }

        QNetworkReply *reply = nam->get(QNetworkRequest(QUrl(fetchURL)));

        // Ugh
        QEventLoop loop;
        QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        loop.exec(QEventLoop::ExcludeUserInputEvents);

        if (createdNam) {
            nam->deleteLater();
        }

        // If we get anything outside a 404 (user deployment not known) or 200,
        // we assume that our attempt at preflight failed because we could not connect
        // to zulip.com
        if (reply->error() != QNetworkReply::NoError &&
            reply->error() != QNetworkReply::ContentNotFoundError) {<commit_msg>Treat a 503 from zilencer as a 404<commit_after>#ifndef UTILS_H
#define UTILS_H

#include <qjson/parser.h>

#include <QStringList>
#include <QHash>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QEventLoop>
#include <QDebug>
#include <QUrl>

namespace Utils {

    static inline QHash<QString, QString> parseURLParameters(const QString& url) {
        QHash<QString, QString> dict;

        foreach (const QString kvs, url.split("&")) {
            if (!kvs.contains("=")) {
                continue;
            }

            QStringList parts = kvs.split("=", QString::KeepEmptyParts);
            if (parts.size() != 2) {
                continue;
            }

            dict.insert(QUrl::fromPercentEncoding(parts[0].toUtf8()), QUrl::fromPercentEncoding(parts[1].toUtf8()));

        }
        return dict;
    }

    static inline QString parametersDictToString(const QHash<QString, QString>& parameters) {
        QStringList parameterList;
        foreach (const QString key, parameters.keys()) {
            parameterList.append(QString("%1=%2").arg(QString::fromUtf8(QUrl::toPercentEncoding(key)))
                                                 .arg(QString::fromUtf8(QUrl::toPercentEncoding(parameters[key]))));
        }
        return parameterList.join("&");
    }

    // Does a SYNCHRONOUS HTTP request for the base url
    static inline QString baseUrlForEmail(QNetworkAccessManager *nam, const QString& email, bool *requestSuccessful) {
        QString fetchURL = QString("https://api.zulip.com/v1/deployments/endpoints?email=%1").arg(email);

        bool createdNam = false;
        if (!nam) {
            createdNam = true;
            nam = new QNetworkAccessManager();
        }

        QNetworkReply *reply = nam->get(QNetworkRequest(QUrl(fetchURL)));

        // Ugh
        QEventLoop loop;
        QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
        loop.exec(QEventLoop::ExcludeUserInputEvents);

        if (createdNam) {
            nam->deleteLater();
        }

        // If we get anything outside a 404 (user deployment not known) or 200,
        // we assume that our attempt at preflight failed because we could not connect
        // to zulip.com
        if (reply->error() != QNetworkReply::NoError &&
            reply->error() != QNetworkReply::ContentNotFoundError &&
            reply->error() != QNetworkReply::UnknownContentError) { /*  UnknownContentError is a server 503 */
{'fraction_non_alphanumeric': 0.12390852390852392, 'fraction_numerical': 0.00893970893970894, 'mean_word_length': 2.5716406829992575, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 34, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '559115', 'n_tokens_mistral': 1408, 'n_tokens_neox': 1342, 'n_words': 312}