1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
|
/* File: gbtoolsTrackhub.cpp
* Author: Gemma Barson (gb10@sanger.ac.uk)
* Copyright (c) 2016: Genome Research Ltd.
*-------------------------------------------------------------------
* ZMap is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* or see the on-line version at http://www.gnu.org/copyleft/gpl.txt
*-------------------------------------------------------------------
* This file is part of the ZMap genome database package
* originally written by:
*
* Ed Griffiths (Sanger Institute, UK) edgrif@sanger.ac.uk,
* Roy Storey (Sanger Institute, UK) rds@sanger.ac.uk
*
* Description: Object to access the Ensembl Track Hub Registry
*
*-------------------------------------------------------------------
*/
#include <iostream>
#include <cstring>
#include <sstream>
#include <curl/curl.h>
#include <json/json.h>
#include <gbtools/gbtoolsTrackhub.hpp>
#include <gbtools/gbtoolsCurl.hpp>
#include <gbtools/gbtoolsUtils.hpp>
using namespace std;
using namespace gbtools;
namespace // unnamed namespace
{
using namespace gbtools::trackhub;
#define TRACKHUB_REGISTRY_HOST "https://www.trackhubregistry.org"
#define API_SEARCH_ENTRIES_PER_PAGE 100
#define API_INFO_PING "/api/info/ping"
#define API_INFO_VERSION "/api/info/version"
#define API_INFO_SPECIES "/api/info/species"
#define API_INFO_ASSEMBLIES "/api/info/assemblies"
#define API_INFO_TRACKHUBS "/api/info/trackhubs"
#define API_SEARCH "/api/search"
#define API_SEARCH_TRACKDB "/api/search/trackdb"
#define API_LOGIN "/api/login"
#define API_LOGOUT "/api/logout"
#define API_TRACKHUB "/api/trackhub"
#define API_TRACKDB "/api/trackdb"
// This enum lists the expected return codes from the trackhub registry api
enum class ResponseCode: long
{ UNSET = 0,
OK = 200,
CREATED = 201,
BAD_REQUEST = 400,
INVALID_CREDENTIALS = 401,
NOT_FOUND = 404,
GONE = 410,
SERVER_ERR = 500,
UNAVAILABLE = 503
} ;
class CurlReadData
{
public:
CurlReadData(): readptr(NULL), sizeleft(0.0)
{
};
CurlReadData(const char *data)
{
readptr = data;
sizeleft = strlen(data);
};
const char *readptr;
long sizeleft;
};
int curlWriteDataCB(char *data, size_t size, size_t nmemb, string *buffer)
{
int size_written = size * nmemb;
if (buffer && data)
{
buffer->append(data, size * nmemb);
}
return size_written;
}
size_t curlReadDataCB(char *data, size_t size, size_t nmemb, CurlReadData *read_data)
{
int size_written = 0;
if (size * nmemb > 0 && read_data && read_data->sizeleft > 0 && data)
{
*data = read_data->readptr[0]; /* copy one single byte */
read_data->readptr++; /* advance pointer */
read_data->sizeleft--; /* we return 1 byte at a time! */
size_written = 1;
}
return size_written;
}
// Create a Track class for the track in the given json and add it to the given list.
// Recurse through any child tracks and add them to the new Track class's children.
// Also add its file type (if it has one) to the list of types.
// Pass in the visibility of the parent track, which will be used if the visibility of this track
// is not set (or "none" if no parent has it set).
void getTracks(Json::ValueIterator &iter,
list<Track> &track_list,
list<string> &file_types,
string visibility)
{
Json::Value js_track = *iter;
if (js_track["visibility"].isString())
visibility = js_track["visibility"].asString() ;
Track track(js_track["track"].asString(),
js_track["shortLabel"].asString(),
js_track["bigDataUrl"].asString(),
js_track["type"].asString(),
visibility
);
if (js_track["type"].isString())
file_types.push_back(js_track["type"].asString());
// Recurse through any child tracks
Json::Value js_children = js_track["members"];
for (Json::ValueIterator child = js_children.begin(); child != js_children.end(); ++child)
getTracks(child, track.children_, file_types, visibility);
// Add the track to the list. Must do this last because it takes a copy.
track_list.push_back(track);
}
// Process the results of a request. If the response code indicates an error, set the error
// message. Returns the contents of the buffer in json format.
Json::Value processRequestResult(const string &buffer,
const long response_code,
Json::Reader reader,
string &err_msg)
{
Json::Value js;
reader.parse(buffer, js);
stringstream err_ss;
ResponseCode code = (ResponseCode)response_code;
switch (code)
{
case ResponseCode::OK:
break;
case ResponseCode::CREATED:
break;
case ResponseCode::BAD_REQUEST:
err_ss << "Bad request: " << js["error"].asString();
break;
case ResponseCode::INVALID_CREDENTIALS:
err_ss << "Invalid credentials: " << js.asString();
break;
case ResponseCode::NOT_FOUND:
err_ss << "Not found: " << js["error"].asString();
break;
case ResponseCode::GONE:
err_ss << "Response '" << response_code << "': server gone";
break;
case ResponseCode::SERVER_ERR:
err_ss << "Response '" << response_code << "': internal server error";
break;
case ResponseCode::UNAVAILABLE:
err_ss << "Response '" << response_code << "': server unavailable";
break;
default:
err_ss << "Unrecognised response code '" << response_code << "'";
break;
} ;
err_msg = err_ss.str();
return js;
}
} // unnamed namespace
namespace gbtools
{
namespace trackhub
{
Registry::Registry()
: curl_object_get_(NULL),
curl_object_post_(NULL),
curl_object_delete_(NULL),
debug_(false),
proxy_(""),
useragent_("")
{
host_ = TRACKHUB_REGISTRY_HOST;
useragent_ = "gbtools/" ;
useragent_ += UtilsGetVersionString() ;
}
Registry::~Registry()
{
}
void Registry::initCurl()
{
if (curl_object_get_)
CURLObjectDestroy(curl_object_get_) ;
if (curl_object_post_)
CURLObjectDestroy(curl_object_post_) ;
if (curl_object_delete_)
CURLObjectDestroy(curl_object_delete_) ;
// Set up curl objects for GET/POST requests. Set the properties here that will be
// the same for all requests. */
curl_object_get_ = CURLObjectNew();
curl_object_post_ = CURLObjectNew();
curl_object_delete_ = CURLObjectNew();
CURLObjectSet(curl_object_get_,
"post", FALSE,
"writefunction", curlWriteDataCB,
NULL);
CURLObjectSet(curl_object_post_,
"post", TRUE,
"writefunction", curlWriteDataCB,
"readfunction", curlReadDataCB,
NULL);
CURLObjectSet(curl_object_delete_,
"post", FALSE,
"writefunction", curlWriteDataCB,
"customrequest", "DELETE",
NULL);
CURLObjectSet(curl_object_get_, "debug", debug_, NULL);
CURLObjectSet(curl_object_post_, "debug", debug_, NULL);
CURLObjectSet(curl_object_delete_, "debug", debug_, NULL);
if (!proxy_.empty())
{
CURLObjectSet(curl_object_get_, "proxy", proxy_.c_str(), NULL);
CURLObjectSet(curl_object_post_, "proxy", proxy_.c_str(), NULL);
CURLObjectSet(curl_object_delete_, "proxy", proxy_.c_str(), NULL);
}
if (!cainfo_.empty())
{
CURLObjectSet(curl_object_get_, "cainfo", cainfo_.c_str(), NULL);
CURLObjectSet(curl_object_post_, "cainfo", cainfo_.c_str(), NULL);
CURLObjectSet(curl_object_delete_, "cainfo", cainfo_.c_str(), NULL);
}
if (!useragent_.empty())
{
CURLObjectSet(curl_object_get_, "useragent", useragent_.c_str(), NULL);
CURLObjectSet(curl_object_post_, "useragent", useragent_.c_str(), NULL);
CURLObjectSet(curl_object_delete_, "useragent", useragent_.c_str(), NULL);
}
}
void Registry::setDebug(const bool debug)
{
debug_ = debug ;
}
void Registry::setProxy(const string &proxy)
{
proxy_ = proxy ;
}
void Registry::setCainfo(const string &cainfo)
{
cainfo_ = cainfo ;
}
void Registry::setUserAgent(const string &useragent)
{
useragent_ = useragent ;
}
// Return the headers for GET requests
curl_slist* Registry::getHeaders(const bool authorise)
{
curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Accept: application/json");
headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "charsets: utf-8");
if (authorise)
{
if (user_.empty() || auth_token_.empty())
{
cout << "Not logged in" << endl;
}
else
{
// Set user and auth token in the headers
string user_header = "User: " + user_;
string auth_header = "Auth-Token: " + auth_token_;
headers = curl_slist_append(headers, user_header.c_str());
headers = curl_slist_append(headers, auth_header.c_str());
}
}
return headers;
}
// Return the headers for POST requests
curl_slist* Registry::postHeaders(const bool authorise)
{
curl_slist *headers = getHeaders(authorise);
#ifdef USE_CHUNKED
headers = curl_slist_append(headers, "Transfer-Encoding: chunked:");
#endif
#ifdef DISABLE_EXPECT
headers = curl_slist_append(headers, "Expect:");
#endif
return headers;
}
// Does the work to send the given GET request using CURL
string Registry::doGetRequest(const string &url,
const bool authorise,
long *response_code)
{
string result("");
curl_slist *headers = getHeaders(authorise);
initCurl() ;
CURLObjectSet(curl_object_get_,
"url", url.c_str(),
"httpheader", headers,
"writedata", &result,
NULL);
CURLObjectStatus res = CURLObjectPerform(curl_object_get_, FALSE);
if (response_code)
g_object_get(curl_object_get_, "response-code", response_code, NULL) ;
if (res == CURL_STATUS_FAILED)
cout << "CURL perform failed" << endl;
// Clean up
if (headers)
curl_slist_free_all(headers);
return result;
}
// Does the work to send the given DELETE request using CURL
string Registry::doDeleteRequest(const string &url,
const bool authorise,
long *response_code)
{
string result("");
curl_slist *headers = getHeaders(authorise);
initCurl() ;
CURLObjectSet(curl_object_delete_,
"url", url.c_str(),
"httpheader", headers,
"writedata", &result,
NULL);
CURLObjectStatus res = CURLObjectPerform(curl_object_delete_, FALSE);
if (response_code)
g_object_get(curl_object_delete_, "response-code", response_code, NULL) ;
if (res == CURL_STATUS_FAILED)
cout << "CURL perform failed" << endl;
// Clean up
if (headers)
curl_slist_free_all(headers);
return result;
}
// Does the work to send the given POST request using CURL
string Registry::doPostRequest(const string &url,
const string &postfields,
const bool authorise,
long *response_code)
{
string result("");
CurlReadData read_data(postfields.c_str());
curl_slist *headers = postHeaders(authorise);
initCurl() ;
CURLObjectSet(curl_object_post_,
"url", url.c_str(),
"httpheader", headers,
"writedata", &result,
"readdata", &read_data,
NULL);
#ifndef USE_CHUNKED
CURLObjectSet(curl_object_post_,
"postfieldsize", read_data.sizeleft,
NULL);
#endif
CURLObjectStatus res = CURLObjectPerform(curl_object_post_, FALSE);
if(response_code)
g_object_get(curl_object_post_, "response-code", response_code, NULL) ;
if (res == CURL_STATUS_FAILED)
cout << "CURL perform failed" << endl;
// Clean up
if (headers)
curl_slist_free_all(headers);
return result;
}
Json::Value Registry::getRequest(const string &request,
const bool authorise,
string &err_msg)
{
Json::Value js;
string url = host_ + request;
long response_code = 0 ;
string buffer = doGetRequest(url, authorise, &response_code);
js = processRequestResult(buffer, response_code, json_reader_, err_msg) ;
return js;
}
Json::Value Registry::postRequest(const string &request,
const string &postfields,
const bool authorise,
string &err_msg)
{
Json::Value js;
string url = host_ + request;
long response_code = 0 ;
string buffer = doPostRequest(url, postfields, authorise, &response_code);
js = processRequestResult(buffer, response_code, json_reader_, err_msg) ;
return js;
}
Json::Value Registry::deleteRequest(const string &request,
const bool authorise,
string &err_msg)
{
Json::Value js;
string url = host_ + request;
long response_code = 0 ;
string buffer = doDeleteRequest(url, authorise, &response_code);
js = processRequestResult(buffer, response_code, json_reader_, err_msg) ;
return js;
}
// Ping the Registry server. Returns true if ok.
bool Registry::ping(string &err_msg)
{
bool result = false;
Json::Value js = getRequest(API_INFO_PING, false, err_msg);
if (js["ping"].isInt())
result = js["ping"].asInt();
return result;
}
// Get the Registry version number
string Registry::version(string &err_msg)
{
string result("");
Json::Value js = getRequest(API_INFO_VERSION, false, err_msg);
if (js["release"].isString())
result = js["release"].asString();
return result;
}
// Get the list of species in the Registry
list<string> Registry::species(string &err_msg)
{
list<string> result;
Json::Value js = getRequest(API_INFO_SPECIES, false, err_msg);
if (js.isArray())
{
for (const Json::Value &iter : js)
{
if (iter.isString())
{
result.push_back(iter.asString());
}
else
{
result.clear();
break;
}
}
}
return result;
}
// Get the list of assemblies in the Registry per species name
map<string, list<string>> Registry::assemblies(string &err_msg)
{
map<string, list<string>> result;
bool ok = true;
Json::Value js = getRequest(API_INFO_ASSEMBLIES, false, err_msg);
for (Json::ValueIterator species_iter = js.begin(); species_iter != js.end(); ++species_iter)
{
Json::Value species_key = species_iter.key();
Json::Value species_val = *species_iter;
if (species_key.isString() && species_val.isArray())
{
list<string> assembly_list;
for (const Json::Value &assembly_iter : species_val)
{
if (assembly_iter.isString())
{
assembly_list.push_back(assembly_iter.asString());
}
else
{
assembly_list.clear();
ok = false;
break;
}
}
result[species_key.asString()] = assembly_list;
}
else
{
result.clear();
ok = false;
break;
}
}
return result;
}
list<Trackhub> Registry::trackhubs(string &err_msg)
{
list<Trackhub> trackhubs;
Json::Value js = getRequest(API_INFO_TRACKHUBS, false, err_msg);
// Loop through each trackhub in the results
for (Json::ValueIterator iter = js.begin(); iter != js.end(); ++iter)
{
// Extract the info about this trackhub
Json::Value item_js = *iter;
trackhubs.push_back(Trackhub(item_js["name"].asString(),
item_js["shortLabel"].asString(),
item_js["longLabel"].asString(),
item_js["url"].asString()) );
}
return trackhubs;
}
// Get the results for a given page of a search and add them to the TrackDb list. Returns true
// when we have processed the last page
bool Registry::getSearchPage(stringstream &payload_ss,
const int page_no,
list<TrackDb> &result,
string &err_msg)
{
bool done = true;
stringstream url_ss;
url_ss << API_SEARCH << "?entries_per_page=" << API_SEARCH_ENTRIES_PER_PAGE << "&page=" << page_no;
Json::Value js = postRequest(url_ss.str(), payload_ss.str(), false, err_msg);
// Check how many results there are
int num_results = 0;
if (js["total_entries"].isInt())
num_results = js["total_entries"].asInt();
if (num_results > 0)
{
// Loop through all items in the result and extract the trackDb IDs
Json::Value items_js = js["items"];
for (Json::ValueIterator iter = items_js.begin(); iter != items_js.end(); ++iter)
{
Json::Value item_js = *iter ;
if (item_js["id"].isString())
{
string track_err_msg;
TrackDb trackdb = searchTrackDb(item_js["id"].asString(), track_err_msg) ;
result.push_back(trackdb) ;
}
}
}
// Check if there are any results remaining after this page
if (num_results > page_no * API_SEARCH_ENTRIES_PER_PAGE)
done = false;
return done;
}
// Search for the given search string and optional filters
list<TrackDb> Registry::search(const string &search_str,
const string &species,
const string &assembly,
const string &hub,
string &err_msg)
{
list<TrackDb> result ;
// Create a json-formatted message
Json::Value payload_js;
payload_js["query"] = search_str;
payload_js["species"] = species;
payload_js["assembly"] = assembly;
payload_js["hub"] = hub;
stringstream payload_ss;
payload_ss << payload_js;
// There may be multiple pages so get all results and compile them into a single list
int page_no = 1;
bool done = false;
while (!done && err_msg.empty())
{
done = getSearchPage(payload_ss, page_no, result, err_msg);
++page_no;
}
return result ;
}
TrackDb Registry::searchTrackDb(const string &trackdb_id, string &err_msg)
{
TrackDb trackdb;
if (!trackdb_id.empty())
{
stringstream query_ss;
query_ss << API_SEARCH_TRACKDB << "/" << trackdb_id;
Json::Value js = getRequest(query_ss.str(), false, err_msg);
// Get the lists of track info and file types
Json::Value tracks_js = js["configuration"];
list<Track> tracks;
list<string> file_types;
for (Json::ValueIterator iter = tracks_js.begin(); iter != tracks_js.end(); ++iter)
{
getTracks(iter, tracks, file_types, "none");
}
trackdb = TrackDb(trackdb_id,
js["hub"]["name"].asString(),
js["hub"]["shortLabel"].asString(),
js["hub"]["longLabel"].asString(),
js["hub"]["url"].asString(),
js["species"]["scientific_name"].asString(),
js["assembly"]["name"].asString(),
js["type"].asString(),
js["status"]["tracks"]["total"].asInt(),
js["status"]["tracks"]["with_data"]["total"].asInt(),
file_types,
tracks
);
}
return trackdb;
}
// Log in to the Registry
bool Registry::login(const string &user, const string &pwd, string &err_msg)
{
bool ok = false;
initCurl() ;
CURLObjectSet(curl_object_get_,
"username", user.c_str(),
"password", pwd.c_str(),
NULL);
Json::Value js = getRequest(API_LOGIN, false, err_msg);
CURLObjectSet(curl_object_get_,
"username", NULL,
"password", NULL,
NULL);
if (js["auth_token"].isString())
{
ok = true;
user_ = user;
auth_token_ = js["auth_token"].asString();
}
return ok;
}
// Log out of the Registry
bool Registry::logout(string &err_msg)
{
bool result = false;
if (loggedIn())
{
// Do the request
Json::Value js = getRequest(API_LOGOUT, true, err_msg);
// Check return value (should be a message saying logged out ok)
if (js["message"].isString())
{
result = true;
user_.clear();
auth_token_.clear();
}
else
{
cout << "Error logging out" << endl;
}
}
else
{
cout << "Cannot log out: not logged in" << endl ;
}
return result;
}
// Register a track hub as the current user
// assemblies: a map of assembly name in the hub to INSDC accessions
// type: "genomics", "epigenomics", "transcriptomics" or "proteomics"
Json::Value Registry::registerHub(const string &url,
const map<string, string> &assemblies,
const string &type,
const bool is_public,
string &err_msg)
{
Json::Value js;
if (loggedIn())
{
// Create a json-formatted message
Json::Value payload_js;
payload_js["url"] = url;
for (auto iter = assemblies.begin(); iter != assemblies.end(); ++iter)
payload_js["assemblies"][iter->first] = iter->second;
if (type.length() > 0)
payload_js["type"] = type;
if (is_public)
payload_js["public"] = 1;
else
payload_js["public"] = 0;
string payload = json_writer_.write(payload_js) ;
// Do the request
js = postRequest(API_TRACKHUB, payload, true, err_msg);
}
else
{
cout << "Cannot register hub: not logged in" << endl ;
}
return js;
}
// Retrieve all trackDbs for the current users' registered hubs. Gets the trackhub with the
// given name, or all registered track hubs if no name is given.
list<TrackDb> Registry::retrieveHub(const string &trackhub, string &err_msg)
{
list<TrackDb> result;
stringstream query_ss;
query_ss << API_TRACKHUB;
if (trackhub.length() > 0)
query_ss << "/" << trackhub;
// Do the request
Json::Value js = getRequest(query_ss.str(), true, err_msg);
// Loop through all items in the returned array
for (Json::ValueIterator hub_iter = js.begin(); hub_iter != js.end(); ++hub_iter)
{
// Loop through all trackdbs in this hub
Json::Value js_trackdbs = (*hub_iter)["trackdbs"];
for (Json::ValueIterator trackdb_iter = js_trackdbs.begin(); trackdb_iter != js_trackdbs.end(); ++trackdb_iter)
{
string uri = (*trackdb_iter)["uri"].asString();
// Chop off the host etc. to get the trackdb id at the end of the uri
stringstream ss;
ss << host_ << API_TRACKDB << "/" ;
string host = ss.str();
if (uri.length() >= host.length() &&
strncmp(uri.c_str(), host.c_str(), host.length()) == 0)
{
const char *trackdb_id = uri.c_str() + host.length();
string track_err_msg;
TrackDb trackdb = searchTrackDb(trackdb_id, track_err_msg);
result.push_back(trackdb);
}
}
}
return result;
}
string Registry::deleteHub(const string &trackhub, string &err_msg)
{
string result;
string query(API_TRACKHUB);
query += trackhub;
Json::Value js = deleteRequest(query, true, err_msg);
if (js["message"].isString())
result = js["message"].asString();
return result;
}
// Retrieve a registered trackdb.
Json::Value Registry::retrieveTrackDb(const string &trackdb, string &err_msg)
{
stringstream query_ss;
query_ss << API_TRACKDB << "/" << trackdb;
// Do the request
Json::Value js = getRequest(query_ss.str(), true, err_msg);
return js;
}
Json::Value Registry::deleteTrackDb(const string &trackdb, string &err_msg)
{
stringstream query_ss;
query_ss << API_TRACKDB << "/" << trackdb;
Json::Value js = deleteRequest(query_ss.str(), true, err_msg);
return js;
}
bool Registry::loggedIn()
{
return (!user_.empty() && !auth_token_.empty());
}
} // namespace trackhub
} // namespace gbtools
|