Issue
Airflow patch rest api is not working for JAVA version 11. Here is the code snippet.
Map<String, Object> params = new LinkedHashMap<>();
params.put("state", "failed");
GoogleCredentials googleCredentials = null;
try {
googleCredentials = GoogleCredentials.getApplicationDefault();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String url = "airflow url";
HttpCredentialsAdapter credentialsAdapter = new HttpCredentialsAdapter(googleCredentials);
HttpRequestFactory requestFactory = new ApacheHttpTransport().createRequestFactory(credentialsAdapter);
HttpRequest request=null;
JsonFactory jsonFactory = new JacksonFactory();
HttpContent content = new JsonHttpContent(jsonFactory, params);
try {
request = requestFactory.buildPatchRequest((new GenericUrl(url)), content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonObjectParser parser = new JsonObjectParser(GsonFactory.getDefaultInstance());
request.setParser(parser);
HttpHeaders httpHeader = new HttpHeaders();
httpHeader.setContentType("application/json");
httpHeader.setAccept("application/json");
request.setHeaders(httpHeader);
HttpResponse response = null;
try {
response = request.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(response.parseAsString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The google cloud composer is trying the redirect the airflow_url to service logic(https://cloud_url
/_signin?continue=?airflow_url) where i am getting the below error
com.google.api.client.http.HttpResponseException: 405 Method Not Allowed PATCH
the same request with python is working fine(https://cloud.google.com/composer/docs/composer-2/access-airflow-api#make_calls_to_airflow_rest_api)
(PATCH) Airflow REST API(https://airflow.apache.org/docs/apache-airflow/stable/stable-rest-api-ref.html#operation/update_dag_run_state)
url: https://airflow.apache.org/api/v1/dags/{dag_id}/dagRuns/{dag_run_id}
body: {
"state": "success"
}
'Content type': application/json
Solution
params.put("state", "failed");
GoogleCredentials googleCredentials = null;
try {
googleCredentials = GoogleCredentials.getApplicationDefault();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String url = "airflow url";
HttpCredentialsAdapter credentialsAdapter = new HttpCredentialsAdapter(googleCredentials);
HttpRequestFactory requestFactory = new ApacheHttpTransport().createRequestFactory(credentialsAdapter);
HttpRequest request=null;
JsonFactory jsonFactory = new JacksonFactory();
HttpContent content = new JsonHttpContent(jsonFactory, params);
try {
request = requestFactory.buildPatchRequest((new GenericUrl(url)), content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpResponse response = null;
try {
response = request.execute();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
System.out.println(response.parseAsString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Answered By - jaddu nikhileswararao
Answer Checked By - Mary Flores (JavaFixing Volunteer)