javascript
stackexchange_通过Spring Social推特StackExchange –第1部分
stackexchange
本文將介紹一個(gè)快速的附帶項(xiàng)目-一個(gè)自動(dòng)從各種Q&A StackExchange網(wǎng)站上發(fā)布熱門問題的機(jī)器人,例如StackOverflow , ServerFault , SuperUser等。我們將為StackExchange API構(gòu)建一個(gè)簡單的客戶端,然后進(jìn)行設(shè)置使用Spring Social與Twitter API的交互-這第一部分將僅關(guān)注StackExchange Client。 此實(shí)現(xiàn)的最初目的不是要成為整個(gè)StackExchange API的完整客戶端-這不在本項(xiàng)目的范圍之內(nèi)。 客戶端存在的唯一原因是,我無法對與正式API的2.x版本兼容的客戶端進(jìn)行罰款。
1. Maven依賴
要使用StackExchange REST API,我們將需要很少的依賴項(xiàng)-本質(zhì)上只是一個(gè)HTTP客戶端-Apache HttpClient可以很好地滿足此目的:
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.2.3</version> </dependency還可以使用Spring RestTemplate與HTTP API進(jìn)行交互,但這會(huì)在項(xiàng)目中引入很多其他與Spring相關(guān)的依賴項(xiàng),這些依賴項(xiàng)不是嚴(yán)格必需的,因此HttpClient可以使事情變得簡單明了。
2.問題客戶
此客戶端的目標(biāo)是使用StackExchange 發(fā)布的/ questions REST服務(wù),而不是為整個(gè)StackExchange API提供通用客戶端–因此,出于本文的目的,我們將僅對此進(jìn)行討論。 使用HTTPClient的實(shí)際HTTP通信相對簡單:
public String questions(int min, String questionsUri) {HttpGet request = null;try {request = new HttpGet(questionsUri);HttpResponse httpResponse = client.execute(request);InputStream entityContentStream = httpResponse.getEntity().getContent();return IOUtils.toString(entityContentStream, Charset.forName('utf-8'));} catch (IOException ex) {throw new IllegalStateException(ex);} finally {if (request != null) {request.releaseConnection();}} }這種簡單的交互非常適合獲取API發(fā)布的原始JSON問題–下一步將處理該JSON。 這里有一個(gè)相關(guān)的細(xì)節(jié)-也就是questionsUri方法參數(shù) -有多個(gè)可以發(fā)布問題的StackExchange API(如官方文檔所建議的那樣),并且此方法必須足夠靈活才能使用所有問題。 例如,它可以使用最簡單的API(通過將questionUri設(shè)置為https://api.stackexchange.com/2.1/questions?site=stackoverflow來返回問題),也可以使用基于https://api.stackexchange.com/的標(biāo)記取而代之的是2.1 / tags / {tags} / faq?site = stackoverflow API,具體取決于客戶端的需求。
對StackExchange API的請求已完全配置有查詢參數(shù),即使對于更復(fù)雜的高級搜索查詢也是如此-沒有任何正文被發(fā)送。 為了構(gòu)造questionsUri ,我們將構(gòu)建一個(gè)基本的流暢的RequestBuilder類,該類將使用 HttpClient庫中的URIBuilder 。 這將確保正確編碼URI,并通常確保最終結(jié)果有效:
public class RequestBuilder {private Map<String, Object> parameters = new HashMap<>();public RequestBuilder add(String paramName, Object paramValue) {this.parameters.put(paramName, paramValue);return this;}public String build() {URIBuilder uriBuilder = new URIBuilder();for (Entry<String, Object> param : this.parameters.entrySet()) {uriBuilder.addParameter(param.getKey(), param.getValue().toString());}return uriBuilder.toString();} }因此,現(xiàn)在,為StackExchange API構(gòu)造一個(gè)有效的URI:
String params = new RequestBuilder().add('order', 'desc').add('sort', 'votes').add('min', min).add('site', site).build(); return 'https://api.stackexchange.com/2.1/questions' + params;3.測試客戶端
客戶端將輸出原始JSON,但是要進(jìn)行測試,我們需要一個(gè)JSON處理庫,特別是Jackson 2 :
<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.1.3</version><scope>test</scope> </dependency>我們將看到的測試將與實(shí)際的StackExchange API交互:
@Test public void whenRequestIsPerformed_thenSuccess()throws ClientProtocolException, IOException {HttpResponse response = questionsApi.questionsAsResponse(50, Site.serverfault);assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); } @Test public void whenRequestIsPerformed_thenOutputIsJson()throws ClientProtocolException, IOException {HttpResponse response = questionsApi.questionsAsResponse(50, Site.serverfault);String contentType = httpResponse.getHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue();assertThat(contentType, containsString('application/json')); } @Test public void whenParsingOutputFromQuestionsApi_thenOutputContainsSomeQuestions()throws ClientProtocolException, IOException {String questionsAsJson = questionsApi.questions(50, Site.serverfault);JsonNode rootNode = new ObjectMapper().readTree(questionsAsJson);ArrayNode questionsArray = (ArrayNode) rootNode.get('items');assertThat(questionsArray.size(), greaterThan(20)); }第一次測試已驗(yàn)證API提供的響應(yīng)確實(shí)為200 OK,因此檢索問題的GET請求實(shí)際上是成功的。 在確?;緱l件之后,我們繼續(xù)使用Content-Type HTTP標(biāo)頭指定的表示形式,該表示形式必須為JSON。 接下來,我們實(shí)際解析JSON并驗(yàn)證輸出中是否確實(shí)存在問題–解析邏輯本身是低級且簡單的,足以滿足測試目的。 請注意,這些請求計(jì)入API指定的速率限制 -出于這個(gè)原因,實(shí)時(shí)測試不包含在標(biāo)準(zhǔn)Maven版本中:
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>2.13</version><configuration><excludes><exclude>**/*LiveTest.java</exclude></excludes></configuration> </plugin>4.下一步
當(dāng)前的Client僅關(guān)注StackExchange API發(fā)布的許多可用類型中的一種資源。 這是因?yàn)樗淖畛跄康氖怯邢薜抹C僅需要使用戶能夠使用StackExchange產(chǎn)品組合中各個(gè)站點(diǎn)的問題 。 因此,可以改進(jìn)客戶端,使其超出此初始用例的范圍,以能夠使用其他類型的API 。 實(shí)現(xiàn)也非常原始 -在使用Questions REST Service之后,它只是將JSON輸出作為字符串返回-而不是從該輸出中輸出任何類型的Questions模型。 因此,下一步可能是將JSON解組到適當(dāng)?shù)挠駾TO中,然后將其返回而不是原始JSON。
5.結(jié)論
本文的目的是演示如何開始與StackExchange API或?qū)嶋H上是基于HTTP的API建立集成。 它涵蓋了如何針對實(shí)時(shí)API編寫集成測試,以及如何確保端到端交互確實(shí)有效。
本文的第二部分將展示如何使用Spring Social庫與Twitter API進(jìn)行交互,以及如何使用我們在此處構(gòu)建的StackExchange Client在新的Twitter帳戶上發(fā)布問題。
我已經(jīng)建立了一些Twitter帳戶,現(xiàn)在每天在Twitter上發(fā)布各種學(xué)科的2個(gè)熱門問題:
- SpringAtSO –每天來自StackOverflow的兩個(gè)最佳Spring問題
- JavaTopSO –每天來自StackOverflow的兩個(gè)最佳Java問題
- AskUbuntuBest –每天來自AskUbuntu的兩個(gè)最佳問題
- BestBash –每天來自所有StackExchange網(wǎng)站的兩個(gè)最佳Bash問題
- ServerFaultBest –每天來自ServerFault的兩個(gè)最佳問題
該StackExchange Client的完整實(shí)現(xiàn)在github上 。
參考: 使用Spring Social 推銷StackExchange –第一部分,來自JCG合作伙伴 Eugen Paraschiv,來自baeldung博客。
翻譯自: https://www.javacodegeeks.com/2013/02/tweeting-stackexchange-with-spring-social-part-1.html
stackexchange
總結(jié)
以上是生活随笔為你收集整理的stackexchange_通过Spring Social推特StackExchange –第1部分的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GWT HTTP请求替代
- 下一篇: camel.js_Camel 2.11