Creating a SOAP client with angular 2 [closed] - soap

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a way to send SOAP request to a web service, with a WSDL. Is it possible to do that with Typescript 2 and Angular 2 ?
I've seen tutorials for Angular 1 but they used old angular methodes, like factory or controller.
I would like if it's possible, a new way to do that with TypeScript.
Any ideas ?

What you need is a service that wraps around Http and provides deserialization:
#Injectable()
export class SOAPService{
constructor(private http:Http){}
public get(url:string, options?:RequestOptionsArgs):Observable<any>{
return this.http.get(url, options).map(res => {
let xmlresult = res.text();
let result = //Here you deserialize your xml object
return result;
}
}
}
Then you can use it this way:
#Component({...})
export class ExampleComponent{
constructor(private soap:SOAPService){}
foo():{
this.soap.get('foo/bar').subscribe(...);
}
}
Since I'm not an xml parsing expert, I can't tell you how to deserialize your XML, but you can check MDN for that, you simply have to wrap the serialization/deserialization process in another service and inject it in your SOAPService.

You could use a regular http-request with Angular2's [Http class] (https://angular.io/docs/ts/latest/api/http/index/Http-class.html)
This is an example from their page:
import {Http, HTTP_PROVIDERS} from '#angular/http';
import 'rxjs/add/operator/map'
#Component({
selector: 'http-app',
viewProviders: [HTTP_PROVIDERS],
templateUrl: 'people.html'
})
class PeopleComponent {
constructor(http: Http) {
http.get('people.json')
// Call map on the response observable to get the parsed people object
.map(res => res.json())
// Subscribe to the observable to get the parsed people object and attach it to the
// component
.subscribe(people => this.people = people);
}
}
instead of asking for a json file, you could use a URL instead.

Related

Ionic 4 Angular 7 - passing object/data to another page [duplicate]

This question already has answers here:
Ionic 4. Alternative to NavParams
(8 answers)
Closed 4 years ago.
I would like to pass a JSON object to another page. What I've tried is to pass the JSON string using Angular router ActivatedRoute like this:
this.router.navigate(['details', details]);
and then retrieve it like this:
import { ActivatedRoute } from '#angular/router';
constructor(private activatedRoute: ActivatedRoute) {
}
ngOnInit() {
this.activatedRoute.params.subscribe(extras => {
console.log(extras);
this.JSONObject = extras;
});
}
It is possible to do it this way but what happened was the nested JSON objects becomes inaccessible. It turns into this string:
"[object Object]"
The stringified JSON object is fine and accessible before I pass it. Another problem is that it appends the JSON string to the url so it doesn't look that nice. From what I read as well, it is not a good practice to pass something more than just an id this way.
I am thinking of something like passing objects as intent extras between activities in Android. I've searched the documentations, forums, and previous stackoverflow questions but I didn't found any solution that enables me to achieve this. Is there any other way of passing objects between pages using Angular router in Ionic4?
I solved it using a service with a simple setter and getter just like in this question that I later found:
Ionic 4. Alternative to NavParams
First, I create a service with setter & getter:
import { Injectable } from '#angular/core';
#Injectable({
providedIn: 'root'
})
export class NavExtrasService {
extras: any;
constructor() { }
public setExtras(data){
this.extras = data;
}
public getExtras(){
return this.extras;
}
}
Let's say I'm navigating from page A to page B, in page A:
this.navExtras.setExtras(extras)
this.router.navigateByUrl('page-b');
Then in Page B, I retrieve the extras this way:
this.location = navExtras.getExtras();
It works so far although I'm still not sure if there are better ways to do it..

Using angular2-sails module for realtime communication using sockets

I would like to use sails.io.js with angular5, so I used angular2-sails module. I managed to connect angular to sails but I didn't manage to retrieve the events from sails.js, for example when a new document is created in database. Is there something to configure sails side ? I used this.sailsService.on("user").subscribe(data => console.log("event on user")). The get and post methods are perfectly working. Sails side I put
ioclient: require('socket.io-client')('http://localhost:1337'),
io: require('sails.io.js'),
In config/http.js, instead of
var io = require('sails.io.js')( require('socket.io-client') );
because else sails cannot load
I didn't write anything in config/socket.js
angular2-sails module is deprecated so I used the io variable from sails.io.js using a service :
import {Injectable} from '#angular/core';
function _window(): any {
// return the global native browser window object
return window;
}
#Injectable()
export class SocketService {
get ioSails(): any {
return _window().io;
}
}

Paths/links with unique ID's angular2

