Convert Webflux API stream response to List<Object> - reactive-programming

I am very new to reactive programming. I am consuming a webflux API which is returning stream response ( application/stream+json ). My task is to call the API and convert response to List. My code snippet for calling API
ClientResponse res = webClient.method(HttpMethod.GET)
.uri("uri")
.header("Authorization", "Basic " + encoding)
.header("Accept","*/*").exchange().block();
The API response is
{
"name" : "Andrew"
....
}
{
"name" : "Bob"
.....
}
I am trying out few examples like
Flux<String> flux = res.bodyToFlux(String.class);
List<String>> list1 = flux.collectList().block;
But this returns list1 with just a single String of the entire response but my requirement is List of multiple string corresponding to each { } in API response. Can anyone please help here.

Using block() you switch to imperative code. So, here
ClientResponse res = webClient.method(HttpMethod.GET)
.uri("uri")
.header("Authorization", "Basic " + encoding)
.header("Accept","*/*").exchange().block();
you are breaking the reactive stream. You should change it to:
return webClient.method(HttpMethod.GET)
.uri("uri")
.header("Authorization", "Basic " + encoding)
.header("Accept","*/*")
.retrieve()
.bodyToFlux(String.class)
.collectList()
.block();

Flux to List without block()
List<String> list = new ArrayList<>();
webClient.method(HttpMethod.GET)
.uri("uri")
.header("Authorization", "Basic " + encoding)
.header("Accept","*/*")
.retrieve()
.bodyToFlux(String.class)
.collectList().subscribe(list::addAll);

Related

Rest API Post with parameters to get search result

I am in Xamarin Forms and making rest calls to an API, I am new to this. I am wanting to send my search parameters which is:
{ "busObIds": [
"8102508"], "searchText": "John"}
(this the raw JSON format) to the API endpoint:
var client = new RestClient("http://localhost/NameAPI/api/V1/getquicksearchresults");
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "Bearer " + bearer);
I also have an Authorization Bearer i need to apply. I am stumbling through this and not sure how to do this correctly, as I was shown how to do in Postman but currently can not convert that into my project in Visual Studio Xamarin Forms. Help please if you can.
So am I doing this correct?
Do i need to serialize my JSON in model class?
Am i posting or getting? as it says POST but im not confident I am doing right.
Do i post my parameters in the AddHeader?
Anyway thank you in advance.
Update:
var _client = new RestClient("http://localhost/NameAPI/api/V1/getquicksearchresults");
var request = new RestRequest(Method.POST);
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddHeader("Authorization", "Bearer " + bearer);
request.AddParameter("busObIds", "8102508");
request.AddParameter("searchText", "John");
IRestResponse response = _client.Execute(request);
var jsonData = response.Content;
var jsonObj = JsonConvert.DeserializeObject<CiResponse>(jsonData);
this.CiList = new ObservableCollection<CiItem>();
if (null != jsonObj && jsonObj.Types != null && jsonObj.Types.Count > 0)
{
foreach (var item in jsonObj.Types)
this.CIList.Add(new CiItem
{
PublicId = item.PublicId,
SubTitle = item.SubTitle,
Text = item.Text,
Title = item.Title
});
}
When running i have no indication that its incorrect, but no output showing on screen.

Can any body share me java code to make a one Rest api call to IBM BPM Cloud

