66
77#include " api.h"
88
9- #include " http.h"
109#include " package_path.h"
11- #include " property_tree .h"
10+ #include " remote .h"
1211#include " settings.h"
1312
13+ #include < grpcpp/grpcpp.h>
14+
1415#include < primitives/log.h>
1516DECLARE_STATIC_LOGGER (logger, " api" );
1617
1718namespace sw
1819{
1920
20- ptree api_call (const Remote &r, const String &api, ptree request )
21+ void check_relative (const Remote &r, PackagePath &p )
2122{
22- if (r.user .empty ())
23- throw std::runtime_error (" Remote user is empty" );
24- if (r.token .empty ())
25- throw std::runtime_error (" Remote token is empty" );
26-
27- request.put (" auth.user" , r.user );
28- request.put (" auth.token" , r.token );
29-
30- HttpRequest req = httpSettings;
31- req.type = HttpRequest::Post;
32- req.url = r.url + " /api/" + api;
33- req.data = ptree2string (request);
34- auto resp = url_request (req);
35- auto ret = string2ptree (resp.response );
36- if (resp.http_code != 200 )
37- {
38- auto e = ret.get <String>(" error" , " " );
39- throw std::runtime_error (e);
40- }
23+ if (p.isRelative (r.user ))
24+ p = " pvt." + r.user + " ." + p.toString ();
25+ }
4126
42- return string2ptree (resp.response );
27+ void apply_auth (const Remote &r, grpc::ClientContext &context)
28+ {
29+ context.AddMetadata (" auth.user" , r.user );
30+ context.AddMetadata (" auth.token" , r.token );
4331}
4432
45- void check_relative ( const Remote &r, PackagePath &p )
33+ void check_status (grpc::Status status )
4634{
47- if (p. isRelative (r. user ))
48- p = " pvt. " + r. user + " . " + p. toString ( );
35+ if (!status. ok ( ))
36+ LOG_ERROR (logger, " RPC failed: " << status. error_code () << " : " << status. error_message () );
4937}
5038
51- void Api::add_project ( const Remote &r, PackagePath p )
39+ void check_status_and_throw (grpc::Status status )
5240{
53- check_relative (r, p);
54- ptree request;
55- request.put (" project" , p.toString ());
56- api_call (r, " add_project" , request);
41+ if (!status.ok ())
42+ throw std::runtime_error (" RPC failed: " + std::to_string (status.error_code ()) + " : " + status.error_message ());
5743}
5844
59- void Api::remove_project (const Remote &r, PackagePath p)
45+ Api::Api (const Remote &r)
46+ : r(r)
47+ , api_(api::ApiService::NewStub(r.getGrpcChannel()))
48+ , user_(api::UserService::NewStub(r.getGrpcChannel()))
6049{
61- check_relative (r, p);
62- ptree request;
63- request.put (" project" , p.toString ());
64- api_call (r, " remove_project" , request);
6550}
6651
67- void Api::add_version (const Remote &r, PackagePath p, const String &cppan )
52+ void Api::addDownloads (const std::set< int64_t > &pkgs )
6853{
69- check_relative (r, p);
70- ptree request;
71- request.put (" project" , p.toString ());
72- request.put (" cppan" , cppan);
73- api_call (r, " add_version" , request);
54+ api::PackageIds request;
55+ for (auto &id : pkgs)
56+ request.mutable_ids ()->Add (id);
57+ grpc::ClientContext context;
58+ check_status (api_->AddDownloads (&context, request, nullptr ));
59+ }
60+
61+ void Api::addClientCall ()
62+ {
63+ google::protobuf::Empty request;
64+ grpc::ClientContext context;
65+ check_status (api_->AddClientCall (&context, request, nullptr ));
66+ }
67+
68+ IdDependencies Api::resolvePackages (const UnresolvedPackages &pkgs)
69+ {
70+ api::UnresolvedPackages request;
71+ for (auto &pkg : pkgs)
72+ {
73+ auto pb_pkg = request.mutable_packages ()->Add ();
74+ pb_pkg->set_path (pkg.ppath );
75+ pb_pkg->set_range (pkg.range .toString ());
76+ }
77+ api::ResolvedPackages resolved;
78+ grpc::ClientContext context;
79+ check_status_and_throw (api_->ResolvePackages (&context, request, &resolved));
80+
81+ IdDependencies id_deps;
82+ for (auto &pkg : resolved.packages ())
83+ {
84+ DownloadDependency d;
85+ d.ppath = pkg.package ().path ();
86+ d.version = pkg.package ().version ();
87+ d.flags = pkg.flags ();
88+ d.hash = pkg.hash ();
89+ d.group_number = pkg.group_number ();
90+
91+ std::unordered_set<db::PackageVersionId> idx;
92+ for (auto &tree_dep : pkg.dependencies ())
93+ idx.insert (tree_dep);
94+ d.setDependencyIds (idx);
95+ }
96+ return id_deps;
7497}
7598
76- void Api::add_version (const Remote &r, PackagePath p, const Version &vnew )
99+ void Api::addVersion (const String &cppan )
77100{
78- add_version (r, p, vnew, String ());
101+ api::NewPackage request;
102+ request.set_script (cppan);
103+
104+ grpc::ClientContext context;
105+ apply_auth (r, context);
106+
107+ check_status (user_->AddPackage (&context, request, nullptr ));
79108}
80109
81- void Api::add_version ( const Remote &r, PackagePath p, const Version &vnew, const String &vold)
110+ void Api::addVersion ( PackagePath p, const Version &vnew, const optional<Version> &vold)
82111{
83112 check_relative (r, p);
84- ptree request;
85- request.put (" project" , p.toString ());
86- request.put (" new" , vnew.toString ());
87- if (!vold.empty ())
88- request.put (" old" , vold);
89- api_call (r, " add_version" , request);
113+
114+ api::NewPackage request;
115+ request.mutable_version ()->mutable_package ()->set_path (p.toString ());
116+ request.mutable_version ()->mutable_package ()->set_version (vnew.toString ());
117+ if (vold)
118+ request.mutable_version ()->set_old_version (vold.value ().toString ());
119+
120+ grpc::ClientContext context;
121+ apply_auth (r, context);
122+
123+ check_status (user_->AddPackage (&context, request, nullptr ));
90124}
91125
92- void Api::update_version ( const Remote &r, PackagePath p, const Version &v)
126+ void Api::updateVersion ( PackagePath p, const Version &v)
93127{
94128 if (!v.isBranch ())
95129 throw std::runtime_error (" Only branches can be updated" );
130+
96131 check_relative (r, p);
97- ptree request;
98- request.put (" project" , p.toString ());
99- request.put (" version" , v.toString ());
100- api_call (r, " update_version" , request);
132+
133+ api::PackageId request;
134+ request.set_path (p.toString ());
135+ request.set_version (v.toString ());
136+
137+ grpc::ClientContext context;
138+ apply_auth (r, context);
139+
140+ check_status (user_->UpdatePackage (&context, request, nullptr ));
101141}
102142
103- void Api::remove_version ( const Remote &r, PackagePath p, const Version &v)
143+ void Api::removeVersion ( PackagePath p, const Version &v)
104144{
105145 check_relative (r, p);
106- ptree request;
107- request.put (" project" , p.toString ());
108- request.put (" version" , v.toString ());
109- api_call (r, " remove_version" , request);
146+
147+ api::PackageId request;
148+ request.set_path (p.toString ());
149+ request.set_version (v.toString ());
150+
151+ grpc::ClientContext context;
152+ apply_auth (r, context);
153+
154+ check_status (user_->UpdatePackage (&context, request, nullptr ));
110155}
111156
112- void Api::get_notifications ( const Remote &r, int n)
157+ void Api::getNotifications ( int n)
113158{
114159 if (n < 0 )
115160 return ;
116161
117- ptree request;
118- request.put (" n" , n);
119- auto response = api_call (r, " get_notifications" , request);
120- auto notifications = response.get_child (" notifications" );
162+ api::NotificationsRequest request;
163+ request.set_n (n);
164+
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));
171+
121172 int i = 1 ;
122- for (auto &n : notifications)
173+ for (auto &n : notifications. notifications () )
123174 {
124- auto nt = (NotificationType)n.second .get <int >(" type" , 0 );
125- auto t = n.second .get <String>(" text" , " " );
126- auto ts = n.second .get <String>(" timestamp" , " " );
127-
175+ auto nt = (NotificationType)n.type ();
128176 std::ostringstream ss;
129177 ss << i++ << " " ;
130178 switch (nt)
@@ -142,14 +190,15 @@ void Api::get_notifications(const Remote &r, int n)
142190 ss << " OK" ;
143191 break ;
144192 }
145- LOG_INFO (logger, ss.str () << " " << ts << " " << t );
193+ LOG_INFO (logger, ss.str () << " " << n. timestamp () << " " << n. text () );
146194 }
147195}
148196
149- void Api::clear_notifications ( const Remote &r )
197+ void Api::clearNotifications ( )
150198{
151- ptree request;
152- api_call (r, " clear_notifications" , request);
199+ google::protobuf::Empty request;
200+ grpc::ClientContext context;
201+ check_status (user_->ClearNotification (&context, request, nullptr ));
153202}
154203
155204}
0 commit comments