LCOV - code coverage report
Current view: top level - Servidor - SharedServerClient.cpp (source / functions) Hit Total Coverage
Test: coverage_merged.info Lines: 83 86 96.5 %
Date: 2016-07-11 17:43:33 Functions: 17 17 100.0 %

          Line data    Source code
       1             : #include "SharedServerClient.h"
       2             : 
       3             : #define DEFAULT_MILISECS_POLL 3000
       4             : 
       5             : using std::string;
       6             : 
       7         206 : SharedServerClient::SharedServerClient(string url_to_connect) {
       8         206 :         conn = new RestClient::Connection(url_to_connect);
       9         206 :         RestClient::HeaderFields headers;
      10         205 :         headers["Content-Type"] = "application/json";
      11         206 :         conn->SetHeaders(headers);
      12         206 : }
      13             : 
      14         206 : SharedServerClient::~SharedServerClient() {
      15         206 :         if (conn != NULL)
      16         206 :                 delete conn;
      17         206 : }
      18             : 
      19         159 : string SharedServerClient::formUrl(string sub_urls[], size_t sub_urls_size) {
      20         159 :     string url = "";
      21         429 :     for (size_t i = 0; i < sub_urls_size; i++) {
      22         270 :         url += "/" + sub_urls[i];
      23             :     }
      24         159 :     return url;
      25             : }
      26             : 
      27         159 : bool SharedServerClient::valid(RestClient::Response *r ){
      28         159 :         if (r->code >= 400){
      29          33 :                 LOGG(DEBUG) << "SS body not ok request:\n " << r->body << "\n";
      30          33 :                 return false;
      31             :         }
      32         126 :         if (r->code < 0){
      33           0 :                 LOGG(ERROR) << "No hay conexion al SS";
      34           0 :                 return false;
      35             :         }
      36         126 :         LOGG(DEBUG) << "SS ok: " << r->code;
      37         126 :         return true;
      38             : }
      39             : 
      40         100 : bool SharedServerClient::setBody(string * body, RestClient::Response *r){
      41         100 :         *body = r->body;
      42         100 :         LOGG(DEBUG) << "SS response " << r->code;
      43         100 :         if (this->valid(r)){
      44          94 :                 return true;
      45             :         }
      46           6 :         return false;
      47             : 
      48             : 
      49             : }
      50             : 
      51          16 : bool SharedServerClient::getUsers(string* users) {
      52          32 :     string sub_urls[] = {SHARED_SERVER_USERS_SUB_URL};
      53          32 :     string url = formUrl(sub_urls, 1);
      54          32 :     RestClient::Response r = conn->get(url.c_str());
      55          48 :     return this->setBody(users,&r);
      56             : 
      57             : }
      58             : 
      59          42 : bool SharedServerClient::getUser(string user_id, string* user_data) {
      60         126 :     string sub_urls[] = {SHARED_SERVER_USERS_SUB_URL, user_id};
      61          84 :     string url = formUrl(sub_urls, 2);
      62          84 :     RestClient::Response r = conn->get(url.c_str());
      63         168 :     return this->setBody(user_data,&r);
      64             : 
      65             : }
      66             : 
      67          16 : bool SharedServerClient::postUsers(string &response, string* user_data) {
      68          32 :     string sub_urls[] = {SHARED_SERVER_USERS_SUB_URL};
      69          32 :     string url = formUrl(sub_urls, 1);
      70          32 :     RestClient::Response r = conn->post(url.c_str(), user_data->c_str());
      71          48 :     return this->setBody(&response,&r);
      72             : 
      73             : }
      74             : 
      75          25 : bool SharedServerClient::changeUser(string user_id, string user_data) {
      76          75 :     string sub_urls[] = {SHARED_SERVER_USERS_SUB_URL, user_id};
      77          50 :     string url = formUrl(sub_urls, 2);
      78          50 :     RestClient::Response r = conn->put(url.c_str(), user_data.c_str());
      79         100 :     return this->valid(&r);
      80             : }
      81             : 
      82           7 : bool SharedServerClient::changeUserPhoto(string user_id, string photo) {
      83          28 :     string sub_urls[] = {SHARED_SERVER_USERS_SUB_URL, user_id, SHARED_SERVER_PHOTO_SUB_URL};
      84          14 :     string url = formUrl(sub_urls, 3);
      85          14 :     JsonParser parser;
      86          14 :     string formated_data = parser.photoToJson(&photo);
      87          14 :     RestClient::Response r = conn->put(url.c_str(), formated_data.c_str());
      88          35 :     return this->valid(&r);
      89             : }
      90             : 
      91          16 : bool SharedServerClient::deleteUser(string user_id, string *response) {
      92          48 :     string sub_urls[] = {SHARED_SERVER_USERS_SUB_URL, user_id};
      93          32 :     string url = formUrl(sub_urls, 2);
      94          32 :     RestClient::Response r = conn->del(url.c_str());
      95          64 :     return this->setBody( response, &r);
      96             : 
      97             : }
      98             : 
      99           3 : bool SharedServerClient::getUsersInterests(string* interests) {
     100           6 :     string sub_urls[] = { SHARED_SERVER_INTERESTS_SUB_URL};
     101           6 :     string url = formUrl(sub_urls, 1);
     102           6 :     RestClient::Response r = conn->get(url.c_str());
     103           9 :     return this->setBody( interests, &r);
     104             : }
     105             : 
     106          27 : bool SharedServerClient::postUsersInterests(string* interest) {
     107          54 :     string sub_urls[] = { SHARED_SERVER_INTERESTS_SUB_URL};
     108          54 :     string url = formUrl(sub_urls, 1);
     109          54 :     RestClient::Response r = conn->post(url.c_str(), interest->c_str());
     110          81 :     return this->valid(&r);
     111             : }
     112             : 
     113           7 : bool SharedServerClient::getUserPhoto(string user_id, string &photo_64) {
     114           7 :         string data;
     115           7 :         if (this->getUser(user_id,&data)){
     116           7 :                 JsonParser parser;
     117           7 :                 parser.parsing(data);
     118           7 :                 photo_64 = parser.getValue(USER_KEY)[PHOTO_KEY].asString();
     119           7 :                 return true;
     120             :         }
     121           0 :         return false;
     122             : }
     123             : 
     124           7 : bool SharedServerClient::getInterestPhoto(string interest_id, string &photo_64) {
     125          28 :         string sub_urls[] = { SHARED_SERVER_INTERESTS_SUB_URL,interest_id,SHARED_SERVER_PHOTO_SUB_URL};
     126          14 :         string url = formUrl(sub_urls, 3);
     127          14 :         RestClient::Response r = conn->get(url.c_str());
     128          35 :         return setBody(&photo_64,&r);
     129          66 : }

Generated by: LCOV version 1.12-4-g04a3c0e