Client of web service in Perl - web-services

I am the client - I wish to call methods of a web service.
I have a web service address (.svc suffix) and I have the method's name, return value and their argument.
The service is implemented with WCF (HTML end point). I wish to call those methods by SOAP::Lite. What should I write for the URI, proxy and how exactly do I call the methods?

You set
the proxy to the endpoint and
the uri (or in the most recent version ns) to the namespace in the method definition.
One of the easiest ways to do this is simply to use the WSDL URI and create a SOAP::Schema object with it, like so:
my $schema = SOAP::Schema->new( schema_url => $destination_URL )->parse();
my $services = $schema->services();
And dump those two objects.
You can look for
my $method_def = $service->{ $method_name };
my $uri = $method_def->{namespace};
my $proxy = $method_def->{endpoint}->value();
And use those values, if everything is there.
I had to dig through a lot of SOAP::Lite dumps in order to get my SOAP client architecture working. You should know how to debug and dump Perl objects if you want to shoot all your troubles.
I'll show you an anonymized dump of a service:
$services = {
ServiceName => {
MethodName => {
endpoint => bless( {
_attr => {},
_name => 'location',
_signature => [],
_value => [
# v-- This value you pass to SOAP::Lite->proxy
'http://some.domain.com/WebServices/SOAPEndpoint.asmx'
]
}, 'SOAP::Custom::XML::Data'
),
# v-- This value you pass to uri/default_ns/ns
namespace => 'http://some.domain.com/',
parameters => [ ... ]
...
}
}
};

Related

Virtual Hosting on Next.js app with Apollo GraphQL

I have a webapp made with Next.js and Apollo as show in example with-apollo. I want to serve multiple domains with my webapp (name-based virtual hosting). Unfortunately HttpLink of ApolloClient requires absolute server URL with domain but this makes backend app unable to recognize domain which user really visited. Is there a way to configure HttpLink with a dynamic URL based on real request or use relative URL or anything else?
Either use an Apollo Link to intercept the query and set uri property on the context
const authMiddleware = setContext((operation, { uri }) => {
return refreshToken().then(res => ({
uri: this.getURI()
})
}))
Or intercept the request with Angular's HttpClient interceptor and change the endpoint.
https://github.com/apollographql/apollo-angular/tree/master/packages/apollo-angular-link-http#options
Source: Updating uri of apollo client instance
The NextPageContext object passed to getInitialProps includes the req object when called on the server-side. So you can do something like:
WithApollo.getInitialProps = async ctx => {
const { AppTree, req } = ctx
const linkBaseUrl = req ? req.protocol + '://' + req.get('host') : ''
...
}
You can then pass this base url down to createApolloClient along with the initial state and prepend your HttpLink's url with it. On the client side, this will prepend an empty string (you only need the full URL on the server).

Not redirecting to payment page when payment created

I am trying to integrate laravel-mollie in my website using in the example that they are providing the example. When i'm creating new payment it should redirect me to payment page, But its not showing anything.. here is my code:
public function preparePayment($data, $orderId)
{
$payment = Mollie::api()->payments()->create([
'amount' => [
'currency' => 'EUR',
'value' => '100.00', // You must send the correct number of decimals, thus we enforce the use of strings
],
"description" => "My first API payment",
"redirectUrl" => route('mollie.payment.status'),
'webhookUrl' => route('webhooks.mollie'),
"metadata" => [
"order_id" => $orderId,
],
]);
$payment = Mollie::api()->payments()->get($payment->id);
// redirect customer to Mollie checkout page
return redirect($payment->getCheckoutUrl(), 303);
}
I printed the url. which is showing the link. But not redirecting to the payment page. What did i do wrong! can anyone point me out?
The $payment object looks ok, just double check if your param for the redirectUrl is correct.
Also if I'm not mistaken the redirect to an external URL in Laravel should be something like:
...
return redirect()->away($payment->getCheckoutUrl());

How can I properly get the errors in unit testing laravel as well as disable csrf checks?

