Reputation: 21
We are assigned to migrate few Lambdas from aws to GCP. I am finding difficulties for the below scenario.
in aws
public class Handler implements RequestHandler<GetVaccinationPlanRequest, VaccinationPlanResponse>{
public vaccinationPlanResponse handleRequest(GetVaccinationPlanRequest req, Context cf){
}
}
My question is how I can modify the code in GCP? If I want to use HttpFunction instead of RequestHandler I should not be able to do that as it will not take parameters. Also HttpFunction has a service method but again it will return void, however I need to return vaccinationPlanResponse... How I can write the below code in GCP???
public class Handler implements RequestHandler<GetVaccinationPlanRequest, VaccinationPlanResponse>
Upvotes: 0
Views: 1433
Reputation: 466
I'm not really sure to understand the question, but here you have some documentation that might be helpful for your issue.
Handling HTTP methods in Google Cloud
import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import java.io.BufferedWriter;
import java.io.IOException;
import java.net.HttpURLConnection;
public class HttpMethod implements HttpFunction {
@Override
public void service(HttpRequest request, HttpResponse response)
throws IOException {
BufferedWriter writer = response.getWriter();
switch (request.getMethod()) {
case "GET":
response.setStatusCode(HttpURLConnection.HTTP_OK);
writer.write("Hello world!");
break;
case "PUT":
response.setStatusCode(HttpURLConnection.HTTP_FORBIDDEN);
writer.write("Forbidden!");
break;
default:
response.setStatusCode(HttpURLConnection.HTTP_BAD_METHOD);
writer.write("Something blew up!");
break;
}
}
}
This is the Java example, in the link above you will find many other examples using php, ruby, c#, etc, as well as code examples for parsing http request, terminating it, and much more information about http use in GCP.
In Google Cloud HTTP methods, according to the official documentation your function receives two parameters: HttpRequest and HttpResponse. In the HttpRequest object, you can include all the data you need. The HttpResponse object is the actual response your client will get when the function is triggered. You can modify this second object in order to fit your needs.
Upvotes: 1