io.github.atmaramnaik.journey.http.rest.steps.RestStep Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of journey-http Show documentation
Show all versions of journey-http Show documentation
Http Module For Processing Workflows known as journeys
package io.github.atmaramnaik.journey.http.rest.steps;
import io.github.atmaramnaik.journey.http.rest.steps.core.*;
import io.github.atmaramnaik.journey.http.rest.steps.core.*;
import io.github.atmaramnaik.journey.journey.Step;
import io.github.atmaramnaik.journey.core.data.runtime.Context;
import io.github.atmaramnaik.journey.core.data.value.DeSerializationException;
import io.github.atmaramnaik.journey.core.io.IO;
import io.github.atmaramnaik.journey.template.template.Extractable;
import io.github.atmaramnaik.journey.template.template.Template;
import io.github.atmaramnaik.journey.template.template.text.Text;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.HttpRequest;
import static io.github.atmaramnaik.journey.template.template.Template.*;
import lombok.Getter;
import java.util.HashMap;
public abstract class RestStep implements Step {
protected Text url;
protected HashMap headers=new HashMap<>();
protected HashMap responseHeaders=new HashMap<>();
public RestStep withHeader(String header,Text headerTemplate){
headers.put(header,headerTemplate);
return this;
}
public RestStep withHeader(String header,String value){
headers.put(header,text(string(value)));
return this;
}
@Getter
protected Extractable response=null;
public RestStep(Text url) {
this.url = url;
}
public RestStep capture(Extractable response){
this.response=response;
return this;
}
public RestStep captureHeader(String headerName,String variableName) {
this.responseHeaders.put(headerName,variableName);
return this;
}
protected abstract HttpRequest getHttpRequest(Context context, IO io);
@Override
public void perform(Context context, IO io) {
HttpRequest httpRequest=getHttpRequest(context,io);
for (String key:
headers.keySet()) {
httpRequest.header(key,headers.get(key).process(context,io).serialize());
}
try {
HttpResponse sResponse=httpRequest.asString();
if(response!=null){
try {
T dataObject=response.deserialize(sResponse.getBody());
response.process(context,io,dataObject);
} catch (DeSerializationException e) {
e.printStackTrace();
}
}
HashMap capturedHeaders=new HashMap<>();
for (String header:responseHeaders.keySet()){
capturedHeaders.put(responseHeaders.get(header),sResponse.getHeaders().getFirst(header));
}
context.pour(capturedHeaders);
} catch (UnirestException e) {
throw new RuntimeException("Something went wrong",e);
}
}
public static PostStep post(Text url, Template body){
PostStep postStep=new PostStep<>(url,body);
return postStep;
}
public static PatchStep patch(Text url, Template body){
PatchStep patchStep=new PatchStep<>(url,body);
return patchStep;
}
public static DeleteStep delete(Text url, Template body){
DeleteStep deleteStep=new DeleteStep<>(url,body);
return deleteStep;
}
public static PutStep put(Text url, Template body){
PutStep putStep=new PutStep<>(url,body);
return putStep;
}
public static GetStep get(Text url){
GetStep getStep=new GetStep(url);
return getStep;
}
}