-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Restapi caller #48
Open
aman9bits
wants to merge
9
commits into
Flipkart:master
Choose a base branch
from
aman9bits:restapi-caller
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Restapi caller #48
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b84a840
Added Rest Api Caller
91b1bff
changed log messages
9c660cd
generalized spring config
793d006
give failure when 2xx not returned by consumer
4980ec6
using defaultMapperImpl instead of DoNothingMapper
5782491
changed zookeeper cluster name
40a8cbc
combined delete and upsert events. Added CAS http client impl.
bfb5657
changed dependecies
75c94ba
changed headerprovider to interfaces
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<artifactId>aesop</artifactId> | ||
<groupId>com.flipkart.aesop</groupId> | ||
<version>1.2.1-SNAPSHOT</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<artifactId>data-layer-api-caller</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jdbc</artifactId> | ||
<version>${org.springframework.version}</version> | ||
</dependency> | ||
<!-- aesop dependencies --> | ||
<dependency> | ||
<groupId>com.flipkart.aesop</groupId> | ||
<artifactId>runtime-client-cluster</artifactId> | ||
<version>1.2.1-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.flipkart.aesop</groupId> | ||
<artifactId>client-event-consumer</artifactId> | ||
<version>1.2.1-SNAPSHOT</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.2.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>flipkart.scm</groupId> | ||
<artifactId>java-cas-client</artifactId> | ||
<version>0.0.17.4-auth</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.ning</groupId> | ||
<artifactId>async-http-client</artifactId> | ||
<version>1.8.15</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.flipkart.kloud.authn</groupId> | ||
<artifactId>client</artifactId> | ||
<version>15.07.17.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.flipkart.kloud.authn</groupId> | ||
<artifactId>dropwizard8-relying-party</artifactId> | ||
<version>15.07.17.1</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.ws.rs</groupId> | ||
<artifactId>javax.ws.rs-api</artifactId> | ||
<version>2.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>flipkart.platform</groupId> | ||
<artifactId>prometheus</artifactId> | ||
<version>2.3.2</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
73 changes: 73 additions & 0 deletions
73
...er-api-caller/src/main/java/com/flipkart/aesop/apicallerdatalayer/ApiCallerDataLayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.flipkart.aesop.apicallerdatalayer; | ||
|
||
import com.flipkart.aesop.apicallerdatalayer.client.HttpPostClient; | ||
import com.flipkart.aesop.apicallerdatalayer.headers.ContextualHeaderProvider; | ||
import com.flipkart.aesop.apicallerdatalayer.headers.StaticHeaderProvider; | ||
import com.flipkart.aesop.event.AbstractEvent; | ||
import com.flipkart.aesop.processor.DestinationEventProcessor; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.linkedin.databus.client.pub.ConsumerCallbackResult; | ||
import org.trpr.platform.core.impl.logging.LogFactory; | ||
import org.trpr.platform.core.spi.logging.Logger; | ||
|
||
import javax.naming.OperationNotSupportedException; | ||
import javax.ws.rs.core.Response; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by aman.gupta on 08/12/15. | ||
*/ | ||
public class ApiCallerDataLayer implements DestinationEventProcessor{ | ||
private String url; | ||
private StaticHeaderProvider staticHeaderProvider; | ||
private ContextualHeaderProvider contextualHeaderProvider; | ||
private HttpPostClient httpPostClient; | ||
private static final Logger LOGGER = LogFactory.getLogger(ApiCallerDataLayer.class); | ||
|
||
public void setStaticHeaderProvider(StaticHeaderProvider staticHeaderProvider) { | ||
this.staticHeaderProvider = staticHeaderProvider; | ||
} | ||
|
||
public void setContextualHeaderProvider(ContextualHeaderProvider contextualHeaderProvider) { | ||
this.contextualHeaderProvider = contextualHeaderProvider; | ||
} | ||
|
||
public void setHttpPostClient(HttpPostClient httpPostClient) { | ||
this.httpPostClient = httpPostClient; | ||
} | ||
|
||
public void setUrl(String url) { | ||
this.url = url; | ||
} | ||
|
||
|
||
@Override | ||
public ConsumerCallbackResult processDestinationEvent(AbstractEvent destinationEvent) throws OperationNotSupportedException { | ||
try { | ||
Map<String, Object> eventMap = destinationEvent.getFieldMapPair(); | ||
Gson gson = new GsonBuilder().serializeNulls().create(); | ||
String payload = gson.toJson(eventMap); | ||
Map<String, String> headers = new HashMap<String, String>(); | ||
headers.putAll(staticHeaderProvider.getHeaders()); | ||
headers.putAll(contextualHeaderProvider.getHeaders(eventMap)); | ||
headers.put("Content-Type", "application/json"); | ||
LOGGER.info("Making a post call to url: " + url + " with payload as: " + payload + " and headers as: " + headers); | ||
Response response = httpPostClient.post(url, payload, headers); | ||
int responseCode = response.getStatus(); | ||
if (responseCode >= 200 && responseCode < 300) { | ||
LOGGER.info("Call successful with response code as " + responseCode + " for payload: " + payload); | ||
return ConsumerCallbackResult.SUCCESS; | ||
} else { | ||
LOGGER.info("Call unsuccessful with response code as " + responseCode + " and message as " + response.readEntity(String.class) + " for payload: " + payload); | ||
return ConsumerCallbackResult.ERROR; | ||
} | ||
}catch(Exception e){ | ||
LOGGER.error("Call unsuccessful with error: ",e); | ||
return ConsumerCallbackResult.ERROR; | ||
} | ||
} | ||
|
||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...api-caller/src/main/java/com/flipkart/aesop/apicallerdatalayer/client/HttpPostClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.flipkart.aesop.apicallerdatalayer.client; | ||
|
||
|
||
import javax.ws.rs.core.Response; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by aman.gupta on 09/12/15. | ||
*/ | ||
public interface HttpPostClient { | ||
//Request request = new Request(url,payload,headers); | ||
//Response response = client.executePost(request); | ||
|
||
public Response post(String url, String payload, Map<String,String> headers); | ||
} |
37 changes: 37 additions & 0 deletions
37
...va/com/flipkart/aesop/apicallerdatalayer/client/implementation/HttpCASPostClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.flipkart.aesop.apicallerdatalayer.client.implementation; | ||
|
||
import com.flipkart.aesop.apicallerdatalayer.client.HttpPostClient; | ||
import com.flipkart.casclient.client.HttpAuthClient; | ||
import com.flipkart.casclient.entity.Request; | ||
import flipkart.platform.cachefarm.Cache; | ||
import org.trpr.platform.core.impl.logging.LogFactory; | ||
import org.trpr.platform.core.spi.logging.Logger; | ||
|
||
import javax.ws.rs.core.Response; | ||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by aman.gupta on 09/12/15. | ||
*/ | ||
public class HttpCASPostClientImpl implements HttpPostClient { | ||
private HttpAuthClient httpAuthClient; | ||
private static final Logger LOGGER = LogFactory.getLogger(HttpCASPostClientImpl.class); | ||
public HttpCASPostClientImpl(String casUrl, String user, String password, boolean enableAuth, Cache cache) { | ||
this.httpAuthClient = new HttpAuthClient(casUrl,user,password,enableAuth,cache); | ||
} | ||
|
||
@Override | ||
public Response post(String url, String payload, Map<String, String> headers) { | ||
Request request = new Request(url,payload,headers); | ||
com.ning.http.client.Response response = httpAuthClient.executePost(request); | ||
String responseBody; | ||
try { | ||
responseBody = response.getResponseBody(); | ||
} catch (IOException e) { | ||
LOGGER.error("Unable to get response body for payload"+payload+" Keeping it blank.",e); | ||
responseBody = ""; | ||
} | ||
return Response.status(response.getStatusCode()).entity(responseBody).build(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../com/flipkart/aesop/apicallerdatalayer/client/implementation/HttpJavaxPostClientImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.flipkart.aesop.apicallerdatalayer.client.implementation; | ||
|
||
|
||
import com.flipkart.aesop.apicallerdatalayer.client.HttpPostClient; | ||
import org.trpr.platform.core.impl.logging.LogFactory; | ||
import org.trpr.platform.core.spi.logging.Logger; | ||
|
||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.Entity; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by aman.gupta on 10/12/15. | ||
*/ | ||
public class HttpJavaxPostClientImpl implements HttpPostClient { | ||
Client client = ClientBuilder.newClient(); | ||
private static final Logger LOGGER = LogFactory.getLogger(HttpJavaxPostClientImpl.class); | ||
@Override | ||
public Response post(String url, String payload, Map<String, String> headers) { | ||
|
||
Response response = client.target(url).request().post(Entity.entity(payload, MediaType.APPLICATION_JSON_TYPE)); | ||
LOGGER.info(response.readEntity(String.class)); | ||
return response; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...src/main/java/com/flipkart/aesop/apicallerdatalayer/headers/ContextualHeaderProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.flipkart.aesop.apicallerdatalayer.headers; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created by aman.gupta on 14/12/15. | ||
*/ | ||
public interface ContextualHeaderProvider { | ||
public Map<String, String> getHeaders(Map<String, Object> event); | ||
} |
10 changes: 10 additions & 0 deletions
10
...ler/src/main/java/com/flipkart/aesop/apicallerdatalayer/headers/StaticHeaderProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.flipkart.aesop.apicallerdatalayer.headers; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created by aman.gupta on 14/12/15. | ||
*/ | ||
public interface StaticHeaderProvider { | ||
public Map<String, String> getHeaders(); | ||
} |
27 changes: 27 additions & 0 deletions
27
...ipkart/aesop/apicallerdatalayer/headers/impl/StringBasedContextualHeaderProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.flipkart.aesop.apicallerdatalayer.headers.impl; | ||
|
||
import com.flipkart.aesop.apicallerdatalayer.headers.ContextualHeaderProvider; | ||
import com.google.common.base.Splitter; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Created by aman.gupta on 14/12/15. | ||
*/ | ||
public class StringBasedContextualHeaderProviderImpl implements ContextualHeaderProvider { | ||
Map<String,String> headers; | ||
|
||
public StringBasedContextualHeaderProviderImpl(String headers) { | ||
this.headers = Splitter.on(",").withKeyValueSeparator("=").split(headers); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getHeaders(Map<String, Object> event) { | ||
Map<String,String> map = new HashMap<String,String>(); | ||
for(Map.Entry<String,String> entry : headers.entrySet()){ | ||
map.put(entry.getKey(),(String)event.get(entry.getValue())); | ||
} | ||
return map; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...m/flipkart/aesop/apicallerdatalayer/headers/impl/StringBasedStaticHeaderProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.flipkart.aesop.apicallerdatalayer.headers.impl; | ||
|
||
import com.flipkart.aesop.apicallerdatalayer.headers.StaticHeaderProvider; | ||
import com.google.common.base.Splitter; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Created by aman.gupta on 14/12/15. | ||
*/ | ||
public class StringBasedStaticHeaderProviderImpl implements StaticHeaderProvider { | ||
Map<String,String> headers; | ||
|
||
public StringBasedStaticHeaderProviderImpl(String headers) { | ||
this.headers = Splitter.on(",").withKeyValueSeparator("=").split(headers); | ||
} | ||
|
||
@Override | ||
public Map<String, String> getHeaders() { | ||
return headers; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null checks on these values. These will be implemented by any client and there will be idiots who will return null if they dont want to send out any custom/static headers.