Beispiel #1
0
def update_state(request, user_profile, state=REQ(validator=check_dict([]))):
    # type: (HttpRequest, UserProfile, Optional[Dict[str, str]]) -> HttpResponse
    try:
        set_bot_state(user_profile, list(state.items()))
    except StateError as e:
        return json_error(str(e))
    return json_success()
Beispiel #2
0
 def put(self, key, value):
     # type: (Text, Text) -> None
     old_entry_size = get_bot_state_size(self.user_profile, key)
     new_entry_size = len(key) + len(value)
     old_state_size = get_bot_state_size(self.user_profile)
     new_state_size = old_state_size + (new_entry_size - old_entry_size)
     if new_state_size > self.state_size_limit:
         raise StateHandlerError("Cannot set state. Request would require {} bytes storage. "
                                 "The current storage limit is {}.".format(new_state_size, self.state_size_limit))
     elif type(key) is not str:
         raise StateHandlerError("Cannot set state. The key type is {}, but it should be str.".format(type(key)))
     else:
         marshaled_value = self.marshal(value)
         if type(marshaled_value) is not str:
             raise StateHandlerError("Cannot set state. The value type is {}, but it "
                                     "should be str.".format(type(marshaled_value)))
         set_bot_state(self.user_profile, key, marshaled_value)
Beispiel #3
0
 def put(self, key, value):
     # type: (Text, Text) -> None
     old_entry_size = get_bot_state_size(self.user_profile, key)
     new_entry_size = len(key) + len(value)
     old_state_size = get_bot_state_size(self.user_profile)
     new_state_size = old_state_size + (new_entry_size - old_entry_size)
     if new_state_size > self.state_size_limit:
         raise StateHandlerError("Cannot set state. Request would require {} bytes storage. "
                                 "The current storage limit is {}.".format(new_state_size, self.state_size_limit))
     elif type(key) is not str:
         raise StateHandlerError("Cannot set state. The key type is {}, but it should be str.".format(type(key)))
     else:
         marshaled_value = self.marshal(value)
         if type(marshaled_value) is not str:
             raise StateHandlerError("Cannot set state. The value type is {}, but it "
                                     "should be str.".format(type(marshaled_value)))
         set_bot_state(self.user_profile, key, marshaled_value)
Beispiel #4
0
 def put(self, key, value):
     # type: (Text, Text) -> None
     set_bot_state(self.user_profile, key, self.marshal(value))
Beispiel #5
0
 def put(self, key: Text, value: Text) -> None:
     set_bot_state(self.user_profile, [(key, self.marshal(value))])