I'm trying to test my post method in the controller. The method definition is something like :
public function store(Request $request)
{
$article = new Article;
$article->id = $request->input('article_id');
$article->title = $request->input('title');
$article->body = $request->input('body');
return response(["success"], 200);
}
I've created a test which just stores the data and checks if the response is 200.
Please also show me how can I make this test better new to testing. But I'm getting 404 error I don't know what is the error. How can I display the errors what are the setting I need to configure?
Test:
public function test_post_new_article(){
$article = factory(Article::class)->make();
$this->call('POST', 'article', [
'_token' => csrf_token(),
'article_id' => 6,
'title'=>"hey",
'body' => "this is a body"
])->assertStatus(200);
}
phpunit error:
There was 1 failure:
1) Tests\Unit\ExampleTest::test_post_new_article
Expected status code 200 but received 404.
Failed asserting that false is true.
I'm assuming you defined the route in routes/api.php such that the prefix of your particular route is /api/.
You have to call the full path to the API route:
$this->call('POST', '/api/article', [
'_token' => csrf_token(),
'article_id' => 6,
'title'=>"hey",
'body' => "this is a body"
])->assertStatus(200);
Also, since CSRF should be implemented in your Middleware layer, and it's tedious and silly to add _token to all your test requests, you should probably just disable middleware in your tests:
use Illuminate\Foundation\Testing\WithoutMiddleware;
class MyControllerTest {
use WithoutMiddleware;
... public function testmyUnitTest() { ... }
}

How to extract special characters from route in Zend framework 3

I'm sending a url that has special characters in them.
/contacts?advanceSearch=true&advanceSearchType=rating&advanceSearchValue=A1A+
As you see the variable value of advanceSearchValue is A1A+
But when I retrieve this in controller
$this->params()->fromQuery("advanceSearchValue");
it shows me A1A. It adds space instead of +
This is my route config.
"contacts" => [
"type" => "segment",
"options" => [
"route" => "/contacts[/:action[/:id]]",
"defaults" => [
"controller" => Controller\ContactController::class,
"action" => "index",
],
],
],
This is because + has a special meaning in a URL and Zend knows this and correctly replaces it with a space.
To get a + character into the parsed data you need to URL escape it. This gives the value %2B.
So your full URL should be
/contacts?advanceSearch=true&advanceSearchType=rating&advanceSearchValue=A1A%2B
By the way, what is producing this URL, a web browser should be automatically converting the + character before sending it to the web server?
You need to encode your request-url :
You can encode it by php Or Javascript -
In javascript :
var url= "/contacts?advanceSearch=true&advanceSearchType=rating&advanceSearchValue=A1A+";
url= encodeURI(uri);
In php :
$url = urlencode('/contacts?advanceSearch=true&advanceSearchType=rating&advanceSearchValue=A1A+');
Then use this encoded Url in your ajax.

How to create a user with the Admin Directory api using the google-api-ruby-client?

I've been trying a few combinations but can't seem to come up with something that works. More information about the API I'm asking about can be found here https://developers.google.com/admin-sdk/directory/v1/reference/users/insert . I have a feeling I'm just not setting up the request correctly. The following bit of code is known to work. I use it to set up the client that is able to query all the users.
client = Google::APIClient.new(:application_name => "myapp", :version => "v0.0.0")
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => "https://www.googleapis.com/auth/admin.directory.user",
:issuer => issuer,
:signing_key => key,
:person => user + "#" + domain)
client.authorization.fetch_access_token!
api = client.discovered_api("admin", "directory_v1")
When I try to use the following code
parameters = Hash.new
parameters["password"] = "ThisIsAPassword"
parameters["primaryEmail"] = "tstacct2#" + domain
parameters["name"] = {"givenName" => "Test", "familyName" => "Account2"}
parameters[:api_method] = api.users.insert
response = client.execute(parameters)
I always get back the same error "code": 400, "message": "Invalid Given/Family Name: FamilyName"
I've observed a few things while looking into this particular API. If I print out the parameters for both the list and the insert functions e.g
puts "--- Users List ---"
puts api.users.list.parameters
puts "--- Users Insert ---"
puts api.users.insert.parameters
Only the List actually displays the parameters
--- Users List ---
customer
domain
maxResults
orderBy
pageToken
query
showDeleted
sortOrder
--- Users Insert ---
This leaves me wondering if the ruby client is unable to retrieve the api and therefore unable to submit the request correctly or if I'm just doing something completely wrong.
I'd appreciate any idea's or direction that might help set me on the right path.
Thank you,
James
You need to supply an Users resource in the request body, which is also the reason why you don't see it in the params.
So the request should look like:
# code dealing with client and auth
api = client.discovered_api("admin", "directory_v1")
new_user = api.users.insert.request_schema.new({
'password' => 'aPassword',
'primaryEmail' => 'testAccount#myDomain.mygbiz.com',
'name' => {
'familyName' => 'John',
'givenName' => 'Doe'
}
})
result = client.execute(
:api_method => api.users.insert,
:body_object => new_user
)