Search in sources :

Example 1 with User

use of model.db.User in project amos-ss17-alexa by c-i-ber.

the class AuthenticationAPI method getAccessToken.

/**
 * Returns the access token for the given user id.
 *
 * @param userId the user id
 * @return the access token
 */
public static String getAccessToken(String userId) {
    if (!accessTokenUsers.containsKey(userId)) {
        User user = (User) DynamoDbMapper.getInstance().load(User.class, userId);
        accessTokenUsers.put(user.getId(), user);
    }
    return accessTokenUsers.get(userId).getAccessToken();
}
Also used : User(model.db.User)

Example 2 with User

use of model.db.User in project amos-ss17-alexa by c-i-ber.

the class AuthenticationAPI method updateAccessToken.

/**
 * Updates the access token if necessary.
 *
 * @param userId the user id
 */
public static void updateAccessToken(String userId) {
    if (!accessTokenUsers.containsKey(userId)) {
        // (model.db.User) DynamoDbClient.instance.getItem(model.db.User.TABLE_NAME, userId, model.db.User.factory);
        User user = (User) DynamoDbMapper.getInstance().load(User.class, userId);
        accessTokenUsers.put(user.getId(), user);
    }
    model.db.User user = accessTokenUsers.get(userId);
    if (user == null) {
        user = new model.db.User();
        user.setBalanceLimit(0);
        user.setId(userId);
        // Update the user object in the db
        DynamoDbMapper.getInstance().save(user);
    // DynamoDbClient.instance.putItem(model.db.User.TABLE_NAME, user);
    }
    if (shouldRefresh(user)) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
        map.add("username", "anton");
        map.add("password", "anton");
        map.add("grant_type", "password");
        map.add("client_id", "public_auth");
        HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
        AuthResponse authResponse = new RestTemplate().exchange(AUTH_URL, HttpMethod.POST, request, AuthResponse.class).getBody();
        log.info("AuthToken: " + authResponse.getAccess_token());
        user.setAccessToken(authResponse.getAccess_token());
        // Add expiry time (seconds) to current time, store this value as a String in the User object
        int expiresInSeconds = Integer.valueOf(authResponse.getExpires_in());
        DateTime expiryDateTime = DateTime.now().withZone(DateTimeZone.UTC).plusSeconds(expiresInSeconds);
        String dtStr = fmt.print(expiryDateTime);
        user.setAccessTokenExpiryTime(dtStr);
        log.info("new expiry time: " + dtStr);
        // Store this token in our HashMap
        accessTokenUsers.put(user.getId(), user);
        // Update the user object in the db
        DynamoDbMapper.getInstance().save(user);
    // DynamoDbClient.instance.putItem(model.db.User.TABLE_NAME, user);
    }
}
Also used : User(model.db.User) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) DateTime(org.joda.time.DateTime) RestTemplate(org.springframework.web.client.RestTemplate) User(model.db.User) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap)

Example 3 with User

use of model.db.User in project amos-ss17-alexa by c-i-ber.

the class BalanceLimitService method onIntent.

@Override
public SpeechletResponse onIntent(SpeechletRequestEnvelope<IntentRequest> requestEnvelope) throws SpeechletException {
    Intent intent = requestEnvelope.getRequest().getIntent();
    Session session = requestEnvelope.getSession();
    SessionStorage.Storage sessionStorage = SessionStorage.getInstance().getStorage(session.getSessionId());
    model.db.User user = (User) DynamoDbMapper.getInstance().load(model.db.User.class, USER_ID);
    if (intent.getName().equals(SET_BALANCE_LIMIT_INTENT)) {
        Map<String, Slot> slots = intent.getSlots();
        Slot balanceLimitAmountSlot = slots.get("BalanceLimitAmount");
        if (balanceLimitAmountSlot == null || balanceLimitAmountSlot.getValue() == null) {
            return getAskResponse(CARD_TITLE, "Auf welchen Betrag möchtest du dein Kontolimit setzen?");
        }
        String balanceLimitAmount = balanceLimitAmountSlot.getValue();
        if (balanceLimitAmount.equals("?")) {
            return getErrorResponse("Der angegebene Betrag ist ungültig.");
        }
        sessionStorage.put(NEW_BALANCE_LIMIT, balanceLimitAmount);
        return getAskResponse(CARD_TITLE, "Möchtest du dein Kontolimit wirklich auf " + balanceLimitAmount + " Euro setzen?");
    } else if (intent.getName().equals(GET_BALANCE_LIMIT_INTENT)) {
        return getResponse(CARD_TITLE, "Dein aktuelles Kontolimit beträgt " + user.getBalanceLimit() + " Euro.");
    } else if (intent.getName().equals(YES_INTENT)) {
        if (!sessionStorage.containsKey(NEW_BALANCE_LIMIT)) {
            return getErrorResponse();
        }
        String balanceLimitAmount = (String) sessionStorage.get(NEW_BALANCE_LIMIT);
        user.setBalanceLimit(Integer.parseInt(balanceLimitAmount));
        DynamoDbMapper.getInstance().save(user);
        return getResponse(CARD_TITLE, "Okay, dein Kontolimit wurde auf " + balanceLimitAmount + " Euro gesetzt.");
    } else if (intent.getName().equals(NO_INTENT)) {
        return getResponse(CARD_TITLE, "");
    }
    return null;
}
Also used : User(model.db.User) User(model.db.User) Slot(com.amazon.speech.slu.Slot) Intent(com.amazon.speech.slu.Intent) SessionStorage(amosalexa.SessionStorage) Session(com.amazon.speech.speechlet.Session)

Aggregations

User (model.db.User)3 SessionStorage (amosalexa.SessionStorage)1 Intent (com.amazon.speech.slu.Intent)1 Slot (com.amazon.speech.slu.Slot)1 Session (com.amazon.speech.speechlet.Session)1 DateTime (org.joda.time.DateTime)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 MultiValueMap (org.springframework.util.MultiValueMap)1 RestTemplate (org.springframework.web.client.RestTemplate)1