Skip to content

Commit eb2c958

Browse files
committed
add http client DELETE method support
1 parent ec968f8 commit eb2c958

5 files changed

Lines changed: 77 additions & 5 deletions

File tree

app/include/api/http.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ class HTTP {
5252
static std::string encode_form(const Form& form);
5353
void _get(const std::string& url, std::ostream* out);
5454
bool getinfo(char** arg);
55-
int propfind(const std::string& url, std::ostream* out, bool self = false);
55+
int propfind(const std::string& url, std::ostream* out);
5656
std::string _post(const std::string& url, const std::string& data);
5757
void set_user_agent(const std::string& agent);
5858
void set_basic_auth(const std::string& user, const std::string& passwd);
59+
void _delete(const std::string& url, std::ostream* out);
5960

6061
template <typename... Ts>
6162
static void set_option(HTTP& s, Ts&&... ts) {

app/include/api/jellyfin.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,26 @@ inline void postJSON(const nlohmann::json& data, Then then, OnError error, std::
5050
});
5151
}
5252

53+
template <typename Result, typename... Args>
54+
inline void deleteJSON(const std::function<void(Result)>& then, OnError error, std::string_view fmt, Args&&... args) {
55+
std::string url = fmt::format(fmt::runtime(fmt), std::forward<Args>(args)...);
56+
brls::async([then, error, url]() {
57+
auto& c = AppConfig::instance();
58+
HTTP::Header header = {c.getAuth(c.getToken())};
59+
60+
try {
61+
HTTP s;
62+
std::ostringstream body;
63+
HTTP::set_option(s, header, HTTP::Timeout{});
64+
s._delete(c.getUrl() + url, &body);
65+
auto j = nlohmann::json::parse(body.str()).get<Result>();
66+
brls::sync(std::bind(std::move(then), std::move(j)));
67+
} catch (const std::exception& ex) {
68+
if (error) brls::sync(std::bind(error, std::string(ex.what())));
69+
}
70+
});
71+
}
72+
5373
}; // namespace jellyfin
5474

5575
#include "jellyfin/system.hpp"

app/include/view/context_menu.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class ContextMenu : public brls::Box {
2222

2323
bool doPlayed();
2424
bool doFavorite();
25+
bool unPlayed();
26+
bool unFavorite();
2527

2628
std::string itemId;
2729
};

app/src/api/http.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void HTTP::_get(const std::string& url, std::ostream* out) {
237237

238238
bool HTTP::getinfo(char** arg) { return curl_easy_getinfo(this->easy, CURLINFO_CONTENT_TYPE, arg) == CURLE_OK; }
239239

240-
int HTTP::propfind(const std::string& url, std::ostream* out, bool self) {
240+
int HTTP::propfind(const std::string& url, std::ostream* out) {
241241
curl_easy_setopt(this->easy, CURLOPT_URL, url.c_str());
242242
curl_easy_setopt(this->easy, CURLOPT_CUSTOMREQUEST, "PROPFIND");
243243
return this->perform(out);
@@ -251,4 +251,11 @@ std::string HTTP::_post(const std::string& url, const std::string& data) {
251251
int code = this->perform(&body);
252252
if (code >= 400) throw curl_error(fmt::format("http status {}", code));
253253
return body.str();
254-
}
254+
}
255+
256+
void HTTP::_delete(const std::string& url, std::ostream* out) {
257+
curl_easy_setopt(this->easy, CURLOPT_URL, url.c_str());
258+
curl_easy_setopt(this->easy, CURLOPT_CUSTOMREQUEST, "DELETE");
259+
int code = this->perform(out);
260+
if (code >= 400) throw curl_error(fmt::format("http status {}", code));
261+
}

app/src/view/context_menu.cpp

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ ContextMenu::ContextMenu(const jellyfin::Item& item) : itemId(item.Id) {
1717
});
1818
this->cancel->addGestureRecognizer(new brls::TapGestureRecognizer(this->cancel));
1919

20-
this->btnFavorite->registerClickAction([this](brls::View* view) { return this->doFavorite(); });
20+
this->btnFavorite->registerClickAction([this](brls::View* view) {
21+
if (this->btnFavorite->getSelected())
22+
return this->unFavorite();
23+
else
24+
return this->doFavorite();
25+
});
2126
this->btnFavorite->addGestureRecognizer(new brls::TapGestureRecognizer(this->btnFavorite));
2227
this->btnFavorite->setSelected(item.UserData.IsFavorite);
2328

24-
this->btnMarkPlay->registerClickAction([this](brls::View* view) { return this->doPlayed(); });
29+
this->btnMarkPlay->registerClickAction([this](brls::View* view) {
30+
if (this->btnMarkPlay->getSelected())
31+
return this->unPlayed();
32+
else
33+
return this->doPlayed();
34+
});
2535
this->btnMarkPlay->addGestureRecognizer(new brls::TapGestureRecognizer(this->btnMarkPlay));
2636
this->btnMarkPlay->setSelected(item.UserData.Played);
2737
}
@@ -63,5 +73,37 @@ bool ContextMenu::doFavorite() {
6373
},
6474
jellyfin::apiFavoriteItems, AppConfig::instance().getUserId(), this->itemId);
6575

76+
return true;
77+
}
78+
79+
bool ContextMenu::unPlayed() {
80+
ASYNC_RETAIN
81+
jellyfin::deleteJSON<jellyfin::UserDataResult>(
82+
[ASYNC_TOKEN](const jellyfin::UserDataResult& r) {
83+
ASYNC_RELEASE
84+
this->btnMarkPlay->setSelected(r.IsFavorite);
85+
},
86+
[ASYNC_TOKEN](const std::string& ex) {
87+
ASYNC_RELEASE
88+
brls::Application::popActivity(brls::TransitionAnimation::NONE, [ex]() { brls::Application::notify(ex); });
89+
},
90+
jellyfin::apiPlayedItems, AppConfig::instance().getUserId(), this->itemId);
91+
92+
return true;
93+
}
94+
95+
bool ContextMenu::unFavorite() {
96+
ASYNC_RETAIN
97+
jellyfin::deleteJSON<jellyfin::UserDataResult>(
98+
[ASYNC_TOKEN](const jellyfin::UserDataResult& r) {
99+
ASYNC_RELEASE
100+
this->btnFavorite->setSelected(r.IsFavorite);
101+
},
102+
[ASYNC_TOKEN](const std::string& ex) {
103+
ASYNC_RELEASE
104+
brls::Application::popActivity(brls::TransitionAnimation::NONE, [ex]() { brls::Application::notify(ex); });
105+
},
106+
jellyfin::apiFavoriteItems, AppConfig::instance().getUserId(), this->itemId);
107+
66108
return true;
67109
}

0 commit comments

Comments
 (0)