Added CLI functionality for uploading a song #7. Next thing that is up adding CLI functionality for downloading songs via id
This commit is contained in:
@@ -47,6 +47,7 @@ set(SOURCES
|
|||||||
src/Managers/FileManager.cpp
|
src/Managers/FileManager.cpp
|
||||||
src/Managers/TokenManager.cpp
|
src/Managers/TokenManager.cpp
|
||||||
src/Managers/UserManager.cpp
|
src/Managers/UserManager.cpp
|
||||||
|
src/Parsers/APIParser.cpp
|
||||||
src/Syncers/Download.cpp
|
src/Syncers/Download.cpp
|
||||||
src/Syncers/Upload.cpp
|
src/Syncers/Upload.cpp
|
||||||
src/Utilities/Conversions.cpp
|
src/Utilities/Conversions.cpp
|
||||||
@@ -57,13 +58,16 @@ set(HEADERS
|
|||||||
src/Managers/FileManager.h
|
src/Managers/FileManager.h
|
||||||
src/Managers/TokenManager.h
|
src/Managers/TokenManager.h
|
||||||
src/Managers/UserManager.h
|
src/Managers/UserManager.h
|
||||||
|
src/Models/API.h
|
||||||
src/Models/Flags.h
|
src/Models/Flags.h
|
||||||
src/Models/IcarusAction.h
|
src/Models/IcarusAction.h
|
||||||
src/Models/Song.h
|
src/Models/Song.h
|
||||||
src/Models/Token.h
|
src/Models/Token.h
|
||||||
src/Models/UploadForm.h
|
src/Models/UploadForm.h
|
||||||
src/Models/User.h
|
src/Models/User.h
|
||||||
|
src/Parsers/APIParser.h
|
||||||
src/Syncers/Download.h
|
src/Syncers/Download.h
|
||||||
|
src/Syncers/SyncerBase.h
|
||||||
src/Syncers/Upload.h
|
src/Syncers/Upload.h
|
||||||
src/Utilities/Conversions.h
|
src/Utilities/Conversions.h
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
|
|
||||||
|
#include"Models/API.h"
|
||||||
|
#include"Models/Song.h"
|
||||||
|
#include"Parsers/APIParser.h"
|
||||||
#include"Syncers/Upload.h"
|
#include"Syncers/Upload.h"
|
||||||
|
|
||||||
#include"TokenManager.h"
|
#include"TokenManager.h"
|
||||||
@@ -14,6 +17,9 @@ using std::string;
|
|||||||
|
|
||||||
using Managers::TokenManager;
|
using Managers::TokenManager;
|
||||||
using Managers::UserManager;
|
using Managers::UserManager;
|
||||||
|
using Models::API;
|
||||||
|
using Models::Song;
|
||||||
|
using Parsers::APIParser;
|
||||||
using Models::IcarusAction;
|
using Models::IcarusAction;
|
||||||
using Syncers::Upload;
|
using Syncers::Upload;
|
||||||
|
|
||||||
@@ -38,6 +44,7 @@ namespace Managers
|
|||||||
case deleteAct:
|
case deleteAct:
|
||||||
break;
|
break;
|
||||||
case downloadAct:
|
case downloadAct:
|
||||||
|
downloadSong();
|
||||||
break;
|
break;
|
||||||
case retrieveAct:
|
case retrieveAct:
|
||||||
break;
|
break;
|
||||||
@@ -55,16 +62,23 @@ namespace Managers
|
|||||||
{"upload", uploadAct}
|
{"upload", uploadAct}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
void CommitManager::downloadSong()
|
||||||
|
{
|
||||||
|
// TODO: Implement this functionality
|
||||||
|
}
|
||||||
void CommitManager::uploadSong()
|
void CommitManager::uploadSong()
|
||||||
{
|
{
|
||||||
cout<<"Uploading song..."<<endl;
|
cout<<"Uploading song..."<<endl;
|
||||||
UserManager usrMgr{icaAction};
|
UserManager usrMgr{icaAction};
|
||||||
auto user = usrMgr.retrieveUser();
|
auto user = usrMgr.retrieveUser();
|
||||||
|
|
||||||
TokenManager tk{user};
|
APIParser apiPrs{icaAction};
|
||||||
|
auto api = apiPrs.retrieveAPI();
|
||||||
|
|
||||||
|
TokenManager tk{user, api};
|
||||||
auto token = tk.requestToken();
|
auto token = tk.requestToken();
|
||||||
|
|
||||||
string songPath{};
|
Song song{};
|
||||||
|
|
||||||
for (auto arg : icaAction.flags)
|
for (auto arg : icaAction.flags)
|
||||||
{
|
{
|
||||||
@@ -73,21 +87,12 @@ namespace Managers
|
|||||||
|
|
||||||
if (flag.compare("-s") == 0)
|
if (flag.compare("-s") == 0)
|
||||||
{
|
{
|
||||||
songPath.assign(arg.value);
|
song.songPath.assign(arg.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Upload upld{};
|
Upload upld{api};
|
||||||
upld.uploadSong(token, songPath);
|
upld.uploadSong(token, song);
|
||||||
|
|
||||||
// TODO: Implement functionality for uploading
|
|
||||||
//
|
|
||||||
// Need to the following:
|
|
||||||
// 1. Parse the login credentials from the Flag model x
|
|
||||||
// 2. Retrieve a token -- implement token management x
|
|
||||||
// 3. Parse song path from the Flag model x Make a parser class
|
|
||||||
// 4. Parse the HTTP API endpoint Make parser class
|
|
||||||
// 5. Upload the song x
|
|
||||||
}
|
}
|
||||||
#pragma Functions
|
#pragma Functions
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ namespace Managers
|
|||||||
void commitAction();
|
void commitAction();
|
||||||
private:
|
private:
|
||||||
void initializeMapActions();
|
void initializeMapActions();
|
||||||
|
void downloadSong();
|
||||||
void uploadSong();
|
void uploadSong();
|
||||||
|
|
||||||
enum ActionValues
|
enum ActionValues
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ using std::endl;
|
|||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
using Managers::TokenManager;
|
using Managers::TokenManager;
|
||||||
|
using Models::API;
|
||||||
using Models::Token;
|
using Models::Token;
|
||||||
using Models::User;
|
using Models::User;
|
||||||
|
|
||||||
@@ -21,6 +22,13 @@ namespace Managers
|
|||||||
{
|
{
|
||||||
this->user = user;
|
this->user = user;
|
||||||
}
|
}
|
||||||
|
TokenManager::TokenManager(const User user, API api)
|
||||||
|
{
|
||||||
|
this->user = user;
|
||||||
|
this->api = api;
|
||||||
|
this->api.endpoint = "api/" + api.version
|
||||||
|
+ "/login";
|
||||||
|
}
|
||||||
#pragma Constructors
|
#pragma Constructors
|
||||||
|
|
||||||
|
|
||||||
@@ -33,7 +41,9 @@ namespace Managers
|
|||||||
usrObj["username"] = user.username;
|
usrObj["username"] = user.username;
|
||||||
usrObj["password"] = user.password;
|
usrObj["password"] = user.password;
|
||||||
|
|
||||||
auto r = cpr::Post(cpr::Url{""},
|
cout<<"Sending request for token"<<endl;
|
||||||
|
auto url = api.url + api.endpoint;
|
||||||
|
auto r = cpr::Post(cpr::Url{url},
|
||||||
cpr::Body{usrObj.dump()},
|
cpr::Body{usrObj.dump()},
|
||||||
cpr::Header{{"Content-Type", "application/json"}});
|
cpr::Header{{"Content-Type", "application/json"}});
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef TOKENMANAGER_H_
|
#ifndef TOKENMANAGER_H_
|
||||||
#define TOKENMANAGER_H_
|
#define TOKENMANAGER_H_
|
||||||
|
|
||||||
|
#include"Models/API.h"
|
||||||
#include"Models/Token.h"
|
#include"Models/Token.h"
|
||||||
#include"Models/User.h"
|
#include"Models/User.h"
|
||||||
|
|
||||||
@@ -10,9 +11,11 @@ namespace Managers
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TokenManager(const Models::User);
|
TokenManager(const Models::User);
|
||||||
|
TokenManager(const Models::User, Models::API);
|
||||||
|
|
||||||
Models::Token requestToken();
|
Models::Token requestToken();
|
||||||
private:
|
private:
|
||||||
|
Models::API api;
|
||||||
Models::User user;
|
Models::User user;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ namespace Managers
|
|||||||
UserManager::UserManager(User user)
|
UserManager::UserManager(User user)
|
||||||
{
|
{
|
||||||
this->user = user;
|
this->user = user;
|
||||||
this->user = User{};
|
|
||||||
}
|
}
|
||||||
UserManager::UserManager(const IcarusAction icaAct)
|
UserManager::UserManager(const IcarusAction icaAct)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef API_H_
|
||||||
|
#define API_H_
|
||||||
|
|
||||||
|
#include<string>
|
||||||
|
|
||||||
|
namespace Models
|
||||||
|
{
|
||||||
|
struct API
|
||||||
|
{
|
||||||
|
std::string url;
|
||||||
|
std::string endpoint;
|
||||||
|
std::string version;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
#include"APIParser.h"
|
||||||
|
|
||||||
|
using Models::API;
|
||||||
|
using Models::IcarusAction;
|
||||||
|
|
||||||
|
namespace Parsers
|
||||||
|
{
|
||||||
|
#pragma
|
||||||
|
APIParser::APIParser(IcarusAction icaAct)
|
||||||
|
{
|
||||||
|
this->icaAct = icaAct;
|
||||||
|
api = API{};
|
||||||
|
parseAPI();
|
||||||
|
}
|
||||||
|
#pragma endregion
|
||||||
|
|
||||||
|
|
||||||
|
#pragma
|
||||||
|
API APIParser::retrieveAPI() const
|
||||||
|
{
|
||||||
|
return api;
|
||||||
|
}
|
||||||
|
|
||||||
|
void APIParser::parseAPI()
|
||||||
|
{
|
||||||
|
auto flags = icaAct.flags;
|
||||||
|
|
||||||
|
for (auto flag : flags)
|
||||||
|
{
|
||||||
|
auto arg = flag.flag;
|
||||||
|
auto value = flag.value;
|
||||||
|
|
||||||
|
if (arg.compare("-h") == 0)
|
||||||
|
{
|
||||||
|
api.url = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: For now I will hard code
|
||||||
|
// the api version since I am only
|
||||||
|
// on version 1
|
||||||
|
api.version = "v1";
|
||||||
|
}
|
||||||
|
#pragma functions
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef APIPARSER_H_
|
||||||
|
#define APIPARSER_H_
|
||||||
|
|
||||||
|
#include"Models/API.h"
|
||||||
|
#include"Models/IcarusAction.h"
|
||||||
|
|
||||||
|
namespace Parsers
|
||||||
|
{
|
||||||
|
class APIParser
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
APIParser(Models::IcarusAction);
|
||||||
|
|
||||||
|
Models::API retrieveAPI() const;
|
||||||
|
private:
|
||||||
|
void parseAPI();
|
||||||
|
|
||||||
|
Models::API api;
|
||||||
|
Models::IcarusAction icaAct;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -10,9 +10,15 @@ using std::endl;
|
|||||||
using std::ofstream;
|
using std::ofstream;
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
|
using Models::API;
|
||||||
|
|
||||||
namespace Syncers
|
namespace Syncers
|
||||||
{
|
{
|
||||||
Download::Download() { }
|
Download::Download() { }
|
||||||
|
Download::Download(API api)
|
||||||
|
{
|
||||||
|
this->api = api;
|
||||||
|
}
|
||||||
Download::Download(string filePath)
|
Download::Download(string filePath)
|
||||||
{
|
{
|
||||||
downloadFilePath = filePath;
|
downloadFilePath = filePath;
|
||||||
|
|||||||
@@ -4,12 +4,18 @@
|
|||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include<string>
|
#include<string>
|
||||||
|
|
||||||
|
#include"Models/API.h"
|
||||||
|
#include"Models/Song.h"
|
||||||
|
|
||||||
|
#include"SyncerBase.h"
|
||||||
|
|
||||||
namespace Syncers
|
namespace Syncers
|
||||||
{
|
{
|
||||||
class Download
|
class Download : SyncerBase
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Download();
|
Download();
|
||||||
|
Download(Models::API);
|
||||||
Download(std::string);
|
Download(std::string);
|
||||||
|
|
||||||
void downloadSong(int);
|
void downloadSong(int);
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#ifndef SYNCERBASE_H_
|
||||||
|
#define SYNCERBASE_H_
|
||||||
|
|
||||||
|
#include"Models/API.h"
|
||||||
|
|
||||||
|
namespace Syncers
|
||||||
|
{
|
||||||
|
class SyncerBase
|
||||||
|
{
|
||||||
|
protected:
|
||||||
|
Models::API api;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
+23
-3
@@ -15,6 +15,8 @@ using std::string;
|
|||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
|
|
||||||
using Managers::FileManager;
|
using Managers::FileManager;
|
||||||
|
using Models::API;
|
||||||
|
using Models::Song;
|
||||||
using Models::UploadForm;
|
using Models::UploadForm;
|
||||||
|
|
||||||
using namespace cpr;
|
using namespace cpr;
|
||||||
@@ -27,6 +29,11 @@ namespace Syncers
|
|||||||
this->songPath = filePath;
|
this->songPath = filePath;
|
||||||
this->fMgr = FileManager(songPath);
|
this->fMgr = FileManager(songPath);
|
||||||
}
|
}
|
||||||
|
Upload::Upload(API api)
|
||||||
|
{
|
||||||
|
this->api = api;
|
||||||
|
this->api.endpoint = "song/data";
|
||||||
|
}
|
||||||
Upload::Upload(UploadForm formData)
|
Upload::Upload(UploadForm formData)
|
||||||
{
|
{
|
||||||
this->url = formData.url;
|
this->url = formData.url;
|
||||||
@@ -52,15 +59,18 @@ namespace Syncers
|
|||||||
cout<<"Finished"<<endl;
|
cout<<"Finished"<<endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
void Upload::uploadSong(const Models::Token token, const std::string songPath)
|
void Upload::uploadSong(const Models::Token token, Song song)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
auto url = retrieveUrl();
|
||||||
|
|
||||||
|
cout<<"url "<<url<<endl;
|
||||||
string auth{token.tokenType};
|
string auth{token.tokenType};
|
||||||
auth.append(" " + token.accessToken);
|
auth.append(" " + token.accessToken);
|
||||||
auto r = cpr::Post(cpr::Url{""},
|
auto r = cpr::Post(cpr::Url{url},
|
||||||
cpr::Multipart{{"key", "small value"},
|
cpr::Multipart{{"key", "small value"},
|
||||||
{"file", cpr::File{songPath}}},
|
{"file", cpr::File{song.songPath}}},
|
||||||
cpr::Header{{"authorization", auth}}
|
cpr::Header{{"authorization", auth}}
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -74,6 +84,14 @@ namespace Syncers
|
|||||||
cout<<"Finished"<<endl;
|
cout<<"Finished"<<endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string Upload::retrieveUrl()
|
||||||
|
{
|
||||||
|
string url = api.url + "api/" + api.version + "/" +
|
||||||
|
api.endpoint;
|
||||||
|
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
#pragma
|
||||||
void Upload::configureSongDemo()
|
void Upload::configureSongDemo()
|
||||||
{
|
{
|
||||||
int id = 0;
|
int id = 0;
|
||||||
@@ -135,4 +153,6 @@ namespace Syncers
|
|||||||
|
|
||||||
return jObj;
|
return jObj;
|
||||||
}
|
}
|
||||||
|
#pragma Testing
|
||||||
|
#pragma Functions
|
||||||
}
|
}
|
||||||
|
|||||||
+12
-7
@@ -6,6 +6,7 @@
|
|||||||
#include<nlohmann/json.hpp>
|
#include<nlohmann/json.hpp>
|
||||||
|
|
||||||
#include"Managers/FileManager.h"
|
#include"Managers/FileManager.h"
|
||||||
|
#include"Models/API.h"
|
||||||
#include"Models/Song.h"
|
#include"Models/Song.h"
|
||||||
#include"Models/Token.h"
|
#include"Models/Token.h"
|
||||||
#include"Models/UploadForm.h"
|
#include"Models/UploadForm.h"
|
||||||
@@ -18,24 +19,28 @@ namespace Syncers
|
|||||||
public:
|
public:
|
||||||
Upload();
|
Upload();
|
||||||
Upload(std::string);
|
Upload(std::string);
|
||||||
|
Upload(Models::API);
|
||||||
Upload(Models::UploadForm);
|
Upload(Models::UploadForm);
|
||||||
|
|
||||||
void uploadSong();
|
void uploadSong();
|
||||||
void uploadSong(const Models::Token, const std::string);
|
void uploadSong(const Models::Token, Models::Song);
|
||||||
private:
|
private:
|
||||||
Managers::FileManager fMgr;
|
Managers::FileManager fMgr;
|
||||||
|
Models::API api;
|
||||||
Models::Song song;
|
Models::Song song;
|
||||||
std::string apiUrl{"http://192.168.1.3"};
|
std::string apiUrl{""}; // Not being used
|
||||||
std::string apiEndPoint{"/api/song/data"};
|
std::string apiEndPoint{""}; // Not being used
|
||||||
std::string songPath;
|
std::string songPath; // Not being used
|
||||||
std::string url;
|
std::string url; // Not being used
|
||||||
int port{9349};
|
int port{9349}; // Not being used
|
||||||
|
|
||||||
|
std::string retrieveUrl();
|
||||||
|
|
||||||
void configureSongDemo();
|
void configureSongDemo();
|
||||||
void printSongDetails();
|
void printSongDetails();
|
||||||
void printJsonData(nlohmann::json);
|
void printJsonData(nlohmann::json);
|
||||||
|
|
||||||
nlohmann::json serializeObject();
|
nlohmann::json serializeObject(); // Not being used
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user