1010#include " remote.h"
1111#include " settings.h"
1212
13+ #include < grpc_helpers.h>
14+
1315#include < grpcpp/grpcpp.h>
1416
1517#include < primitives/log.h>
@@ -30,16 +32,18 @@ void apply_auth(const Remote &r, grpc::ClientContext &context)
3032 context.AddMetadata (" auth.token" , r.token );
3133}
3234
33- void check_status (grpc::Status status )
35+ auto getContext ( )
3436{
35- if (!status.ok ())
36- LOG_ERROR (logger, " RPC failed: " << status.error_code () << " : " << status.error_message ());
37+ auto ctx = std::make_unique<grpc::ClientContext>();
38+ ctx->AddMetadata (" client.version" , " 0.3.0" );
39+ return ctx;
3740}
3841
39- void check_status_and_throw (grpc::Status status )
42+ auto getContextWithAuth ( const Remote &r )
4043{
41- if (!status.ok ())
42- throw std::runtime_error (" RPC failed: " + std::to_string (status.error_code ()) + " : " + status.error_message ());
44+ auto ctx = getContext ();
45+ apply_auth (r, *ctx);
46+ return ctx;
4347}
4448
4549Api::Api (const Remote &r)
@@ -54,15 +58,15 @@ void Api::addDownloads(const std::set<int64_t> &pkgs)
5458 api::PackageIds request;
5559 for (auto &id : pkgs)
5660 request.mutable_ids ()->Add (id);
57- grpc::ClientContext context;
58- check_status (api_-> AddDownloads (&context, request, nullptr ) );
61+ auto context = getContext () ;
62+ GRPC_CALL (api_, AddDownloads, google::protobuf::Empty );
5963}
6064
6165void Api::addClientCall ()
6266{
6367 google::protobuf::Empty request;
64- grpc::ClientContext context;
65- check_status (api_-> AddClientCall (&context, request, nullptr ) );
68+ auto context = getContext () ;
69+ GRPC_CALL (api_, AddClientCall, google::protobuf::Empty );
6670}
6771
6872IdDependencies Api::resolvePackages (const UnresolvedPackages &pkgs)
@@ -74,12 +78,11 @@ IdDependencies Api::resolvePackages(const UnresolvedPackages &pkgs)
7478 pb_pkg->set_path (pkg.ppath );
7579 pb_pkg->set_range (pkg.range .toString ());
7680 }
77- api::ResolvedPackages resolved;
78- grpc::ClientContext context;
79- check_status_and_throw (api_->ResolvePackages (&context, request, &resolved));
81+ auto context = getContext ();
82+ GRPC_CALL_THROWS (api_, ResolvePackages, api::ResolvedPackages);
8083
8184 IdDependencies id_deps;
82- for (auto &pkg : resolved .packages ())
85+ for (auto &pkg : response .packages ())
8386 {
8487 DownloadDependency d;
8588 d.ppath = pkg.package ().path ();
@@ -90,7 +93,7 @@ IdDependencies Api::resolvePackages(const UnresolvedPackages &pkgs)
9093
9194 std::unordered_set<db::PackageVersionId> idx;
9295 for (auto &tree_dep : pkg.dependencies ())
93- idx.insert (tree_dep);
96+ idx.insert (tree_dep. id () );
9497 d.setDependencyIds (idx);
9598 }
9699 return id_deps;
@@ -100,11 +103,8 @@ void Api::addVersion(const String &cppan)
100103{
101104 api::NewPackage request;
102105 request.set_script (cppan);
103-
104- grpc::ClientContext context;
105- apply_auth (r, context);
106-
107- check_status (user_->AddPackage (&context, request, nullptr ));
106+ auto context = getContextWithAuth (r);
107+ GRPC_CALL (user_, AddPackage, google::protobuf::Empty);
108108}
109109
110110void Api::addVersion (PackagePath p, const Version &vnew, const optional<Version> &vold)
@@ -117,10 +117,8 @@ void Api::addVersion(PackagePath p, const Version &vnew, const optional<Version>
117117 if (vold)
118118 request.mutable_version ()->set_old_version (vold.value ().toString ());
119119
120- grpc::ClientContext context;
121- apply_auth (r, context);
122-
123- check_status (user_->AddPackage (&context, request, nullptr ));
120+ auto context = getContextWithAuth (r);
121+ GRPC_CALL (user_, AddPackage, google::protobuf::Empty);
124122}
125123
126124void Api::updateVersion (PackagePath p, const Version &v)
@@ -134,10 +132,8 @@ void Api::updateVersion(PackagePath p, const Version &v)
134132 request.set_path (p.toString ());
135133 request.set_version (v.toString ());
136134
137- grpc::ClientContext context;
138- apply_auth (r, context);
139-
140- check_status (user_->UpdatePackage (&context, request, nullptr ));
135+ auto context = getContextWithAuth (r);
136+ GRPC_CALL (user_, UpdatePackage, google::protobuf::Empty);
141137}
142138
143139void Api::removeVersion (PackagePath p, const Version &v)
@@ -148,10 +144,8 @@ void Api::removeVersion(PackagePath p, const Version &v)
148144 request.set_path (p.toString ());
149145 request.set_version (v.toString ());
150146
151- grpc::ClientContext context;
152- apply_auth (r, context);
153-
154- check_status (user_->UpdatePackage (&context, request, nullptr ));
147+ auto context = getContextWithAuth (r);
148+ GRPC_CALL (user_, RemovePackage, google::protobuf::Empty);
155149}
156150
157151void Api::getNotifications (int n)
@@ -162,15 +156,12 @@ void Api::getNotifications(int n)
162156 api::NotificationsRequest request;
163157 request.set_n (n);
164158
165- api::Notifications notifications;
166-
167- grpc::ClientContext context;
168- apply_auth (r, context);
169-
170- check_status_and_throw (user_->GetNotifications (&context, request, ¬ifications));
159+ auto context = getContextWithAuth (r);
160+ GRPC_CALL_THROWS (user_, GetNotifications, api::Notifications);
171161
162+ // move out; return as result
172163 int i = 1 ;
173- for (auto &n : notifications .notifications ())
164+ for (auto &n : response .notifications ())
174165 {
175166 auto nt = (NotificationType)n.type ();
176167 std::ostringstream ss;
@@ -197,8 +188,8 @@ void Api::getNotifications(int n)
197188void Api::clearNotifications ()
198189{
199190 google::protobuf::Empty request;
200- grpc::ClientContext context;
201- check_status (user_-> ClearNotification (&context, request, nullptr ) );
191+ auto context = getContextWithAuth (r) ;
192+ GRPC_CALL (user_, ClearNotification, google::protobuf::Empty );
202193}
203194
204195}
0 commit comments