I know there's a lot of tutorials over there about how to use RxJs/Observables.
I need some very straightforward example in order to see how to use Observables with http angular2 service using map, subscribe and catch methods.
Up to now, I've able to generate a service in order to get communication with a REST endpoint:
@Injectable()
export class UsersApi {
protected basePath = 'http://localhost:8082/commty/cmng';
constructor(protected http: Http) {
}
public create(user: string, passwd: string, extraHttpRequestParams?: any): Observable<{}> {
return this.createWithHttpInfo(user, passwd, extraHttpRequestParams)
.map((response: Response) => {
if (response.status === 204) {
return undefined;
} else {
return response.json();
}
});
}
public createWithHttpInfo(user: string, passwd: string, extraHttpRequestParams?: any): Observable<Response> {
const path = this.basePath + `/users`;
let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
// verify required parameter 'user' is not null or undefined
if (user === null || user === undefined) {
throw new Error('Required parameter user was null or undefined when calling create.');
}
// verify required parameter 'passwd' is not null or undefined
if (passwd === null || passwd === undefined) {
throw new Error('Required parameter passwd was null or undefined when calling create.');
}
// to determine the Content-Type header
let consumes: string[] = [
];
// to determine the Accept header
let produces: string[] = [
'application/json'
];
let requestOptions: RequestOptionsArgs = new RequestOptions({
method: RequestMethod.Put,
headers: headers,
search: queryParameters
});
// https://github.com/swagger-api/swagger-codegen/issues/4037
if (extraHttpRequestParams) {
requestOptions = this.extendObj(requestOptions, extraHttpRequestParams);
}
return this.http.request(path, requestOptions);
}
I've used map method but I don't quite understand what it stands for.
How would I need to use UserApi.create(user, password) method?
EDIT
Since http.get(...) method isn't going to be performed without a subscription, I figure out I need any stuff like:
userApi.create('user', 'password').subscribe(...)
Could somebody fill up subscribe method?