Search in sources :

Example 1 with JSONException

use of com.amazonaws.util.json.JSONException in project amos-ss17-alexa by c-i-ber.

the class FinanceApi method getStockPrice.

public static String getStockPrice(String stock) {
    try {
        FinanceApi finance = new FinanceApi();
        String response = finance.run("http://finance.google.com/finance/info?client=ig&q=NASDAQ:" + stock);
        response = response.substring(5, response.length() - 2);
        //create a JSON-Object of the String
        final JSONObject obj = new JSONObject(response);
        // read out the current stock price
        //Gson gson = new Gson();
        JsonObject jsonObject = new JsonParser().parse(response).getAsJsonObject();
        // JsonObject jsonObject = new JsonParser().parse("{\"pcls_fix\": \"68.38\"}").getAsJsonObject();
        String stockPrice = jsonObject.get("pcls_fix").getAsString();
        return stockPrice;
    } catch (IOException io) {
        System.out.println("Error: " + io);
    } catch (JSONException json) {
        System.out.println("Error: " + json);
    }
    return null;
}
Also used : JSONObject(com.amazonaws.util.json.JSONObject) JsonObject(com.google.gson.JsonObject) JSONException(com.amazonaws.util.json.JSONException) IOException(java.io.IOException) JsonParser(com.google.gson.JsonParser)

Example 2 with JSONException

use of com.amazonaws.util.json.JSONException in project amos-ss17-alexa by c-i-ber.

the class SavingsPlanDialog method createSavingsPlanOneOffPayment.

private void createSavingsPlanOneOffPayment(String betrag) {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("amount", betrag);
        //TODO Hard coded savings account?
        jsonObject.put("sourceAccount", "DE42100000009999999999");
        jsonObject.put("destinationAccount", "DE39100000007777777777");
        jsonObject.put("valueDate", "2017-05-24");
        jsonObject.put("description", "Savings Plan");
    } catch (JSONException e) {
        LOGGER.error(e.getMessage());
    }
    BankingRESTClient bankingRESTClient = BankingRESTClient.getInstance();
    //TODO Post mapped to Object.class
    bankingRESTClient.postBankingModelObject("/api/v1_0/transactions", jsonObject.toString(), Object.class);
}
Also used : JSONObject(com.amazonaws.util.json.JSONObject) JSONException(com.amazonaws.util.json.JSONException) BankingRESTClient(api.BankingRESTClient)

Example 3 with JSONException

use of com.amazonaws.util.json.JSONException in project zeppelin by apache.

the class ZeppelinhubClient method runAllParagraph.

boolean runAllParagraph(String noteId, String hubMsg) {
    LOG.info("Running paragraph with noteId {}", noteId);
    try {
        JSONObject data = new JSONObject(hubMsg);
        if (data.equals(JSONObject.NULL) || !(data.get("data") instanceof JSONArray)) {
            LOG.error("Wrong \"data\" format for RUN_NOTEBOOK");
            return false;
        }
        Client client = Client.getInstance();
        if (client == null) {
            LOG.warn("Base client isn't initialized, returning");
            return false;
        }
        Message zeppelinMsg = new Message(OP.RUN_PARAGRAPH);
        JSONArray paragraphs = data.getJSONArray("data");
        for (int i = 0; i < paragraphs.length(); i++) {
            if (!(paragraphs.get(i) instanceof JSONObject)) {
                LOG.warn("Wrong \"paragraph\" format for RUN_NOTEBOOK");
                continue;
            }
            zeppelinMsg.data = gson.fromJson(paragraphs.getString(i), new TypeToken<Map<String, Object>>() {
            }.getType());
            client.relayToZeppelin(zeppelinMsg, noteId);
            LOG.info("\nSending RUN_PARAGRAPH message to Zeppelin ");
        }
    } catch (JSONException e) {
        LOG.error("Failed to parse RUN_NOTEBOOK message from ZeppelinHub ", e);
        return false;
    }
    return true;
}
Also used : JSONObject(com.amazonaws.util.json.JSONObject) Message(org.apache.zeppelin.notebook.socket.Message) ZeppelinhubMessage(org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.protocol.ZeppelinhubMessage) JSONArray(com.amazonaws.util.json.JSONArray) JSONException(com.amazonaws.util.json.JSONException) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Map(java.util.Map)

Example 4 with JSONException

