Exemplo n.º 1
0
    def config_err_obj(self, response):
        err = RCReturnObsErr()
        if (RCErrDomain.is_rcclient_err(response.status_code)):
            err.config_domain(RCErrDomain.CLIENT_DOMAIN) \
             .config_code(RCErrDomain.CLIENT_CODE)

        elif (RCErrDomain.is_rcserver_err(response.status_code)):
            err = RCReturnObsErr().config_domain(RCErrDomain.SERVER_DOMAIN) \
                   .config_code(RCErrDomain.SERVER_CODE)
        msg = response.json().get('m`essage') or response.json().get(
            'error') or ""
        err.config_msg(requests.status_codes._codes[response.status_code][0] +
                       msg)
        return err
Exemplo n.º 2
0
 def get_user_by_username(self, username):
     headers = {'X-Auth-Token': self.auth_token, 'X-User-Id': self.user_id}
     params = {'username': username}
     get_url = self.url + RCAPI.GET_USER_INFO
     response = requests.get(get_url, headers=headers, params=params)
     r = response.json()
     obj = RCReturnObs(False)
     if (RCErrDomain.is_rclogic_err(response.status_code)):
         is_success = r.get('success')
         user = r.get('user')
         if (is_success and user):
             temp = RCUserData(user.get('_id'))
             temp.config_user(user.get('name'),
                 user.get('username')) \
               .config_roles(user.get('roles'))
             obj = RCUserInfoReturn(is_success, temp)
         else:
             err = RCReturnObsErr().config_domain(RCErrDomain.LOGIC_DOMAIN) \
                   .config_code(RCErrDomain.LOGIC_CODE) \
                   .config_msg(RCErrDomain.NULL_DATA)
             obj.config_err(err)
     else:
         err = self.config_err_obj(response)
         obj.config_err(err)
     return obj
Exemplo n.º 3
0
 def update_message(self, rid, mid, text):
     headers = self.headers()
     payload = json.dumps({'roomId': rid, 'msgId': mid, 'text': text})
     post_url = self.url + RCAPI.UPDATE_MESSAGE
     response = requests.post(post_url, headers=headers, data=payload)
     r = response.json()
     obj = RCReturnObs(False)
     if (RCErrDomain.is_rclogic_err(response.status_code)):
         is_success = r.get('success')
         msg = r.get('message')
         if (is_success and msg):
             obj = RCUpdateMessReturn(is_success)
             temp_mess = RCMessage(msg.get('_id'))
             temp_user = RCUserData(msg.get('u').get('_id'))
             temp_user.config_user(
                 msg.get('u').get('username'),
                 msg.get('u').get('username'))
             temp_mess.config(msg.get('msg'),
                              temp_user).config_rid(msg.get('rid'))
             obj.config_mess(temp_mess)
         else:
             err = RCReturnObsErr().config_domain(RCErrDomain.LOGIC_DOMAIN) \
                   .config_code(RCErrDomain.LOGIC_CODE) \
                   .config_msg(RCErrDomain.NULL_DATA)
             obj.config_err(err)
     else:
         err = self.config_err_obj(response)
         obj.config_err(err)
     return obj
Exemplo n.º 4
0
    def get_users(self):
        headers = {'X-Auth-Token': self.auth_token, 'X-User-Id': self.user_id}
        getUsersUrl = self.url + RCAPI.GET_USER
        response = requests.get(getUsersUrl, headers=headers)
        r = response.json()
        obj = RCReturnObs(False)

        if (RCErrDomain.is_rclogic_err(response.status_code)):
            is_success = r.get('success')
            users = r.get('users')

            if (is_success and users):
                obj = RCGetUserReturn(is_success, [])
                for json_obj in users:
                    temp = RCUserData(json_obj.get('_id'))
                    temp.config_user(json_obj.get('name'),
                         json_obj.get('username')) \
                     .config_roles(json_obj.get('roles'))
                    obj.add_user(temp)
            else:
                err = RCReturnObsErr().config_domain(RCErrDomain.LOGIC_DOMAIN) \
                      .config_code(RCErrDomain.LOGIC_CODE) \
                      .config_msg(RCErrDomain.NULL_DATA)
                obj.config_err(err)
        else:
            err = self.config_err_obj(response)
            obj.config_err(err)
        return obj
Exemplo n.º 5
0
 def login(self, username, password):
     payload = {'username': username, 'password': password}
     login_url = self.url + RCAPI.LOGIN
     response = requests.post(login_url, json=payload)
     r = response.json()
     obj = RCReturnObs(False)
     if (RCErrDomain.is_rclogic_err(response.status_code)):
         data = r.get('data')
         is_success = r.get('status') == "success"
         if (is_success and data):
             obj = RCLoginReturn(is_success, data.get('userId'),
                                 data.get('authToken'))
         else:
             err = RCReturnObsErr().config_domain(RCErrDomain.LOGIC_DOMAIN) \
                   .config_code(RCErrDomain.LOGIC_CODE) \
                   .config_msg(r.get('error') or RCErrDomain.NULL_DATA)
             obj.config_err(err)
     else:
         err = self.config_err_obj(response)
         obj.config_err(err)
     return obj
Exemplo n.º 6
0
    def post_message(self, channel, text, opt=None):
        headers = self.headers()

        payload = {'channel': channel, 'text': text}
        if opt is not None:
            payload.update(opt)

        payload = json.dumps(payload)
        post_message_url = self.url + RCAPI.POST_MESSAGE
        response = requests.post(post_message_url,
                                 headers=headers,
                                 data=payload)
        r = response.json()
        obj = RCReturnObs(False)
        if (RCErrDomain.is_rclogic_err(response.status_code)):
            is_success = r.get('success')
            msgs = r.get('message')
            if (is_success and msgs):
                obj = RCPostMessReturn(is_success)
                for msg in msgs:
                    temp_mess = RCMessage(msg.get('_id'))
                    temp_user = RCUserData(msg.get('u').get('_id'))
                    temp_user.config_user(
                        msg.get('u').get('name'),
                        msg.get('u').get('username'))
                    temp_mess.config(msg.get('msg'),
                                     temp_user).config_rid(msg.get('rid'))
                    obj.add_msg(temp_mess)
                obj.config_channel(r.get('channel'))
            else:
                err = RCReturnObsErr().config_domain(RCErrDomain.LOGIC_DOMAIN) \
                      .config_code(RCErrDomain.LOGIC_CODE) \
                      .config_msg(RCErrDomain.NULL_DATA)
                obj.config_err(err)
        else:
            err = self.config_err_obj(response)
            obj.config_err(err)
        return obj