I'm creating a quiz making/sharing website using angular2 but am not sure how to share the quizzes. i was thinking of using each of my quizzes identifiers as URLs. The quizzes are made using forms and are saved on a docmentdb as JSON. they have unique ID's to identify them. Any ideas as to how i could do this?
Those URLs must be dynamically created, as new quizzes can be submitted and thereafter accessed.
You can use the same base url for your page with quiz, but differentiate quizzes by path parameter like:
quizsite.com/#/quiz/12 (12 being quiz id)
Inside your component you can read what path parameters are located in your url and their values by accessing ActivateRoute object:
Component
import { Component } from '#angular/core';
import { ActivatedRoute } from '#angular/router';
#Component({
templateUrl: 'quizComponent.html',
})
export class QuizComponent {
constructor(private activatedRoute: ActivatedRoute){}
ngOnInit() {
// Reason for this being a observable is that you can watch
parameter changing (manually in url, or programmatically)
and without any page refresh, read new parameter and change you quiz
this.activatedRoute.params.subscribe(params => {
console.log(params.quizId);
// With this parameter you can make call to your REST API and return
data for that quiz
});
}
}

Xamarin forms Rest Client [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 5 years ago.
Improve this question
Anybody suggest me a good Rest client that i can use for my xamarin forms cross platform application ?
(Android and Windows Phone)
Thanks in advance.
That's a very open question.
Anyway my favorite:
Refit
Updated Removing the other two libraries that even though you can do REST with them they are not considered REST clients but HTTP clients.
You can you Microsoft HTTP Client Libraries.
Then, you define a RestService class that contains a HttpClient instance:
public class RestService : IRestService
{
HttpClient client;
public RestService()
{
client = new HttpClient();
client.MaxResponseContentBufferSize = 256000;
}
// example for GET request
public async Task<List<TodoItem>> RefreshDataAsync()
{
var uri = new Uri(string.Format(Constants.RestUrl, string.Empty));
var response = await client.GetAsync(uri); // make a GET request
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
// handle response here
}
}
}
You should follow this article:
https://developer.xamarin.com/guides/xamarin-forms/cloud-services/consuming/rest/
I do not know if there are simple implementation for this purpose but you have to write your own parsers, senders, receivers according to your needs. I would give you a little example :) First of all i have a base class for my generic restCall
public abstract class BaseDto
{
public string Id {get;set;}
}
than write your business objects like
public class UserDto : BaseDto
{
public string Name {get;set;}
//etc.
}
public class SavedUserDto : BaseDto
{
public int Id {get;set;}
public string Name {get;set;}
//etc.
}
than write a simple http caller
public string Post<T>(string url, T entity) where T : BaseDto
{
//here u will write a HttpClient and send receive json. U can find examples on the net. Of course use Newtonsoft for json convertions
}
Than write a generic method to call this post method, of course you will send a baseDto and receive a baseDto too :)
public K Call<T, K>(
string restApiPath,
T entity) where T : BaseDto where K : BaseDto
{
var response = Post(restApiPath, entity);
//you can handle errors, auth faults etc. here.
return JsonConvert.DeserializeObject<K>(response);
}
than in your code just do
var savedUser = Call<UserDto,SavedUserDto>("127.0.0.1/user/save",new UserDto{Name="John"})
Hope it can give you an idea. Than when you add a new service method on your rest api, you can call it just with a single line of code (of course you have to write new business DTO's - aka data transfer objects:))
Of course all this POST and CALL methods on different classes. Do not forget, a class must do only one thing. So POST method owner class (let's call it HttpCaller) will only send the Dto to the server and get the answer. CALL method owner class (lets call it MyService) will get the resut and process it etc.
You can use HttpClient. It is good. get from nuget System.Http.Net

Angular2 multiple components using one REST call

I am part of a Angular2 application (we use beta3) and the issue is the following:
Usually we have a component that uses some service that uses some rest call and the component displays the data. Great.
However we do have a page with more then 6 components all of them using the same REST call...(the backend returns data for ALL of them) and it doesn't make sense to call 6 times the REST for each component, also it will be weird if we do some client side caching.
Is there something available out of the box ? Or a Pattern to handle such case?
Thanks.
Just do it in a shared service. If you add it only in bootstrap(..., [OtherProviders, HTTP_PROVIDERS, MyService]) each component will get injected the same instance. Store the data in the service and every component can access it
export class MyComponent {
constructor(private dataService:MyService) {
dataService.getData().subscribe(data => { this.data = data; });
}
}
export class MyService {
getData() {
if(!this.data) {
return http.get(...).map(...).subscribe(data => { this.data = data;});
}
return this.data;
}
}
The #Günter's answer really makes sense!
I don't know your code is organized but observable can also be subscribed several times. To do that you need to make them "hot" using the share operator:
export class MyService {
dataObservable:Observable;
initDataObservable() {
this.dataObservable = http.get(...).map(...).share();
}
}
Without using the share operator, corresponding request will executed several times (one per subscribe).
You can notice that the request will be executed once one subscribe method is called on the observable.