How to get synchronous HTTP response? - windows-phone-7

I write a class to handle the request to web. And it has a method which is using WebClient actually to do the main job. When the DownloadStringCompleted method has been done, I want to return the value of the response.
I want to use that like this:
// the pubTimeLine() method returns the value
// of the request to the web using WebClient
textBlock1.DataContext = wp.pubTimeLine(url);
How to make it? Or how to get the synchronous response of HTTP request?

You should never make synchronous network calls, it will freeze up your UI (and therefore your phone) which is a very bad user experience.
Instead do it asynchronously, something like:
wp.pubTimeLine(url, result => textBlock1.DataContext = result);
Where the second parameter is a lambda expression containing the callback that is called when the pubTimeLine method is done executing asynchronously.

Related

How to access request body in cy.route response callback?

I have built something that can capture network requests and save them to a file. Currently I am now trying to build the second part which should return these captured requests, but running into some difficulties. Sometimes I have multiple requests going to a single method/url combination, and I need to return a different response depending on the request body. The problem I am facing is illustrated in the example below:
cy.route({
url: 'api.example.com/**',
method: myMethod,
response: routeData => {
// I can set the response here
// But I don't have access to the request body
},
onRequest: xhr => {
// I can access the request body here
// But I am not supposed/able to set the response
},
})
If I understand the API docs correctly, I am supposed to set the response in the response callback. However, in that callback I do not seem to have access to the XHR object from which I could read the request body.
Is there a way to access the request body in the response callback?
Or, alternatively, is there a way to set the response from the onRequest callback?
Update: just saw this post which mentions a body property which can be added to the cy.route options object. I don't see this in the cypress route docs so I don't know if this is even a valid option, and I also wouldn't know if making multiple calls to cy.route with an identical method and url, but a different body would produce the correct results. If this was of any use, I would have hoped to have seen some branching logic based on a body property somewhere in this file, so I am not super hopeful.
Cypress v6 comes with the cy.intercept API. Using that is much more convenient than using cy.server and cy.route.

Partial markup via Tapestry block

I'm using Tapestry 5.3.
In a Tapestry component, I'm trying to implement an event handler that creates responses to AJAX requests.
I know that I can call MarkupWriterFactory#newPartialMarkupWriter to get an instance of MarkupWriter and then generate the response using this instance. The built-in mixin Autocomplete generates responses to AJAX requests in this way. Here is a trivial example:
#Inject
private MarkupWriterFactory markupWriterFactory;
#OnEvent("myevent")
Object generatePartialMarkup() {
final ContentType contentType = new ContentType("text/html");
final MarkupWriter markupWriter = markupWriterFactory.newPartialMarkupWriter(contentType);
markupWriter.element("hr");
markupWriter.end();
return new TextStreamResponse(contentType.toString(), markupWriter.toString());
}
However, this is a clumsy solution for a partial HTML that is much more complex than just a hr element. I wonder if there is a chance to create such response using a Tapestry block rather than generating the response "manually" via MarkupWriter. If I just return an instance of block from the event handler, an exception is thrown when the event is triggered.
Thanks.
Looks like you're making life difficult for yourself. From what I can see you can solve this with an eventlink, a zone and an event handler which returns a Block. See here for more info.
Not entirely sure what you're doing but if you need to render a block as a string, see the capture component here

What should I name the actions for ajax requests?

Let's say I have an action:
def result = Action { Ok(views.html.home.result()) }
From result view I want to send ajax requests to the server. What's the standard (if any) way to name the actions that receive such the ajax requests? It might be something like:
def getResultAjax(param1: Int) = //....
To my mind, it look clumsy.
Your ideas?
There's no such convention in Play, anyway action's name should rather contain info about returned data i.e. listOfBooks then just getResult.
On the other hand when there's a lot of different methods (some common, other for ajax requests) it can be cleaner if you'll use ajaxListOfBooks or create BooksAjax controller to handle AJAX request only.
BTW: Purists would say that the requests REST's HTTP methods should also be taken into account, and then action names can be simplified, pseudo routes:
GET /ajax/books Books.list
GET /ajax/books/:id Books.get(id)
PUT /ajax/books Books.create
POST /ajax/books/:id Books.update(id)

Stub repeated method calls for rake test

For my project I use rake test for testing my libs. For example, I've got a method, like connection.users.add_users(options)
json_response = check_users(options)
batch = nil
Timeout::timeout(30) do
begin
sleep 1
batch = connection.batches.find(json_response["batch_id"])
end while batch.state !="completed"
end
connection.users.add_users(batch.target_id, options)
So, first I make an HTTP request to my service, then I get the response (batch_id), loop until the batch is finished, then make another request and return the response.
Usually, in specs I do
let(:connection){setup_test_connection('{"batch_id": 344235}', '202')}
Which will stub connection's response, but in the case of this method it stubs only the first call and then tries to make a real request to my service and so I get an error (timeout because the service is actually down at that time).
Is there any way to stub every possible call of connection's class methods?
So i found out.
I should have used stubs to fake inside requests like this:
connection.servers.stubs(:schedule_create).returns({"batch_id" => 235234})

ajax response for node file

so still a newb to nodeJS and back end code in general. I need some help with ajax and node. For instance I have a function that goes like this
function random(response) {
var objToJson = {...};
response.write(objToJson);
response.end();
}
if instead of writing I want to pass this json object to another function as a response of an ajax call made to it how would that be?
Thanks for you help!
Node.js allows you to easy manipulate HTTP request and response objects.
Ajax still sends a HTTP request object and the Ajax onsuccess callback manipulates a HTTP response object.
Writing an object to a response for an ajax request allows your ajax success handler to manipulate that data.
There are abstraction libraries for RPC like now
It sounds like you want to return a javascript object to work with in your client-side code. While that's not possible directly (you can't send an object directly over HTTP; you're always serializing/deserialing in some fashion), you can certainly return a JSON payload and easily convert that to an in-memory javascript object. If that's what you're doing, you should set the response content type to application/json.
response.setHeader("Content-Type", "application/json");
If you're writing "pure" javascript (no framework wrapping XmlHttpRequest), you'll need to eval() the responseText to convert it to an object. If you're using something like jQuery, it will do that work for you (assuming you set the content type as suggested above).

Resources