/** * The main body of the code. */ private void run() { QuandlSession session = QuandlSession.create(); TabularResult tabularResult = session.getDataSet( DataSetRequest.Builder.of("WIKI/AAPL").build()); System.out.println(tabularResult.toPrettyPrintedString()); }
@Override public Map<String, HeaderDefinition> getMultipleHeaderDefinition(final MultiMetaDataRequest request) { final Map<String, HeaderDefinition> bulkMetaData = new LinkedHashMap<String, HeaderDefinition>(); for (final String quandlCode : request.getQuandlCodes()) { try { MetaDataResult metaData = getMetaData(MetaDataRequest.of(quandlCode)); bulkMetaData.put(quandlCode, metaData.getHeaderDefinition()); } catch (final QuandlRuntimeException qre) { s_logger.error("There was a problem requesting metadata for {}, skipping", quandlCode, qre); continue; } } return bulkMetaData; }
/** * The main body of the code. */ private void run() { QuandlSession session = QuandlSession.create(); Map<String, HeaderDefinition> headers = session.getMultipleHeaderDefinition(MultiMetaDataRequest.of("WIKI/AAPL", "DOE/RWTC", "WIKI/MSFT")); System.out.println(PrettyPrinter.toPrettyPrintedString(headers)); }
/** * The main body of the code. */ private void run() { QuandlSession session = QuandlSession.create(); MetaDataResult metaData = session.getMetaData(MetaDataRequest.of("WIKI/AAPL")); System.out.println(metaData.toPrettyPrintedString()); }
/** * The main body of the code. */ private void run() { QuandlSession quandl = QuandlSession.create(); SearchResult searchResult = quandl.search(SearchRequest.Builder.ofQuery("Apple").withMaxPerPage(2).build()); System.out.println(searchResult.toPrettyPrintedString()); for (MetaDataResult metaData : searchResult.getMetaDataResultList()) { System.out.println(PrettyPrinter.toPrettyPrintedString(metaData.getRawJSON())); } TabularResult tabularResult = null; tabularResult = quandl.getDataSet(DataSetRequest.Builder.of("WIKI/AAPL").withFrequency(Frequency.QUARTERLY) .withColumn(CLOSE_COLUMN).withTransform(Transform.NORMALIZE).build()); System.out.println(PrettyPrinter.toPrettyPrintedString(tabularResult)); TabularResult tabularResultMulti = quandl.getDataSets( MultiDataSetRequest.Builder.of( QuandlCodeRequest.allColumns("WIKI/AAPL"), QuandlCodeRequest.allColumns("DOE/RWTC") ).withStartDate(RECENTISH_DATE) .build()); System.out.println(PrettyPrinter.toPrettyPrintedString(tabularResultMulti)); MetaDataResult metaData = quandl.getMetaData(MetaDataRequest.of("WIKI/AAPL")); System.out.println(PrettyPrinter.toPrettyPrintedString(metaData.getRawJSON())); MetaDataResult metaData2 = quandl.getMetaData(MultiMetaDataRequest.of("WIKI/AAPL", "DOE/RWTC", "WIKI/MSFT")); System.out.println(PrettyPrinter.toPrettyPrintedString(metaData2.getRawJSON())); }
@Test public void testSimpleGetDataSet() { QuandlSession session = getTestSession("https://www.quandl.com/api/v3/datasets/WIKI/MSFT.csv"); TabularResult tabularResult = session.getDataSet(DataSetRequest.Builder.of("WIKI/MSFT").build()); Assert.assertEquals(TEST_TABULAR_RESULT, tabularResult); }
TabularResult tabularResult = null; try { tabularResult = getDataSet(dataSetRequest); } catch (final QuandlRuntimeException qre) { s_logger.error("Exception processing request for {}, giving up and skipping. Full request was {}", return mergeTables(results, request.getSortOrder());
/** * The main body of the code. */ private void run() { QuandlSession session = QuandlSession.create(); TabularResult tabularResultMulti = session.getDataSets( MultiDataSetRequest.Builder .of( QuandlCodeRequest.singleColumn("WIKI/AAPL", CLOSE_COLUMN), QuandlCodeRequest.allColumns("DOE/RWTC") ) .withStartDate(RECENTISH_DATE) .withFrequency(Frequency.MONTHLY) .build()); System.out.println(tabularResultMulti.toPrettyPrintedString()); }
@Override public MetaDataResult getMetaData(final MultiMetaDataRequest request) { ArgumentChecker.notNull(request, "request"); Map<String, HeaderDefinition> multipleHeaderDefinition = getMultipleHeaderDefinition(request); try { JSONObject result = new JSONObject(); if (multipleHeaderDefinition.size() > 0) { result.append(JSON_COLUMN_NAMES_FIELD, DATE_COLUMN); result.append(JSON_COLUMNS_FIELD, DATE_COLUMN); } for (Map.Entry<String, HeaderDefinition> entry : multipleHeaderDefinition.entrySet()) { String quandlCode = entry.getKey(); HeaderDefinition headerDef = entry.getValue(); for (String columnName : headerDef.getColumnNames()) { if (!columnName.equals(DATE_COLUMN)) { // skip Date column for each data set. result.append(JSON_COLUMN_NAMES_FIELD, quandlCode + " - " + columnName); result.append(JSON_COLUMNS_FIELD, columnName); } } } result.put(JSON_DATA_FIELD, new JSONArray()); result.put(JSON_ERRORS_FIELD, Collections.emptyMap()); result.put(JSON_FREQUENCY_FIELD, (Object) null); result.put(JSON_FROM_DATE_FIELD, (Object) null); result.put(JSON_TO_DATE_FIELD, (Object) null); return MetaDataResult.of(result); } catch (JSONException ex) { throw new QuandlRuntimeException("Problem building JSON response", ex); } }
@Override public TabularResult getDataSet(final DataSetRequest request) { ArgumentChecker.notNull(request, "request"); Client client = getClient(); WebTarget target = client.target(API_BASE_URL_V3); target = withAuthToken(target); target = request.appendPathAndQueryParameters(target); int retries = 0; QuandlRuntimeException lastException; do { try { return _restDataProvider.getTabularResponse(target, request); } catch (QuandlTooManyRequestsException qtmre) { lastException = qtmre; s_logger.debug("Quandl returned Too Many Requests, retrying if appropriate"); if (qtmre.isDataExhausted()) { throw new QuandlRequestFailedException("Data request limit exceeded", qtmre); } } catch (QuandlServiceUnavailableException qsue) { lastException = qsue; s_logger.debug("Quandl returned Service Not Available, retrying if appropriate"); } } while (_sessionOptions.getRetryPolicy().checkRetries(retries++)); throw new QuandlRequestFailedException("Giving up on request after " + retries + " retries.", lastException); }
@Test public void testSimpleSearch() { QuandlSession session = getTestSession("https://www.quandl.com/api/v3/datasets.json?query=Crude+Oil"); SearchResult result = session.search(SearchRequest.Builder.of("Crude Oil").build()); Assert.assertEquals(result, SearchResult.of(new JSONObject())); }
/** * Create a Quandl session with detailed SessionOptions. No attempt will be made to read the java property <em>quandl.auth.token</em> even * if available. Note creating this object does not make any actual API requests, the token is used in subsequent requests. * * @param sessionOptions a user created SessionOptions instance, not null * @return an instance of the Quandl session, not null */ public static QuandlSession create(final SessionOptions sessionOptions) { ArgumentChecker.notNull(sessionOptions, "sessionOptions"); return new QuandlSession(sessionOptions); }
@Test(expectedExceptions = QuandlRuntimeException.class) public void testLargeMultiDataSetRequestWithEmpty() { QuandlSession session = getTestSession("https://www.quandl.com/api/v3/multisets.csv?columns=&collapse=monthly&transformation=normalize"); TabularResult tabularResult = session.getDataSets( // expect an exception here because list is empty MultiDataSetRequest.Builder.of() .withFrequency(Frequency.MONTHLY) .withTransform(Transform.NORMALIZE) .build()); Assert.assertEquals(TEST_TABULAR_RESULT, tabularResult); }
private QuandlSession getTestSession(final String expectedURL) { return QuandlSession.create( SessionOptions.Builder.withoutAuthToken() .withRESTDataProvider( new TestRESTDataProvider(expectedURL)) .build()); }
@Override public SearchResult search(final SearchRequest request) { Client client = ClientBuilder.newClient(); WebTarget target = client.target(API_BASE_URL_V3); target = withAuthToken(target); target = request.appendPathAndQueryParameters(target); int retries = 0; QuandlRuntimeException lastException; do { try { return SearchResult.of(_restDataProvider.getJSONResponse(target, request)); } catch (QuandlTooManyRequestsException qtmre) { lastException = qtmre; s_logger.debug("Quandl returned Too Many Requests, retrying if appropriate"); if (qtmre.isDataExhausted()) { throw new QuandlRequestFailedException("Data request limit exceeded", qtmre); } } catch (QuandlServiceUnavailableException qsue) { lastException = qsue; s_logger.debug("Quandl returned Service Not Available, retrying if appropriate"); } } while (_sessionOptions.getRetryPolicy().checkRetries(retries++)); throw new QuandlRequestFailedException("Giving up on request after " + retries + " retries.", lastException); } }
/** * Run a set of data set requests using the provided quandl codes. * @param session the Quandl session * @param resultProcessor a result processor to ether record or check the results * @param quandlCodes a random set of Quandl codes to construct the requests from */ private void fuzzDataSetRequests(final QuandlSession session, final ResultProcessor resultProcessor, final Set<String> quandlCodes) { for (String quandlCode : quandlCodes) { DataSetRequest req = fuzz(DataSetRequest.Builder.of(quandlCode)).build(); try { @SuppressWarnings("unused") TabularResult dataSet = session.getDataSet(req); //s_logger.info(req.toString()); //s_logger.info(PrettyPrinter.toPrettyPrintedString(dataSet)); } catch (QuandlRuntimeException qre) { s_logger.warn("Caught" + qre); s_logger.info("Continuing..."); } } }
/** * The main body of the code. */ private void run() { QuandlSession session = QuandlSession.create(); MetaDataResult metaData = session.getMetaData(MultiMetaDataRequest.of("WIKI/AAPL", "DOE/RWTC", "WIKI/MSFT")); System.out.println(metaData.toPrettyPrintedString()); }
/** * The main body of the code. */ private void run() { QuandlSession session = QuandlSession.create(); SearchResult searchResult = session.search(new SearchRequest.Builder().withQuery("Apple").withMaxPerPage(2).build()); System.out.println("Current page:" + searchResult.getCurrentPage()); System.out.println("Documents per page:" + searchResult.getDocumentsPerPage()); System.out.println("Total matching documents:" + searchResult.getTotalDocuments()); for (MetaDataResult document : searchResult.getMetaDataResultList()) { System.out.println("Quandl code " + document.getQuandlCode() + " matched"); System.out.println("Available columns are: " + document.getHeaderDefinition()); } }
/** * The main body of the code. */ private void run() { QuandlSession session = QuandlSession.create(); TabularResult tabularResultMulti = session.getDataSets( MultiDataSetRequest.Builder .of( QuandlCodeRequest.singleColumn("WIKI/AAPL", CLOSE_COLUMN), QuandlCodeRequest.allColumns("DOE/RWTC") ) .withStartDate(RECENTISH_DATE) .withFrequency(Frequency.MONTHLY) .build()); System.out.println("Header definition: " + tabularResultMulti.getHeaderDefinition()); Iterator<Row> iter = tabularResultMulti.iterator(); while (iter.hasNext()) { Row row = iter.next(); LocalDate date = row.getLocalDate("Date"); Double value = row.getDouble("DOE/RWTC - Value"); System.out.println("Value on date " + date + " was " + value); } }