Unable to get Dataflow steps information using Java API - java

I am trying to get the steps & jobMetrics information of an active streaming job using the following code:
val jobMetrics = dataflowClient.projects().jobs().getMetrics(projectId, jobId).execute().getMetrics
val steps = dataflowClient.projects().jobs().get(projectId, jobId).execute().getSteps
But, even though I am able to get the jobMetrics information, the steps is always returning null.
Any pointers on what I am doing wrong?

Apologies I'm Java illiterate but with regards to the details that you need I can point you to which endpoint in Dataflow API to get them. My examples are done by sending HTTP requests to the Dataflow API using curl.
To retrieve steps using Dataflow API use projects.jobs.get endpoint and specify what JobView you want. JOB_VIEW_ALL is required to return job.steps.
Sample curl command used:
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) https://dataflow.googleapis.com/v1b3/projects/my-project/locations/us-central1/jobs/2022-01-13_19_11_56-999999999999?view=JOB_VIEW_ALL
Response snippet with steps:
But if following Dataflow Java API. You need to build your GetJobRequest to include setView() with Enum JobView as the parameter. Using code snippet from getJob() documentation:
try (JobsV1Beta3Client jobsV1Beta3Client = JobsV1Beta3Client.create()) {
GetJobRequest request =
GetJobRequest.newBuilder()
.setProjectId("your-project")
.setJobId("jobId101296568")
.setView(JobView.forNumber(2)) // try 2 as value for JOB_VIEW_ALL as per Enum JobView documentation provided above
.setLocation("us-central1")
.build();
Job response = jobsV1Beta3Client.getJob(request);
}
Code snippet above should return Job object where you can try doing getSteps() and see if it will return something.

Related

HTTP connection to JIRA XRAY Rest API not working

Need help
I am trying to connect to XRAY JIRA using Rest API and want to execute a case but getting 400 error response at step inputStream=new InputStreamReader(con.getInputStream(),"UTF-8")
java.io.IOException: Server returned HTTP response code: 400 for URL:
My Code is below:
*HttpURLConnection con=null;
InputStreamReader inputStream=null;
URL jira_API_URL=new URL("https://jira.abc.com/rest/raven/latest/import/execution");
String encodeCredentials=Base64.getEncoder().encodeToString(
"kkris:testjira#234".getBytes("UTF-8"));
con=(HttpURLConnection)jira_API_URL.openConnection();
con.setRequestMethod("POST"); con.setDoOutput(true);
con.setRequestProperty("Autherization", "Basic "+encodeCredentials);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestProperty("X-Atlassian-Token", "nocheck");
try(OutputStream os=con.getOutputStream()){
byte[] input=json.toString().getBytes("UTF-8");
os.write(input,0,input.length);
}
inputStream=new InputStreamReader(con.getInputStream(),"UTF-8");*
Note :Would like to add that I am able to hit this RestAPI using postman and Restassured & able to execute testcase in XRAYJIRA successfully
Well, first of all we need to clarify if you have Xray on Jira server/datacenter or Xray on Jira cloud, as they are different products and the APIs are also slightly different.
From your example, it seems that you're targetting Xray on Jira server/datacenter, and that you aim to import results using the Xray JSON format and respective endpoint as detailed here.
The endpoint URL in that case should either be
<jira_base_url>/rest/raven/1.0/import/execution or <jira_base_url>/rest/raven/2.0/import/execution
Also, please make sure the Xray JSON content you submit follows this syntax.
Note: you may want to have a look at this repo which contains some sample code for submiting results in Java and other languages, including a proof-of-concept client api.
The response body content may show you clues about what's wrong. You can start by using curl utility first as shown here and then implement the java code.
curl -H "Content-Type: application/json" -X POST -u admin:admin --data #data.json http://yourserver/rest/raven/1.0/import/execution

Hazelcast Rest API returns no content

