Enable API Response Compression (gzip) on ServiceStack 5.8.1 - angular7

We have a ServiceStack 5.8.1 API running in Azure that uses EF Core to run queries against an Azure SQL database that is returning 500,000+ records. Calling the API methods returns a JSON representation of the data down to the client.
The front-end client also running in Azure is an Angular 7.x SPA which is doing HTTP client calls to the API and consuming the returned JSON response.
Is there a way in ServiceStack to enable response compression something like GZIP (not caching - as we want the most recent data on every request) that would send the JSON response back to the Angular client in a compressed format?
If that is possible then we could then look to de-compress the result in the Angular client (if that's possible) so to reduce the amount of data being transferred over the network.

Look at method ToOptimizedResultAsync.
There is also ToOptimizedResultUsingCache if you are wanting responses that were cached.
Also mentioned here Enable gzip/deflate compression
Example:
var response = new SomeViewModel
{
Results = ....
}
return base.Request.ToOptimizedResultAsync(response);

Related

How to pass along Json without parsing in web api?

I have a web api based server. All controller methods accept objects and return objects (parsed from/to json by the framework). This has been working fine.
However, due to a new requirement, I need to create an adapter and place it between clients and the server. The adapter will be responsible for translation of a small subset of client messages before passing them to the server.
I'd like to avoid unnecessary deserialization/serialization from/to json of messages that do not need to be translated. That is, in majority of cases I'd like to pass raw json client requests to the server and raw json server replies to clients.
The controller methods that will do the translation will have 'standard' signatures - no problem here. However, I'm not sure how to forward messages (json strings) from clients to the server (and vice versa) without deserialization and serialization.
Is this possible in ApiController?
Thanks.

Java SOAP client very slow

I am building a client for a web service. I didn't want to the client downloading the wsdl everytime and got this answer.
But evaluating the source files of WSServiceDelegate,
URL url = wsdl.getSystemId()==null ? null : JAXWSUtils.getEncodedURL(wsdl.getSystemId());
WSDLModel model = parseWSDL(url, wsdl, serviceClass);
service = model.getService(this.serviceName);
if (service == null)
throw new WebServiceException(
ClientMessages.INVALID_SERVICE_NAME(this.serviceName,
buildNameList(model.getServices().keySet())));
// fill in statically known ports
for (WSDLPort port : service.getPorts())
ports.put(port.getName(), new PortInfo(this, port));
I see that it still parses the wsdl to get the service. How can I get around that. I provided the endpoint url using the context.
I need the client to be as fast and as small as possible, adding a huge wsdl in there is worst than downloading the wsdl.
For the operations you are interested in, you can build your own SOAP messages based on the wsdl's Request/Response messages and the xsd. You can use Jaxb tools to convert from XSD to Java classes. You then need to make post calls using Http Clients (like Spring RestTemplate) to post the POST body, soap based, to the endpoint address. This will make your calls faster but you have to code more in order to benefit.

Caching http responses in iOS

I'm starting an iOS app that consume a Restful API.
I have control over that API and I'm confusing with the caching policies.
To begin, I only need caching a concrete resoruce, but the problem is that resource can change when I insert new record in the database.
Then, how can I tell to the application "Hey! Make the request only if there have been changes and if not, you get the data from the cache!"
I'm using AFNetworking to make requests..
You'll have to make a decision on either server or client side and build your own protocol.
Example:
You could send the server JSON post request which contains the 'version' of the data you have in the app. On the server-side you will increment the version number each time the data gets refreshed. If the version number does not match at server-side, the server will respond with all new data, else the server responds JSON with 'up to date'
EDIT:
If you are looking for an HTTP response saying that the data is not modified. This is done on server side. You'll have to implement this in the server.

Use of REST with scaffolding actions

I see that Grails 2.3 is using REST for the CRUD actions in scaffolding. While it's a great way to learn how REST works, I am wondering if using REST to communicate inside of a single application stack is very efficient. Doesn't it send the request all the way up to the network layer and back down again instead of going directly from the app server to the database? I am visualizing a "pop fly" as opposed to a "line drive." Am I misunderstanding how this works?
I assume when you say, "using REST for the CRUD actions in scaffolding" you are referring to the basic scaffolding (i.e. generate-all example.Book). The scaffolded controllers are not calling the REST API (https://yourapp/book.json) to retrieve the data, they are still using GORM to access the database, and then using the Respond method to render the data in the appropriate format based on the Request's content-type (XML, JSON, HTML). So,
Typical request-response cycle
Client, typically an HTML page, makes request (GET http://yourapp/books/1)
Grails 'parses' request params (id: 1)
Grails' GORM retrieves data from database and creates object instance
Grails resolves response content type to HTML
Grails "responds" to the request with an HTML response using a view from the views directory
SPA/API call
Client, typically javascript, makes request (GET http://yourapp/books/1.json **)
Grails 'parses' request params (id: 1)
Grails' GORM retrieves data from database and creates object instance
Grails resolves response content type to JSON
Grails "responds" to the request with an JSON response
Client consumes the JSON response and acts accordingly
** content-type can be specified in a number of ways, just used the .json suffix since it is the most transparent. See http://grails.org/doc/2.3.x/guide/single.html#contentNegotiation.
And to answer your question regarding whether it "very efficient". I would argue that it is almost always yes, because your payload tends to be much smaller, since you are only transferring data, not data + formatting (HTML, javascript, css, etc). It also provides a way of separating concerns, allowing the client to focus on state and presentation and the backend to focus on data. Furthermore, it means that you can create multiple clients (mobile, desktop-based, web-based) using the same backend API.

Best wrapper for simultaneous API requests?

I am looking for the easiest, simplest way to access web APIs that return either JSON or XML, with concurrent requests.
For example, I would like to call the twitter search API and return 5 pages of results at the same time (5 requests). The results should ideally be integrated and returned in one array of hashes.
I have about 15 APIs that I will be using, and already have code to access them individually (using simple a NET HTTP request) and parse them, but I need to make these requests concurrent in the easiest way possible. Additionally, any error handling for JSON/XML parsing is a bonus.
I'd recommend Weary.
It handles multiple simultaneous asynchronous requests by spawning a thread for each request, and with it you can write API connectors that are readable and DRY. On top of that it has a built in .parse method which works with JSON or XML responses.

Resources