Ruby 2 intercepting request with Typhoeus - interceptor

I want to change query params before request is hit using typhoeus. What should be right approach? Do we have interceptors in Ruby as that of Java, should I be using something like before_filter as that in Rails or meta-programming in Ruby?
I just checked there is Typhoeus::Request::Before module to hook request. Can anyone help me out how can I implement it?

I have implemented typhoeus before to change query params just before request gets executed as below. Hope it helps someone.
require 'uri'
Typhoeus.before { |request|
uri = URI.parse(request.base_url)
uri.query = [uri.query, "param1=value1"].compact.join('&')
request.base_url = uri.to_s
request
}

Related

How to use sagemaker java API to invoke a endpoint?

I was trying to run this example: tensorflow_abalone_age_predictor_using_layers
, in which abalone_predictor.predict(tensor_proto) is used to call the endpoint and make the prediction. I was trying to use the java API AmazonSageMakerRuntime to achieve the same effect, but I don't know how to specify the body and contentType for the InvokeEndPointRequest. The document is not in detailed abou the format of the request. Greatly appreciate any piece of help!
I have not tried the specific example but the below snippet should help you to invoke the endpoint for predictions
InvokeEndpointRequest invokeEndpointRequest = new InvokeEndpointRequest();
invokeEndpointRequest.setContentType("application/x-image");
ByteBuffer buf = ByteBuffer.wrap(image);
invokeEndpointRequest.setBody(buf);
invokeEndpointRequest.setEndpointName(endpointName);
invokeEndpointRequest.setAccept("application/json");
AmazonSageMakerRuntime amazonSageMaker = AmazonSageMakerRuntimeClientBuilder.defaultClient();
InvokeEndpointResult invokeEndpointResult = amazonSageMaker.invokeEndpoint(invokeEndpointRequest);
I see the example you are trying creates a TensorProto and passes to the endpoint request. You can try to create a TensorProto of your invoke request and set as the body
Just figured I can override the input_fn to convert the request body string to something can be fed to the model, in this case a TensorProto object.

is that possible to send header information in $window.open(url) method

I am downloading the file from the server using API, for that i have to send session details in header, how can i do it using angularjs?. Please help me out.
Thank you in advance for suggestions.
No - It is not possible to send headers in straight way using $window.open
Yes - Its is possible but not straight way, If you've got server-side control then you can set header value in query string and get it parsed from query string on the back-end.
I don't suggest to pass params with window.open.
BUT you can use window.open like this.
var params = {
access_token: 'An access_token',
other_header: 'other_header'
};
//Add authentication headers in URL
var url = [url_generating_pdf, $.param(params)].join('?');
//Open window
window.open(url);
Please check the details info here

Angular.js JSONP callback request issue with Symfony2

My App which has just gone live for some reason worked fine during the testing phase but just broke when published on Google play.
I am submitting information to my server eg. logging in through angular.js json requests which used to work but now, I am getting an invalid callback name error in vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/JsonResponse.php at line 77.
The callback name is not valid.
The issue emanates from the callback string attached by angular.js
&callback=angular.callbacks._0
Even though my URL is constructed like this:
url = serviceBase + q + '?' + obj.serialize(object) + '&callback=JSON_CALLBACK';
This is how I handle response in symfony action
$response = new JsonResponse();
$response->setCallback($callback);
$response->setData($feedback);
return $response;
Is there any way this could be remedied? I tried looking around and googling but there was no definitive answer that was not a hack. Thanks in advance, I hope someone will help me before anyone wakes up and tries to register or logs in

transform query string with ngresource for ransack query

I am using ransack in my Rails backend to make research.
I use Angularjs for my app and I would like to call my Rails controller with ransack formated query params, something like :
/conversations?q[subject_cont]=toto&q[status_eq]=1
I can't find any solution to transform any object to this query :/
I have tried to play with http interceptors but did not get any luck.
I am using ngresource to call my API.
Any idea?
in your controller, you could have something like:
var params = {
'q[subject_cont]': $scope.subjectCont',
'q[status_eq]': $scope. statusEq'
}
$scope.conversions = Conversation.query(params);

How to know if a Kohana request is an internal one?

I'm writing an API using Kohana. Each external request must be signed by the client to be accepted.
However, I also sometime need to do internal requests by building a Request object and calling execute(). In these cases, the signature is unnecessary since I know the request is safe. So I need to know that the request was internal so that I can skip the signature check.
So is there any way to find out if the request was manually created using a Request object?
Can you use the is_initial() method of the request object? Using this method, you can determine if a request is a sub request.
Kohana 3.2 API, Request - is_initial()
It sounds like you could easily solve this issue by setting some sort of static variable your app can check. If it's not FALSE, then you know it's internal.
This is how I ended up doing it: I've overridden the Request object and added a is_server_side property to it. Now, when I create the request, I just set this to true so that I know it's been created server-side:
$request = Request::factory($url);
$request->is_server_side(true);
$response = $request->execute();
Then later in the controller receiving the request:
if ($this->request->is_server_side()) {
// Skip signature check
} else {
// Do signature check
}
And here is the overridden request class in application/classes/request.php:
<?php defined('SYSPATH') or die('No direct script access.');
class Request extends Kohana_Request {
protected $is_server_side_ = false;
public function is_server_side($v = null) {
if ($v === null) return $this->is_server_side_;
$this->is_server_side_ = $v;
}
}
Looking through Request it looks like your new request would be considered an internal request but does not have any special flags it sets to tell you this. Look at 782 to 832 in Kohana_Request...nothing to help you.
With that, I'd suggest extending the Kohana_Request_Internal to add a flag that shows it as internal and pulling that in your app when you need to check if it is internal/all others.
Maybe you are looking for is_external method:
http://kohanaframework.org/3.2/guide/api/Request#is_external
Kohana 3.3 in the controller :
$this->request->is_initial()
http://kohanaframework.org/3.3/guide-api/Request#is_initial

Resources