How can I use the return from a axios http request in nest js - axios

this return an Observable, and i want to iterate over the result
how can I do that?

I used firstDataValue instead of PiPe.
So the error was that the API was sending me back the data as a string, and I wanted the iterate over the result and I couldn't. So I use JSON.parse(data).
The result was:
let data=firstValueFrom(await this.httpService.get("url")).data;
let dataParsed=JSON.Parse(data);//this was the error cause the api was sending strign
return dataParsed[0]

Related

How to handle a 404 response with spring-cloud-function and AWS

As part of a POC, I’m using spring-cloud-function with spring-cloud-function-adapter-aws to expose a function behind an AWS ALB, that looks up a business object and returns it. However, in the event of a missing object I’m struggling to understand which function signature to use that allows me to return a 404 status code with an empty payload.
I’ve tried a few options, such as Function<APIGatewayProxyRequestEvent, Owner> and Function<APIGatewayProxyRequestEvent, Message<Owner>>. The former doesn’t let you control statusCode and headers that are returned, and the latter doesn’t let you return a Message with a null payload.
I’ve landed on Function<APIGatewayProxyRequestEvent, Message<String>> getOwnerById() which allows me to specify a statusCode and an empty string as the body, but I’m wondering if there’s a more idiomatic approach to this in spring-cloud-function that allows me to return an empty body?
Ideally, I’d just like to return a APIGatewayProxyResponseEvent, which I’d have full control of and would bypass all post-function response conversion.
I have my solution in GitHub, with the function and a test which reproduces the issue
https://github.com/foyst/spring-petclinic-rest-serverless/blob/1-spring-cloud-function/src/main/java/org/springframework/samples/petclinic/lambda/LambdaConfig.java
https://github.com/foyst/spring-petclinic-rest-serverless/blob/1-spring-cloud-function/src/test/java/org/springframework/samples/petclinic/lambda/LambdaConfigTests.java
Thanks in advance!

Grpc-gateway make strange wrapping of com.google.protobuf.wrappers.StringValue result

I have GRPC service with the following function:
rpc StreamMessages(StreamMessagesRequest) returns (stream google.protobuf.StringValue) {
option (google.api.http) = {
post: "/messages:stream"body: "*"
};
}
With grpc-gateway behind it.
Once I have collection of 3 strings: "msg1", "msg2", "msg3" - wrapping every one as com.google.protobuf.wrappers.StringValue and returning as stream.
On GRPC side everything fine, but when I'm trying to execute REST request via gateway the issue happens:
According to documentation, Json representation of google.protobuf.StringValue is just JsonString, so expected streaming result is:
"msg1"
"msg2"
"msg3"
But it returns unexpected format instead:
{"result":"msg1"}
{"result":"msg2"}
{"result":"msg3"}
Question: How can I cause gateway to return the expected?
In order to obtain what you're looking for you need to use the protobuf specific package for the json marshaling. Instead of the standard one, use this one: google.golang.org/protobuf/encoding/protojson.
The interface is the same, but it correctly marshals StringValues to strings when an actual value is provided and the fields get ignored if the StringValue pointer is nil.

Check type of generic Combine Publisher Output Swift

I have a function which access data from my server. The server can either return a JSON object of type Player:
{"id":"6B38EF76-6BBC-4423-BCB6-FD9F9B5E7A6F","ign":"Shard","region":"eu","rating":1000}
or of type Standard Response:
{"statusCode":4,"description":"Invalid Arguments"}
I have a function with the following pattern:
fetch<T: Decodable>(from endpoint: Endpoint) -> AnyPublisher<T, DatabaseError>
This function access the server via a data task publisher and decodes the result into a generic T using combine's built in decode operator:
.decode(type: T.self, decoder: JSONDecoder())
I have the two types of response I showed above modelled as structs with the relevant coding keys etc and this part of the code works fine.
I use the fetch function inside a different function which passes it the relevant endpoint
func getPlayerDetails(of id: UUID) -> AnyPublisher<Player, DatabaseError> {
return fetch(from: .getDetails(of: id, isTeam: false))
}
As you can see this function returns the publisher with Player as the output. My question is how can I check the generic output of fetch(from:) and if it is a player object, pass that through, and if not return my custom error DatabaseError?
Thanks
Okay so I figured out how I to solve my problem, this doesn't really answer the original question however this link does to the most part solve it.
The solution I used in the end was to check the status code of my data task before anything else. My server side code was configured such that the status code would be 200 for when the first JSON was sent, and 500 if the second JSON was sent. This meant I could simply decode the second JSON before if the status code showed that it would be sent.
Thanks for your help

Protractor: Extracting URL value from an element and using URL to open new browser instance

I am trying to do following:
get element(div in this case) containing a URL for eg.
`ele = "www.xyz.com".
Use getAttribute('value') or getText() to
grep URL
Use this URL to fork new instance of browser and GET the
URL
newBrowser = browser.forkNewDriverInstance();
ele.getAttribute('value').then(function(val){
newBrowser.get(val);
});
and I am getting following error:
RangeError: Maximum call stack size exceeded
Second method I tried was without promise and got the error saying that url should be string and not object.
As in:
var url = ele.getText();
newBrowser.get(url);
Is there a way to convert the object returned by getText() into a string and store into variable so that it can be used in some other place.
In your second appraoch, ele.getText() will give you a promise that needs to be resolved. You can resolve the promise on the second approach by using something like this-
ele.getText().then(function(url){
newBrowser.get(url);
})
If this does not work, try printing url with console.log(url). I think it is an object array and you need to get the item you need by referring to the index like url[0] or url[1]. try using console log to print all these values.

How to make ajax call in extjs and display the json value inside div?

I am new in extjs. I need to know how to make ajax call in extjs and display the json values in inside div. I don't need to use grid..
In ExtJS, you will have to use the Ext.Ajax class to make ajax calls to a remote server. Following is a typical code showing how to do it:
Ext.Ajax.request({
url: 'ajax_demo/sample.json',
success: function(response, opts) {
var obj = Ext.decode(response.responseText);
console.dir(obj);
},
failure: function(response, opts) {
console.log('server-side failure with status code ' + response.status);
}
});
In case of HTTP success (200 OK), the control will go inside the success callback and the first things that we have to do is decode the response.responseText which will give you the JSON response coming from the back-end data source.
Once you have code the JSON, you are free to format it and add it to any element (say to a div in your case). In case you want to format the JSON data nicely before adding, you may do that using Template/XTemplate.
I have used something like this.
$.getJSON('somepathtoserver/somefile.php?callback?', variable,function(res){
});
In the somefile.php, I have a callback function that processes and return the value to the js function.
like this:
{
echo $_GET['callback']. '(' . "{'someValue' : $calculatedVariable}" . ')';
}
This is tricky, but very useful when trying to ajax from one server to a different server, which is the reason I would use JSON here and not just a straight AJAX request.