Running hazelcast (3.4) on linux (rh).
I've created MAP in one instance, added couple records:
HazelcastInstance hz = Hazelcast.newHazelcastInstance();
IMap<String,Event> map = hz.getMap("EVENTS");
I'm able to read map using java client, HazelcastClient, but not
able to see anything when hitting REST interface with curl or browser:
curl -X GET http://localhost:5701/hazelcast/rest/maps/EVENTS/
in fact browser shows response code 204 - no content
looks like it has limited REST interface.
I would expect to receive all elements using url above.
I found that only this works to read data:
curl -X GET http://localhost:5701/hazelcast/rest/maps/EVENTS/some-key-value

Converting Curl to POST api for java

I would like to post some data to a rest api.
The API documentation (See page 38) asks for the following:
curl -u "USERNAME:PASSWORD" -H "Content-type: text/xml" -X "POST"
--data-binary #-
"https://qualysapi.qualys.com/qps/rest/3.0/create/was/webapp/" <
file.xml
Note: “file.xml” contains the request POST data.
Request POST data:
<ServiceRequest>
<data>
<WebApp>
<name><![CDATA[My Web Application]]></name>
<url><![CDATA[http://mywebapp.com]]></url>
</WebApp>
</data>
</ServiceRequest>
I have confirmed that the call works on the command line using curl.
I then began to write a small app in Java and found UniRest.
Thats where the problem starts. I do not know how to convert the curl request into Unirest.
I have this much so far:
Unirest.post("http://apiurl).basicAuth("user","pass").field(name, file).asBinary();
the latter half
.field(name, file).asBinary();
doesnt make sense to me. What is the intent behind giving the file a name. Isn't suppose to retrieve the data from the file?
Furthermore, I would like to avoid writing data to file. How can I create the same xml with UniRest.
If not xml, could I do the same with JSON? The API attached above (appendix C) also accepts JSON. However, how can I nest fields with the builder pattern of the Unirest api
According to the UniRest documentation, it looks like you can write any array of bytes to a field in the request. You just have to encode the string into a byte array.
Unirest.post("http://apiroot")
.field(name, xmlString.getBytes(StandardCharsets.UTF_8))
.asBinary();
Alternatively, you can use any InputStream,
Unirest.post("http://apiroot")
.field(name, new CharSequenceInputStream(xmlString, StandardCharsets.UTF_8))
.asBinary();
Usually data is the body of the request though (not as a field). If you do want to send the data as the request body and not a form field, you should use the body(String body) method instead of the field(String name, Object object) method, for instance:
String data = "<ServiceRequest>... etc...</ServiceRequest>";
Unirest.post("http://apiroot")
.body(xmlString)
.asBinary();

Suddenly getting 405 Method Not Allowed during POST to create folder in box-api

I'm suddenly seeing a problem with code (java) that was working about a week ago using the Box API. I'm getting a 405 Method Not Allowed while doing a POST to create a folder. I have tried to trouble shoot the problem assuming it may have something to do with recent v2 api going live. However, going back to trying the examples in the docs I am also seeing problems. For example, the docs give the following example ...
curl https://api.box.com/2.0/folders -H "Authorization: Bearer MY_V1_AUTH_TOKEN_HERE" -d '{"name":"API Test Create", "parent": {"id": "ID_OF_PARENT_FOLDER_HERE"}}' -X POST
Which does nothing when I test it. No new folder and no output at all. I have tried with different folder IDs (including zero) and and I have tried generating new V1 auth tokens. Still nothing.
From what I understand, the V1 auth tokens should continue to work for a little longer. Is that not correct? Is anyone else seeing this issue?
Here is the existing java code that suddenly now has started giving a 405. It uses apache fluent lib ...
String response = Request.Post(new
StringBuilder(API_BASE_URL).append("/folders/").append(parent_folder_id).toString())
.addHeader("Authorization", API_REQUEST_HEADER)
.bodyString(new StringBuilder("{\"name\":\"")
.append(name).append("\"}").toString(), ContentType.DEFAULT_TEXT)
.execute()
.handleResponse(myResponseHandler);
where API_BASE_URL="https://www.box.com/api/2.0" and API_REQUEST_HEADER="BoxAuth api_key=MY_APP_API_KEY&auth_token=MY_V1_AUTH_TOKEN"
It would be great if there is a quick, even temporary, solution to this issue. Any clues are appreciated.
The Bearer header i.e.
Authorization: Bearer {a bearer token acquired through oauth2}
will only work with bearer tokens retrieved through the OAuth 2 process. This header will not work with auth tokens retrieved through the V1 Auth process. You'll need to use the old header style with V1 auth tokens i.e.
Authorization: BoxAuth api_key={your api key}&auth_token={your v1 auth token}
The Create a New Folder method has changed just a bit; this is indicated in the cURL example you've included. Now you must not include the parent folder ID at the end of the request URL, and you must include the parent folder ID in the request body:
String response = Request.Post(newStringBuilder(API_BASE_URL)
.append("/folders").toString()
.addHeader("Authorization", API_REQUEST_HEADER)
.bodyString(new StringBuilder("{\"name\":\"").append(name)
.append("\", \"parent\": {\"id\": \"").append(parent_folder_id).append("\"}}")
.toString(), ContentType.DEFAULT_TEXT)
.execute().handleResponse(myResponseHandler);
EDIT: While I think the method signature change will address your immediate problem, seanrose is spot on in pointing out that you'll need to transition to OAuth2 for long-term stability.

POST curl request returns syantactic error on Spring tc server

I'm new to Spring, STS, MVC, and web development.
I have a working Spring REST-based web application. I also have a java client which works. I followed these 2 tutorials:
RESTful webservices with Spring
Get started with Spring MVC
What I was able to do is use a java client within the same project and use the RestTemplate postForLocation method. It works! However, now I would like the client to be an iPhone and I don't know how to do that. In the java client, the RestTemplate did a post using the com.project.Transaction class.
Please correct me if I am wrong here. In the XML file, the restTemplate "messageConverter" attribute gets set to jaxbMarshaller which is a Jaxb2Marshaller class with "Transaction" as one of the classes bounded. I don't understand the details of it but I am assuming the RestTemplate takes the Transaction object and marshalls it into XML which gets sent to the server as a POST request.
First question:
Is there any way I can see what the marshalled object (ie: the output) looks like? I'm using STS and please be specific as I am new. For example, maybe the data sent is like <xml ...?
Second question:
I am trying to use curl to make a similar POST request as the java client. This is my curl request:
curl -X POST -H 'Accept:application/xml' -H 'Content-Type: application/xml' http://localhost:8080/BarcodePayment/transactions/ --data "<?xml version="1.0" encoding="UTF-8"?><transaction><amount>3.1</amount><id>5</id><paid>true</id></transaction>"
When I do that, I receive a STATUS 400: syntactically error message.
Third queston:
I would love to be able to understand the details a little bit better. I looked into the source code for RestTemplate from here. In it, the postForLocation method uses HttpEntityRequestCallback which I cannot find any info in Google. I found HttpHeaders in java API doc. However, in RestTemplate, they call getLocation() method which does not exist in the Java API doc under javax.ws.rs.core -> HttpHeaders. How can I understand this stuff better?
I know it's a lot of question. Thanks for the help! Let me know if you need more code snippets and I'll be happy to provide!
Details:
TransactionController
#RequestMapping(method = RequestMethod.POST)
public View addTransaction(#RequestBody Transaction transaction)
{
transactionService.saveTransaction(transaction);
return new RedirectView("/transactionsView/"+ transaction.getId());
}
Your xml is incorrect:
<transaction><amount>3.1</amount><id>5</id><paid>true</id></transaction>
^^^ should be /paid
It turns out I have to use single quotes for the data. After --data "< xml .... ", I change it to ' < xml ... ' and it works.

Categories