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:
@@ -2,6 +2,9 @@
|
||||
|
||||
#include<iostream>
|
||||
|
||||
#include"Models/API.h"
|
||||
#include"Models/Song.h"
|
||||
#include"Parsers/APIParser.h"
|
||||
#include"Syncers/Upload.h"
|
||||
|
||||
#include"TokenManager.h"
|
||||
@@ -14,6 +17,9 @@ using std::string;
|
||||
|
||||
using Managers::TokenManager;
|
||||
using Managers::UserManager;
|
||||
using Models::API;
|
||||
using Models::Song;
|
||||
using Parsers::APIParser;
|
||||
using Models::IcarusAction;
|
||||
using Syncers::Upload;
|
||||
|
||||
@@ -38,6 +44,7 @@ namespace Managers
|
||||
case deleteAct:
|
||||
break;
|
||||
case downloadAct:
|
||||
downloadSong();
|
||||
break;
|
||||
case retrieveAct:
|
||||
break;
|
||||
@@ -55,16 +62,23 @@ namespace Managers
|
||||
{"upload", uploadAct}
|
||||
};
|
||||
}
|
||||
void CommitManager::downloadSong()
|
||||
{
|
||||
// TODO: Implement this functionality
|
||||
}
|
||||
void CommitManager::uploadSong()
|
||||
{
|
||||
cout<<"Uploading song..."<<endl;
|
||||
UserManager usrMgr{icaAction};
|
||||
auto user = usrMgr.retrieveUser();
|
||||
|
||||
TokenManager tk{user};
|
||||
APIParser apiPrs{icaAction};
|
||||
auto api = apiPrs.retrieveAPI();
|
||||
|
||||
TokenManager tk{user, api};
|
||||
auto token = tk.requestToken();
|
||||
|
||||
string songPath{};
|
||||
Song song{};
|
||||
|
||||
for (auto arg : icaAction.flags)
|
||||
{
|
||||
@@ -73,21 +87,12 @@ namespace Managers
|
||||
|
||||
if (flag.compare("-s") == 0)
|
||||
{
|
||||
songPath.assign(arg.value);
|
||||
song.songPath.assign(arg.value);
|
||||
}
|
||||
}
|
||||
|
||||
Upload upld{};
|
||||
upld.uploadSong(token, songPath);
|
||||
|
||||
// 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
|
||||
Upload upld{api};
|
||||
upld.uploadSong(token, song);
|
||||
}
|
||||
#pragma Functions
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace Managers
|
||||
void commitAction();
|
||||
private:
|
||||
void initializeMapActions();
|
||||
void downloadSong();
|
||||
void uploadSong();
|
||||
|
||||
enum ActionValues
|
||||
|
||||
@@ -11,6 +11,7 @@ using std::endl;
|
||||
using json = nlohmann::json;
|
||||
|
||||
using Managers::TokenManager;
|
||||
using Models::API;
|
||||
using Models::Token;
|
||||
using Models::User;
|
||||
|
||||
@@ -21,6 +22,13 @@ namespace Managers
|
||||
{
|
||||
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
|
||||
|
||||
|
||||
@@ -33,7 +41,9 @@ namespace Managers
|
||||
usrObj["username"] = user.username;
|
||||
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::Header{{"Content-Type", "application/json"}});
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef TOKENMANAGER_H_
|
||||
#define TOKENMANAGER_H_
|
||||
|
||||
#include"Models/API.h"
|
||||
#include"Models/Token.h"
|
||||
#include"Models/User.h"
|
||||
|
||||
@@ -10,9 +11,11 @@ namespace Managers
|
||||
{
|
||||
public:
|
||||
TokenManager(const Models::User);
|
||||
TokenManager(const Models::User, Models::API);
|
||||
|
||||
Models::Token requestToken();
|
||||
private:
|
||||
Models::API api;
|
||||
Models::User user;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ namespace Managers
|
||||
UserManager::UserManager(User user)
|
||||
{
|
||||
this->user = user;
|
||||
this->user = User{};
|
||||
}
|
||||
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::string;
|
||||
|
||||
using Models::API;
|
||||
|
||||
namespace Syncers
|
||||
{
|
||||
Download::Download() { }
|
||||
Download::Download(API api)
|
||||
{
|
||||
this->api = api;
|
||||
}
|
||||
Download::Download(string filePath)
|
||||
{
|
||||
downloadFilePath = filePath;
|
||||
|
||||
@@ -4,12 +4,18 @@
|
||||
#include<iostream>
|
||||
#include<string>
|
||||
|
||||
#include"Models/API.h"
|
||||
#include"Models/Song.h"
|
||||
|
||||
#include"SyncerBase.h"
|
||||
|
||||
namespace Syncers
|
||||
{
|
||||
class Download
|
||||
class Download : SyncerBase
|
||||
{
|
||||
public:
|
||||
Download();
|
||||
Download(Models::API);
|
||||
Download(std::string);
|
||||
|
||||
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 Managers::FileManager;
|
||||
using Models::API;
|
||||
using Models::Song;
|
||||
using Models::UploadForm;
|
||||
|
||||
using namespace cpr;
|
||||
@@ -27,6 +29,11 @@ namespace Syncers
|
||||
this->songPath = filePath;
|
||||
this->fMgr = FileManager(songPath);
|
||||
}
|
||||
Upload::Upload(API api)
|
||||
{
|
||||
this->api = api;
|
||||
this->api.endpoint = "song/data";
|
||||
}
|
||||
Upload::Upload(UploadForm formData)
|
||||
{
|
||||
this->url = formData.url;
|
||||
@@ -52,15 +59,18 @@ namespace Syncers
|
||||
cout<<"Finished"<<endl;
|
||||
|
||||
}
|
||||
void Upload::uploadSong(const Models::Token token, const std::string songPath)
|
||||
void Upload::uploadSong(const Models::Token token, Song song)
|
||||
{
|
||||
try
|
||||
{
|
||||
auto url = retrieveUrl();
|
||||
|
||||
cout<<"url "<<url<<endl;
|
||||
string auth{token.tokenType};
|
||||
auth.append(" " + token.accessToken);
|
||||
auto r = cpr::Post(cpr::Url{""},
|
||||
auto r = cpr::Post(cpr::Url{url},
|
||||
cpr::Multipart{{"key", "small value"},
|
||||
{"file", cpr::File{songPath}}},
|
||||
{"file", cpr::File{song.songPath}}},
|
||||
cpr::Header{{"authorization", auth}}
|
||||
);
|
||||
|
||||
@@ -74,6 +84,14 @@ namespace Syncers
|
||||
cout<<"Finished"<<endl;
|
||||
}
|
||||
|
||||
string Upload::retrieveUrl()
|
||||
{
|
||||
string url = api.url + "api/" + api.version + "/" +
|
||||
api.endpoint;
|
||||
|
||||
return url;
|
||||
}
|
||||
#pragma
|
||||
void Upload::configureSongDemo()
|
||||
{
|
||||
int id = 0;
|
||||
@@ -135,4 +153,6 @@ namespace Syncers
|
||||
|
||||
return jObj;
|
||||
}
|
||||
#pragma Testing
|
||||
#pragma Functions
|
||||
}
|
||||
|
||||
+12
-7
@@ -6,6 +6,7 @@
|
||||
#include<nlohmann/json.hpp>
|
||||
|
||||
#include"Managers/FileManager.h"
|
||||
#include"Models/API.h"
|
||||
#include"Models/Song.h"
|
||||
#include"Models/Token.h"
|
||||
#include"Models/UploadForm.h"
|
||||
@@ -18,24 +19,28 @@ namespace Syncers
|
||||
public:
|
||||
Upload();
|
||||
Upload(std::string);
|
||||
Upload(Models::API);
|
||||
Upload(Models::UploadForm);
|
||||
|
||||
void uploadSong();
|
||||
void uploadSong(const Models::Token, const std::string);
|
||||
void uploadSong(const Models::Token, Models::Song);
|
||||
private:
|
||||
Managers::FileManager fMgr;
|
||||
Models::API api;
|
||||
Models::Song song;
|
||||
std::string apiUrl{"http://192.168.1.3"};
|
||||
std::string apiEndPoint{"/api/song/data"};
|
||||
std::string songPath;
|
||||
std::string url;
|
||||
int port{9349};
|
||||
std::string apiUrl{""}; // Not being used
|
||||
std::string apiEndPoint{""}; // Not being used
|
||||
std::string songPath; // Not being used
|
||||
std::string url; // Not being used
|
||||
int port{9349}; // Not being used
|
||||
|
||||
std::string retrieveUrl();
|
||||
|
||||
void configureSongDemo();
|
||||
void printSongDetails();
|
||||
void printJsonData(nlohmann::json);
|
||||
|
||||
nlohmann::json serializeObject();
|
||||
nlohmann::json serializeObject(); // Not being used
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user