Can any body share a java client code which makes a Rest calls to IBM Cloud BPM. Basically I want to know how to authenticate IBM Cloud BPM.
I tried the following code but it is not working
String user_info_url="https://ustrial01.bpm.ibmcloud.com/bpm/dev/rest/bpm/wle/v1/user/current?includeInternalMemberships=true&parts=all";
logger.info("user_info_url :" + user_info_url);
HttpClient client = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(user_info_url);
String authData = "rajesh.kohir123#gmail.com" + ":" + "password";
String encoded = new sun.misc.BASE64Encoder().encode(authData .getBytes());
get.setHeader("Content-Type", "application/json");
get.setHeader("Accept", "application/json");
get.setHeader("Authorization", "Basic " + encoded);
HttpResponse cgResponse = client.execute(get);
if(cgResponse.getStatusLine().getStatusCode() != 200) {
logger.info("IBM Rest call failed");
}
if(cgResponse.getStatusLine().getStatusCode() == 200) {
logger.info("IBM Rest call Succeded");
String content = EntityUtils.toString(cgResponse.getEntity());
logger.info(content);
}
Any help is greatly appreciated
I ran your code and just made the changes in URL. It worked. I hope this helps you.
Following is the URL I used to execute an exposed service :
https://vhost031.bpm.ibmcloud.com/bpm/dev/rest/bpm/wle/v1/service/OMS#Greetings
I used the following code to add the parameters :
String parameters = "{'name':'pramod'}";
URIBuilder builder = new URIBuilder("https://vhost031.bpm.ibmcloud.com/bpm/dev/rest/bpm/wle/v1/service/OMS#Greetings");
List nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("action", "start"));
nameValuePairs.add(new BasicNameValuePair("params", parameters));
nameValuePairs.add(new BasicNameValuePair("createTask", "false"));
nameValuePairs.add(new BasicNameValuePair("parts", "all"));
builder.setParameters(nameValuePairs);
HttpGet get = new HttpGet(builder.build());
Download the download.zip form the post.
Look at the SampleBPDProcessTests.java - Line no 103
JSONObject results = bpmClient.runBPD(BPD_ID, PROCESS_APP_ID, bpdArgs);
The actual Java Code for Rest call is available as part of "bpm-rest-client.jar"
Try this concept.
Sample Java code to start a process:
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://ustrial03.bpm.ibmcloud.com:443/bpm/dev/rest/bpm/wle/v1/process?
processAppId=3u092jr02j-djaodaj.u092302c166c1&bpdId=25.jklaklaa-539a-4150-
b63e-9ef94e96e521&action=start")
.put(null)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addHeader("Accept", "application/json")
.addHeader("Connection", "keep-alive")
.addHeader("Authorization", "Basic YXJrYX24223232hQGRlbG9pdHRlLmNvbTpkZWZjb240QA==")
.addHeader("Cache-Control", "no-cache")
.addHeader("Postman-Token", "f46c1525-7a75-954c-9265-bb2b21a57f16")
.build();
Response response = client.newCall(request).execute();
A full explanation of REST integration with BPM Cloud can be found in my answer at:
How to run IBM BPM Rest api call from Post man client

Headers in POST in Grails 3 app are not being sent with rest of service

Using Grails 3.0.9, and grabbing the freshest REST API with this snippet in gradle.build:
compile 'org.grails:grails-datastore-rest-client:4.0.7.RELEASE', {
['commons-codec', 'grails-async', 'grails-core',
'grails-plugin-converters', 'grails-web', 'groovy'].each {
exclude module: it
}
}
I am trying to make the following POST request:
def rest = new RestBuilder(headers:["X-LSS-Env":"devmo"], connectTimeout:10000, readTimeout:20000)
response = rest.post("http://..../..") {
accept "application/json"
contentType "application/json"
json jsonBuilder
}
Now, the POST receiver gets the json okay, give back a response okay, but this is the problem: it receives the headers as an empty map or as null!
So, what is the correct way of passing header data to the POST receiver? This is needed because the environment key X-LSS-Env could have different values, which instructs the receiver to do further routing based on it. Same with the GET request of course.
* UPDATE *
The consumer of my POST requests is actually a Java application, running on Apache Tomcat/8.0.26. The is how the service looks on the other side:
private javax.servlet.http.HttpServletRequest hsr;
#POST
#Path("/na")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public Response postSomething(Ggfp ggfp ){
try {
Enumeration<String> hnames = hsr.getHeaderNames();
int i = 0;
while (hnames.hasMoreElements()) {
String headerName = hnames.nextElement();
System.out.println(++i+ " headerName: " + headerName);
String val = hsr.getHeader(headerName);
System.out.println(" val: " + val);
}
String hval = hsr.getHeader("X-LSS-Env");
return Response.status(Status.OK).entity("X-LSS-Env is " + hval).build();
} catch (Exception e) {
}
}
Calling this service from Postman works, headers are identified. Calling it from the Grails app results into an empty map - like I am sending no headers!
The RestBuilder constructor never liked the way I used (or abused) it. Here is a clean way of achieving what I set out to do, with tryCatch logic if a timeout transpires.
def makePostWsr(serviceUrl, jsonBuilder) {
try {
def rest = new RestBuilder(connectTimeout:connectTimeout, readTimeout:readTimeout)
def response = rest.post("$wsUrl/$serviceUrl") {
header 'X-LSS-Env', 'devmo'
accept "application/json"
contentType "application/json"
json jsonBuilder
}
response
} catch (Exception e) {
println "== problem makePostWsr on $serviceUrl"
null
}
}

BOX API:how to get location attribute in response using https://api.box.com/2.0/files/{fileId}/content for downloading