use of com.amazonaws.util.json.JSONException in project ice by Netflix.

the class EddaResourceService method getResource.

@Override
public String getResource(Account account, Region region, Product product, String resourceId, String[] lineItem, long millisStart) {
    // currently we support ec2
    if (Product.ec2.equals(product) || Product.ec2_instance.equals(product)) {
        if (StringUtils.isEmpty(resourceId)) {
            logger.warn("Had empty resourceId");
            return "Error";
        }
        try {
            JSONArray instances = readInstanceArray();
            boolean found = false;
            for (int i = 0; i < instances.length(); i++) {
                String instance = instances.getString(i);
                if (resourceId.equals(instance)) {
                    found = true;
                    break;
                }
            }
            if (!found) {
                logger.warn("Did not find resourceId in edda: " + resourceId);
                return "Unknown";
            }
            InputStream stream = new URL(EDDA_ROOT_URL + "view/instances/" + resourceId).openStream();
            final String json;
            try {
                json = IOUtils.toString(stream);
            } finally {
                stream.close();
            }
            JSONObject object = new JSONObject(json);
            JSONArray tags = object.getJSONArray("tags");
            for (int i = 0; i < tags.length(); i++) {
                JSONObject tag = tags.getJSONObject(i);
                String key = tag.getString("key");
                if (key.equals(EDDA_TAG_NAME)) {
                    String usage = tag.getString("value");
                    logger.debug("Found usage: " + usage + " for resource " + resourceId);
                    return usage;
                }
            }
            logger.debug("Did not find tag 'Usage' for resource " + resourceId);
            return "Unknown";
        } catch (JSONException e) {
            logger.warn("error parsing json", e);
            return "Error";
        } catch (MalformedURLException e) {
            logger.warn("error parsing url", e);
            return "Error";
        } catch (IOException e) {
            logger.warn("error fetching data from edda at " + EDDA_ROOT_URL, e);
            return "Error";
        }
    }
    logger.debug("Product: " + product + " not handled, resourceId: " + resourceId);
    //logger.info("get resource for account " + account + " region " + region + " product " + product + " resource: " + resourceId + " lineItem: " + Arrays.toString(lineItem));
    return super.getResource(account, region, product, resourceId, lineItem, millisStart);
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONObject(com.amazonaws.util.json.JSONObject) InputStream(java.io.InputStream) JSONArray(com.amazonaws.util.json.JSONArray) JSONException(com.amazonaws.util.json.JSONException) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with JSONException

use of com.amazonaws.util.json.JSONException in project amos-ss17-alexa by c-i-ber.

the class SavingsPlanDialog method createSavingsPlanStandingOrder.

private void createSavingsPlanStandingOrder(String monatlicheZahlung) {
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("payee", "Max Mustermann");
        jsonObject.put("amount", monatlicheZahlung);
        //TODO Hard coded savings account?
        jsonObject.put("destinationAccount", "DE39100000007777777777");
        jsonObject.put("firstExecution", "2017-06-01");
        jsonObject.put("executionRate", "MONTHLY");
        jsonObject.put("description", "Savings Plan");
    } catch (JSONException e) {
        LOGGER.error(e.getMessage());
    }
    BankingRESTClient bankingRESTClient = BankingRESTClient.getInstance();
    bankingRESTClient.postBankingModelObject("/api/v1_0/accounts/9999999999/standingorders", jsonObject.toString(), StandingOrder.class);
}
Also used : JSONObject(com.amazonaws.util.json.JSONObject) JSONException(com.amazonaws.util.json.JSONException) BankingRESTClient(api.BankingRESTClient)

Aggregations

JSONException (com.amazonaws.util.json.JSONException)6 JSONObject (com.amazonaws.util.json.JSONObject)6 BankingRESTClient (api.BankingRESTClient)3 JSONArray (com.amazonaws.util.json.JSONArray)2 IOException (java.io.IOException)2 PlainTextOutputSpeech (com.amazon.speech.ui.PlainTextOutputSpeech)1 SimpleCard (com.amazon.speech.ui.SimpleCard)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Map (java.util.Map)1 ZeppelinhubMessage (org.apache.zeppelin.notebook.repo.zeppelinhub.websocket.protocol.ZeppelinhubMessage)1 Message (org.apache.zeppelin.notebook.socket.Message)1 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)1