Skip to content

Commit

Permalink
set multiple request metadata items in a single transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-p committed Nov 11, 2021
1 parent 953757c commit c1e9331
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
12 changes: 10 additions & 2 deletions psicash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,17 @@ void PsiCash::SetHTTPRequestFn(MakeHTTPRequestFn make_http_request_fn) {
make_http_request_fn_ = std::move(make_http_request_fn);
}

Error PsiCash::SetRequestMetadataItem(const string& key, const string& value) {
Error PsiCash::SetRequestMetadataItems(const std::map<std::string, std::string>& items) {
MUST_BE_INITIALIZED;
return PassError(user_data_->SetRequestMetadataItem(key, value));
UserData::Transaction transaction(*user_data_);
for (const auto& it : items) {
// Errors won't manifest until we commit
(void)user_data_->SetRequestMetadataItem(it.first, it.second);
}
if (auto err = transaction.Commit()) {
return WrapError(err, "user data write failed");
}
return nullerr;
}

Error PsiCash::SetLocale(const string& locale) {
Expand Down
2 changes: 1 addition & 1 deletion psicash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class PsiCash {

/// Set values that will be included in the request metadata. This includes
/// client_version, client_region, sponsor_id, and propagation_channel_id.
error::Error SetRequestMetadataItem(const std::string& key, const std::string& value);
error::Error SetRequestMetadataItems(const std::map<std::string, std::string>& items);

/// Set current UI locale.
error::Error SetLocale(const std::string& locale);
Expand Down
18 changes: 13 additions & 5 deletions psicash_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,19 +279,27 @@ TEST_F(TestPsiCash, SetHTTPRequestFn) {
}
}

TEST_F(TestPsiCash, SetRequestMetadataItem) {
TEST_F(TestPsiCash, SetRequestMetadataItems) {
PsiCashTester pc;
auto err = pc.Init(TestPsiCash::UserAgent(), GetTempDir().c_str(), nullptr, false);
ASSERT_FALSE(err);

auto j = pc.user_data().GetRequestMetadata();
ASSERT_EQ(j.size(), 0);

err = pc.SetRequestMetadataItem("k", "v");
err = pc.SetRequestMetadataItems({{"k", "v"}});
ASSERT_FALSE(err);

j = pc.user_data().GetRequestMetadata();
ASSERT_EQ(j["k"], "v");

err = pc.SetRequestMetadataItems({{"a", "b"}, {"x", "y"}});
ASSERT_FALSE(err);

j = pc.user_data().GetRequestMetadata();
ASSERT_EQ(j["k"], "v");
ASSERT_EQ(j["a"], "b");
ASSERT_EQ(j["x"], "y");
}

TEST_F(TestPsiCash, SetLocale) {
Expand Down Expand Up @@ -934,15 +942,15 @@ TEST_F(TestPsiCash, ModifyLandingPage) {
// With metadata
//

err = pc.SetRequestMetadataItem("k", "v");
err = pc.SetRequestMetadataItems({{"k", "v"}, {"x", "y"}});
ASSERT_FALSE(err);
url_in = {"https://asdf.sadf.gf", "", ""};
res = pc.ModifyLandingPage(url_in.ToString());
ASSERT_TRUE(res);
url_out.Parse(*res);
ASSERT_EQ(url_out.scheme_host_path_, url_in.scheme_host_path_);
ASSERT_EQ(url_out.fragment_, url_in.fragment_);
ASSERT_THAT(TokenPayloadsMatch(url_out.query_.substr(key_part.length()), R"({"metadata":{"k":"v"},"tokens":"kEarnerTokenType"})"_json), IsEmpty());
ASSERT_THAT(TokenPayloadsMatch(url_out.query_.substr(key_part.length()), R"({"metadata":{"k":"v","x":"y"},"tokens":"kEarnerTokenType"})"_json), IsEmpty());

//
// Errors
Expand Down Expand Up @@ -1077,7 +1085,7 @@ TEST_F(TestPsiCash, GetRewardedActivityData) {
ASSERT_TRUE(res);
ASSERT_EQ(*res, base64::B64Encode(utils::Stringer(R"({"metadata":{"user_agent":")", TestPsiCash::UserAgent(), R"(","v":1},"tokens":"kEarnerTokenType","v":1})")));

err = pc.SetRequestMetadataItem("k", "v");
err = pc.SetRequestMetadataItems({{"k", "v"}});
ASSERT_FALSE(err);
res = pc.GetRewardedActivityData();
ASSERT_TRUE(res);
Expand Down

0 comments on commit c1e9331

Please sign in to comment.