My code is given below
WebResource webResource1 = cl.resource("https://api.box.com/2.0/files/{fileId}/content");
ClientResponse res1 = webResource1.header("Authorization", "Bearer"+p1.getAccess_token()).get(ClientResponse.class);
String jsonStr1 = res1.getEntity(String.class);
And my response is given below-
{Object-Id=[file_20317568941], Cache-control=[private], Date=[Wed, 24 Sep 2014 12:11:43 GMT], Content-Length=[27], X-Robots-Tag=[noindex, nofollow], Content-Disposition=[attachment;filename="upload.txt";filename*=UTF-8''upload.txt], Accept-Ranges=[bytes, bytes], Connection=[keep-alive], Content-Type=[text/plain; charset=UTF-8], Server=[nginx], X-Content-Type-Options=[nosniff]}
I am getting status code 200, OK; but to get the location attribute I need to have the status code 302 along with the location url (https://dl.boxcloud.com/*).
Without getting location: https://dl.boxcloud.com/* attribute in the response, how can I download file from box api?
last Saturday I got some time to look into your issue. The basic problem is that if you need to get the Location value you need to stop the automatic redirection. Following is the explanation & solutions of your problem:
Quoting Box API docs of Download a File:
If the file is available to be downloaded, the response will be a 302
Found to a URL at dl.boxcloud.com.
From Wikipedia article on HTTP 302:
The HTTP response status code 302 Found is a common way of performing
URL redirection.
An HTTP response with this status code will additionally provide a URL
in the Location header field. The user agent (e.g. a web browser) is
invited by a response with this code to make a second, otherwise
identical, request to the new URL specified in the Location field.
So to get the Location attribute in the response header you need to stop the automatic redirection. Otherwise as per the box doc you will get the raw data of the file instead of the download URL.
Following is a solution implemented using Commons HTTPClient:
private static void getFileDownloadUrl(String fileId, String accessToken) {
try {
String url = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId);
GetMethod getMethod = new GetMethod(url);
getMethod.setFollowRedirects(false);
Header header = new Header();
header.setName("Authorization");
header.setValue("Bearer " + accessToken);
getMethod.addRequestHeader(header);
HttpClient client = new HttpClient();
client.executeMethod(getMethod);
System.out.println("Status Code: " + getMethod.getStatusCode());
System.out.println("Location: " + getMethod.getResponseHeader("Location"));
} catch (Exception cause) {
cause.printStackTrace();
}
}
An alternate solution using java.net.HttpURLConnection:
private static void getFileDownloadUrl(String fileId, String accessToken) {
try {
String serviceURL = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId);
URL url = new URL(serviceURL);
HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection());
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
connection.setRequestMethod("GET");
connection.setInstanceFollowRedirects(false);
connection.connect();
int statusCode = connection.getResponseCode();
System.out.println("Status Code: " + statusCode);
Map<String, List<String>> headerFields = connection.getHeaderFields();
List<String> locations = headerFields.get("Location");
if(locations != null && locations.size() > 0) {
System.out.println("Location: " + locations.get(0));
}
} catch (Exception cause) {
cause.printStackTrace();
}
}
Since Commons HTTPClient is outdated the following solution is based on the Apache HttpComponents:
private static void getFileDownloadUrl(String fileId, String accessToken) {
try {
String url = MessageFormat.format("https://api.box.com/2.0/files/{0}/content", fileId);
CloseableHttpClient client = HttpClientBuilder.create().disableRedirectHandling().build();
HttpGet httpGet = new HttpGet(url);
BasicHeader header = new BasicHeader("Authorization", "Bearer " + accessToken);
httpGet.setHeader(header);
CloseableHttpResponse response = client.execute(httpGet);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Status Code: " + statusCode);
org.apache.http.Header[] headers = response.getHeaders(HttpHeaders.LOCATION);
if(header != null && headers.length > 0) {
System.out.println("Location: " + headers[0]);
}
} catch (Exception cause) {
cause.printStackTrace();
}
}

REST Services - JSON deserialization error: Encountered unexpected character '<'

public async void MakeRequest(string requestUrl)
{
try
{
HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
using (HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception(string.Format(
"Server error(HTTP {0}:{1}.",
response.StatusCode,
response.StatusDescription));
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(RootObject));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
root = (RootObject)objResponse;
if (root == null)
this.Frame.Navigate(typeof(MainPage));
}
}
catch (Exception ex)
{
ThrowException(ex);
}
}
Link : http://dev.virtualearth.net/REST/V1/Routes?wp.0=39.920829,32.853883&wp.1=39.877666,32.864728&key=BingMapsKey
Is there anything wrong in my in the codes above?
Just change this line for make the query with JSON and not XML format
So from this ...
string query = "http://dev.virtualearth.net/REST/v1/Locations/" +
queryString + "?output=xml" + " &key=" + BingMapsKey;
To this ...
string query = "http://dev.virtualearth.net/REST/v1/Locations/" +
queryString + "?output=json" + " &key=" + BingMapsKey;
i.e. just change output=xml to output=json
This code is working fine for me once I replace the undefined RootObject in your code with the BingMapsRESTService.Common.JSON.Response type found in the Bing Maps REST Service .NET Libraries that #rbrundritt suggested you use in your other post