initial commit
This commit is contained in:
@@ -1,6 +1,25 @@
|
||||
#include "cexportinventree.h"
|
||||
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonArray>
|
||||
|
||||
#include <QByteArray>
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QUrl>
|
||||
#include <QUrlQuery>
|
||||
#include <QNetworkRequest>
|
||||
#include <QNetworkReply>
|
||||
|
||||
#include <QEventLoop>
|
||||
|
||||
#include <QSettings>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
QString cExportInventree::pluginName()
|
||||
{
|
||||
return(PLUGIN_NAME);
|
||||
@@ -10,3 +29,107 @@ qint16 cExportInventree::pluginVersion()
|
||||
{
|
||||
return(PLUGIN_VERSION);
|
||||
}
|
||||
|
||||
void cExportInventree::test()
|
||||
{
|
||||
// QString userName;
|
||||
// QString firstName;
|
||||
// QString lastName;
|
||||
// QString email;
|
||||
|
||||
// getUser(userName, firstName, lastName, email);
|
||||
getPart();
|
||||
}
|
||||
|
||||
bool cExportInventree::getUser(QString& userName, QString& firstName, QString& lastName, QString& email)
|
||||
{
|
||||
QString reply = request("/user/me/");
|
||||
|
||||
QJsonDocument jsonResponse = QJsonDocument::fromJson(reply.toUtf8());
|
||||
QJsonObject jsonObj = jsonResponse.object();
|
||||
|
||||
userName = jsonObj["username"].toString();
|
||||
firstName = jsonObj["first_name"].toString();
|
||||
lastName = jsonObj["last_name"].toString();
|
||||
email = jsonObj["email"].toString();
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
bool cExportInventree::getPart()
|
||||
{
|
||||
QJsonObject obj;
|
||||
obj["limit"] = 100;
|
||||
|
||||
QJsonDocument doc(obj);
|
||||
QHash<QString, QString> data;
|
||||
|
||||
// data.insert("limit", "100");
|
||||
data.insert("search", "0k");
|
||||
|
||||
|
||||
QString reply = request("/part/.*", data);
|
||||
|
||||
QJsonDocument jsonResponse = QJsonDocument::fromJson(reply.toUtf8());
|
||||
QJsonArray jsonArray = jsonResponse.array();
|
||||
|
||||
for(int z = 0;z < jsonArray.count();z++)
|
||||
{
|
||||
QJsonObject jsonObj = jsonArray[z].toObject();
|
||||
QString name = jsonObj["name"].toString();
|
||||
qDebug() << name;
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
QString cExportInventree::request(const QString& request, const QHash<QString, QString>& data)
|
||||
{
|
||||
QSettings settings;
|
||||
|
||||
QString requestUrl = QString(settings.value("plugins/exportInventree/url", "").toString()) + request;
|
||||
QNetworkAccessManager* networkAccessManager = new QNetworkAccessManager(this);
|
||||
QUrl url(requestUrl);
|
||||
|
||||
if(!data.isEmpty())
|
||||
{
|
||||
QUrlQuery query;
|
||||
|
||||
for(auto i = data.begin(), end = data.end(); i != end; ++i)
|
||||
{
|
||||
query.addQueryItem(i.key(), i.value());
|
||||
}
|
||||
|
||||
url.setQuery(query);
|
||||
}
|
||||
|
||||
QNetworkRequest networkRequest(url);
|
||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
networkRequest.setRawHeader("Authorization", QString(QString("Token ") + QString(settings.value("plugins/exportInventree/token", "").toString())).toLocal8Bit());
|
||||
|
||||
QNetworkReply* reply = networkAccessManager->get(networkRequest);
|
||||
|
||||
QEventLoop loop;
|
||||
|
||||
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
|
||||
loop.exec();
|
||||
|
||||
QString strReply;
|
||||
|
||||
if(reply->error() == QNetworkReply::NoError)
|
||||
{
|
||||
strReply = (QString)reply->readAll();
|
||||
|
||||
delete reply;
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << reply->errorString();
|
||||
|
||||
delete reply;
|
||||
}
|
||||
|
||||
delete networkAccessManager;
|
||||
|
||||
return(strReply);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QtPlugin>
|
||||
#include <QByteArray>
|
||||
|
||||
#include "cexportinterface.h"
|
||||
|
||||
#define PLUGIN_NAME "Export InvenTree"
|
||||
@@ -19,6 +21,14 @@ class cExportInventree : public QObject, cExportInterface
|
||||
public:
|
||||
QString pluginName() override;
|
||||
qint16 pluginVersion() override;
|
||||
|
||||
void test() override;
|
||||
|
||||
bool getUser(QString& userName, QString& firstName, QString& lastName, QString& email);
|
||||
bool getPart();
|
||||
|
||||
private:
|
||||
QString request(const QString& request, const QHash<QString, QString>& data = QHash<QString, QString>());
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -17,6 +17,10 @@ qint16 cImportDigikey::pluginVersion()
|
||||
return(PLUGIN_VERSION);
|
||||
}
|
||||
|
||||
void cImportDigikey::test()
|
||||
{
|
||||
}
|
||||
|
||||
bool cImportDigikey::showSearch(QWidget* parent)
|
||||
{
|
||||
cSearchDialog* searchDialog = new cSearchDialog(parent);
|
||||
|
||||
@@ -20,6 +20,8 @@ public:
|
||||
QString pluginName() override;
|
||||
qint16 pluginVersion() override;
|
||||
|
||||
void test() override;
|
||||
|
||||
bool showSearch(QWidget* parent) override;
|
||||
};
|
||||
|
||||
|
||||
@@ -12,6 +12,10 @@ qint16 cImportMouser::pluginVersion()
|
||||
return(PLUGIN_VERSION);
|
||||
}
|
||||
|
||||
void cImportMouser::test()
|
||||
{
|
||||
}
|
||||
|
||||
bool cImportMouser::showSearch(QWidget* parent)
|
||||
{
|
||||
cSearchDialog* searchDialog = new cSearchDialog(parent);
|
||||
|
||||
@@ -20,6 +20,8 @@ public:
|
||||
QString pluginName() override;
|
||||
qint16 pluginVersion() override;
|
||||
|
||||
void test() override;
|
||||
|
||||
bool showSearch(QWidget* parent) override;
|
||||
};
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ public:
|
||||
virtual QString pluginName() = 0;
|
||||
virtual qint16 pluginVersion() = 0;
|
||||
virtual plugin_type pluginType() = 0;
|
||||
virtual void test() = 0;
|
||||
};
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
|
||||
@@ -90,6 +90,7 @@ void cMainWindow::on_m_test_clicked()
|
||||
{
|
||||
if(!m_exportInterfaceList.at(i)->pluginName().compare("Export InvenTree", Qt::CaseSensitive))
|
||||
{
|
||||
m_exportInterfaceList.at(i)->test();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user