List of usage examples for org.apache.http.client.methods HttpUriRequest setHeader
void setHeader(Header header);
From source file:com.precioustech.fxtrading.oanda.restapi.trade.OandaTradeManagementProvider.java
@Override public Collection<Trade<Long, String, Long>> getTradesForAccount(Long accountId) { Collection<Trade<Long, String, Long>> allTrades = Lists.newArrayList(); CloseableHttpClient httpClient = getHttpClient(); try {/*from w ww .j a va2 s . co m*/ HttpUriRequest httpGet = new HttpGet(getTradesInfoUrl(accountId)); httpGet.setHeader(this.authHeader); httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER); LOG.info(TradingUtils.executingRequestMsg(httpGet)); HttpResponse resp = httpClient.execute(httpGet); String strResp = TradingUtils.responseToString(resp); if (strResp != StringUtils.EMPTY) { Object obj = JSONValue.parse(strResp); JSONObject jsonResp = (JSONObject) obj; JSONArray accountTrades = (JSONArray) jsonResp.get(trades); for (Object accountTrade : accountTrades) { JSONObject trade = (JSONObject) accountTrade; Trade<Long, String, Long> tradeInfo = parseTrade(trade, accountId); allTrades.add(tradeInfo); } } else { TradingUtils.printErrorMsg(resp); } } catch (Exception ex) { LOG.error("error encountered whilst fetching trades for account:" + accountId, ex); } finally { TradingUtils.closeSilently(httpClient); } return allTrades; }
From source file:jetbrains.buildServer.commitPublisher.github.api.impl.GitHubApiImpl.java
private void setDefaultHeaders(@NotNull HttpUriRequest request) { request.setHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "UTF-8")); request.setHeader(new BasicHeader(HttpHeaders.ACCEPT, "application/json")); }
From source file:com.precioustech.fxtrading.oanda.restapi.marketdata.historic.OandaHistoricMarketDataProvider.java
private List<CandleStick<String>> getCandleSticks(TradeableInstrument<String> instrument, String url, CandleStickGranularity granularity) throws OandaLimitExceededException { List<CandleStick<String>> allCandleSticks = Lists.newArrayList(); CloseableHttpClient httpClient = getHttpClient(); try {// w ww .j a v a 2 s . com HttpUriRequest httpGet = new HttpGet(url); httpGet.setHeader(authHeader); httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER); LOG.info(TradingUtils.executingRequestMsg(httpGet)); HttpResponse resp = httpClient.execute(httpGet); String strResp = TradingUtils.responseToString(resp); if (strResp != StringUtils.EMPTY) { Object obj = JSONValue.parse(strResp); JSONObject jsonResp = (JSONObject) obj; JSONArray candlsticks = (JSONArray) jsonResp.get(candles); for (Object o : candlsticks) { JSONObject candlestick = (JSONObject) o; final double openPrice = ((Number) candlestick.get(openMid)).doubleValue(); final double highPrice = ((Number) candlestick.get(highMid)).doubleValue(); final double lowPrice = ((Number) candlestick.get(lowMid)).doubleValue(); final double closePrice = ((Number) candlestick.get(closeMid)).doubleValue(); final long timestamp = Long.parseLong(candlestick.get(time).toString()); CandleStick<String> candle = new CandleStick<String>(openPrice, highPrice, lowPrice, closePrice, new DateTime(TradingUtils.toMillisFromNanos(timestamp)), instrument, granularity); allCandleSticks.add(candle); } } else { String errResponse = TradingUtils.getResponse(resp); Object obj = JSONValue.parse(errResponse); JSONObject jsonResp = (JSONObject) obj; int errCode = Integer.parseInt(jsonResp.get(code).toString()); if (errCode == LIMIT_ERR_CODE) { throw new OandaLimitExceededException(); } System.err.println(errResponse); } } catch (OandaLimitExceededException leex) { throw leex; } catch (Exception e) { LOG.error(String.format( "exception encountered whilst retrieving candlesticks for instrument %s with granularity %s", instrument.getInstrument(), granularity.getLabel()), e); } finally { TradingUtils.closeSilently(httpClient); } return allCandleSticks; }
From source file:com.precioustech.fxtrading.oanda.restapi.trade.OandaTradeManagementProvider.java
@Override public Trade<Long, String, Long> getTradeForAccount(Long tradeId, Long accountId) { CloseableHttpClient httpClient = getHttpClient(); try {/*from w w w. ja va2s. co m*/ HttpUriRequest httpGet = new HttpGet(getTradeForAccountUrl(tradeId, accountId)); httpGet.setHeader(this.authHeader); httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER); LOG.info(TradingUtils.executingRequestMsg(httpGet)); HttpResponse resp = httpClient.execute(httpGet); String strResp = TradingUtils.responseToString(resp); if (strResp != StringUtils.EMPTY) { JSONObject trade = (JSONObject) JSONValue.parse(strResp); return parseTrade(trade, accountId); } else { TradingUtils.printErrorMsg(resp); } } catch (Exception ex) { LOG.error( String.format("error encountered whilst fetching trade %d for account %d", tradeId, accountId), ex); } finally { TradingUtils.closeSilently(httpClient); } return null; }
From source file:com.precioustech.fxtrading.oanda.restapi.order.OandaOrderManagementProvider.java
@Override public Order<String, Long> pendingOrderForAccount(Long orderId, Long accountId) { CloseableHttpClient httpClient = getHttpClient(); try {/*from ww w.j a v a 2s.c om*/ HttpUriRequest httpGet = new HttpGet(orderForAccountUrl(accountId, orderId)); httpGet.setHeader(this.authHeader); httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER); LOG.info(TradingUtils.executingRequestMsg(httpGet)); HttpResponse resp = httpClient.execute(httpGet); String strResp = TradingUtils.responseToString(resp); if (strResp != StringUtils.EMPTY) { JSONObject order = (JSONObject) JSONValue.parse(strResp); return parseOrder(order); } else { TradingUtils.printErrorMsg(resp); } } catch (Exception e) { LOG.error( String.format("error encountered whilst fetching pending order for account %d and order id %d", accountId, orderId), e); } finally { TradingUtils.closeSilently(httpClient); } return null; }
From source file:com.precioustech.fxtrading.oanda.restapi.account.transaction.OandaTransactionDataProviderService.java
@Override public Transaction<Long, Long, String> getTransaction(Long transactionId, Long accountId) { Preconditions.checkNotNull(transactionId); Preconditions.checkNotNull(accountId); CloseableHttpClient httpClient = getHttpClient(); try {//from ww w .j ava 2s . c o m HttpUriRequest httpGet = new HttpGet(getSingleAccountTransactionUrl(transactionId, accountId)); httpGet.setHeader(authHeader); httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER); LOG.info(TradingUtils.executingRequestMsg(httpGet)); HttpResponse httpResponse = httpClient.execute(httpGet); String strResp = TradingUtils.responseToString(httpResponse); if (strResp != StringUtils.EMPTY) { JSONObject transactionJson = (JSONObject) JSONValue.parse(strResp); final String instrument = transactionJson.get(OandaJsonKeys.instrument) .toString();/* do not use derive instrument here */ final String type = transactionJson.get(OandaJsonKeys.type).toString(); final Long tradeUnits = getUnits(transactionJson); final DateTime timestamp = getTransactionTime(transactionJson); final Double pnl = getPnl(transactionJson); final Double interest = getInterest(transactionJson); final Double price = getPrice(transactionJson); final String side = getSide(transactionJson); return new Transaction<Long, Long, String>(transactionId, OandaUtils.toOandaTransactionType(type), accountId, instrument, tradeUnits, OandaUtils.toTradingSignal(side), timestamp, price, interest, pnl); } else { TradingUtils.printErrorMsg(httpResponse); } } catch (Exception e) { LOG.error(e); } return null; }
From source file:com.precioustech.fxtrading.oanda.restapi.account.transaction.OandaTransactionDataProviderService.java
@Override public List<Transaction<Long, Long, String>> getTransactionsGreaterThanId(Long minTransactionId, Long accountId) {/*from w ww . j a va 2s.co m*/ Preconditions.checkNotNull(minTransactionId); Preconditions.checkNotNull(accountId); CloseableHttpClient httpClient = getHttpClient(); List<Transaction<Long, Long, String>> allTransactions = Lists.newArrayList(); try { HttpUriRequest httpGet = new HttpGet(getAccountMinTransactionUrl(minTransactionId, accountId)); httpGet.setHeader(authHeader); httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER); LOG.info(TradingUtils.executingRequestMsg(httpGet)); HttpResponse httpResponse = httpClient.execute(httpGet); String strResp = TradingUtils.responseToString(httpResponse); if (strResp != StringUtils.EMPTY) { Object obj = JSONValue.parse(strResp); JSONObject jsonResp = (JSONObject) obj; JSONArray transactionsJson = (JSONArray) jsonResp.get(TRANSACTIONS); for (Object o : transactionsJson) { try { JSONObject transactionJson = (JSONObject) o; final String type = transactionJson.get(OandaJsonKeys.type).toString(); Event transactionEvent = OandaUtils.toOandaTransactionType(type); Transaction<Long, Long, String> transaction = null; if (transactionEvent instanceof TradeEvents) { transaction = handleTradeTransactionEvent((TradeEvents) transactionEvent, transactionJson); } else if (transactionEvent instanceof AccountEvents) { transaction = handleAccountTransactionEvent((AccountEvents) transactionEvent, transactionJson); } else if (transactionEvent instanceof OrderEvents) { transaction = handleOrderTransactionEvent((OrderEvents) transactionEvent, transactionJson); } if (transaction != null) { allTransactions.add(transaction); } } catch (Exception e) { LOG.error("error encountered whilst parsing:" + o, e); } } } else { TradingUtils.printErrorMsg(httpResponse); } } catch (Exception e) { LOG.error("error encountered->", e); } return allTransactions; }
From source file:com.precioustech.fxtrading.oanda.restapi.order.OandaOrderManagementProvider.java
private Collection<Order<String, Long>> pendingOrdersForAccount(Long accountId, TradeableInstrument<String> instrument) { Collection<Order<String, Long>> pendingOrders = Lists.newArrayList(); CloseableHttpClient httpClient = getHttpClient(); try {//from w w w . j a va 2s .c om HttpUriRequest httpGet = new HttpGet(this.url + OandaConstants.ACCOUNTS_RESOURCE + TradingConstants.FWD_SLASH + accountId + ordersResource + (instrument != null ? "?instrument=" + instrument.getInstrument() : StringUtils.EMPTY)); httpGet.setHeader(this.authHeader); httpGet.setHeader(OandaConstants.UNIX_DATETIME_HEADER); LOG.info(TradingUtils.executingRequestMsg(httpGet)); HttpResponse resp = httpClient.execute(httpGet); String strResp = TradingUtils.responseToString(resp); if (strResp != StringUtils.EMPTY) { Object obj = JSONValue.parse(strResp); JSONObject jsonResp = (JSONObject) obj; JSONArray accountOrders = (JSONArray) jsonResp.get(orders); for (Object o : accountOrders) { JSONObject order = (JSONObject) o; Order<String, Long> pendingOrder = parseOrder(order); pendingOrders.add(pendingOrder); } } else { TradingUtils.printErrorMsg(resp); } } catch (Exception e) { LOG.error(String.format( "error encountered whilst fetching pending orders for account %d and instrument %s", accountId, instrument.getInstrument()), e); } finally { TradingUtils.closeSilently(httpClient); } return pendingOrders; }
From source file:com.okta.sdk.framework.ApiClient.java
/** * Sets the authorization header to Okta's custom SWSS + API_TOKEN format. * * @param httpUriRequest {@link HttpUriRequest} Current HTTP URI request object. * @throws IOException If an input or output exception occurred. *//*w w w . j av a 2 s. c o m*/ protected void setTokenHeader(HttpUriRequest httpUriRequest) throws IOException { Header authHeader = new BasicHeader("Authorization", String.format("SSWS %s", this.token)); httpUriRequest.setHeader(authHeader); }
From source file:eu.prestoprime.p4gui.connection.P4HttpClient.java
public HttpResponse executeRequest(HttpRequestBase request) throws IOException { // set userID request.setHeader(new BasicHeader("userID", this.userID)); // disable redirect handling HttpParams params = new BasicHttpParams(); params.setParameter(ClientPNames.HANDLE_REDIRECTS, false); request.setParams(params);/* ww w . j a v a 2 s .co m*/ // execute request HttpResponse response = super.execute(request); // check redirect if (redirectCodes.contains(response.getStatusLine().getStatusCode())) { logger.debug("Redirecting..."); // get newURL String newURL = response.getFirstHeader("Location").getValue(); // create newRequest try { HttpUriRequest newRequest = request.getClass().getDeclaredConstructor(String.class) .newInstance(newURL); // copy entity if (request instanceof HttpEntityEnclosingRequestBase) { HttpEntity entity = ((HttpEntityEnclosingRequestBase) request).getEntity(); if (entity != null) { logger.debug("Cloning entity..."); ((HttpEntityEnclosingRequestBase) newRequest).setEntity(entity); } } // set userID newRequest.setHeader(new BasicHeader("userID", this.userID)); // retry response = new P4HttpClient(userID).execute(newRequest); } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) { e.printStackTrace(); } } return response; }