File

projects/typescript-xmlrpc/src/lib/typescript-xmlrpc.service.ts

Index

Properties
Methods

Constructor

constructor(http: HttpClient)
Parameters :
Name Type Optional
http HttpClient No

Methods

configureService
configureService(url: URL)

Call this method before any other to configure the service. Otherwise other methods may behave incorrectly.

Parameters :
Name Type Optional Description
url URL No

The URL of the XMLRPC Service.

Returns : void
Static instanceOfMethodFault
instanceOfMethodFault(object: object)
Parameters :
Name Type Optional
object object No
Returns : MethodFault
Static instanceOfMethodResponse
instanceOfMethodResponse(object: object)
Parameters :
Name Type Optional
object object No
Returns : MethodResponse
Static instanceOfXmlRpcArray
instanceOfXmlRpcArray(object: XmlRpcTypes)
Parameters :
Name Type Optional
object XmlRpcTypes No
Returns : XmlRpcArray
Static instanceOfXmlRpcStruct
instanceOfXmlRpcStruct(object: XmlRpcTypes)
Parameters :
Name Type Optional
object XmlRpcTypes No
Returns : XmlRpcStruct
methodCall
methodCall(method: string, params?: Array<XmlRpcTypes>, encoding?: string)

Makes an XML-RPC call to the server specified by the constructor's options.

Parameters :
Name Type Optional Description
method string No

The method name.

params Array<XmlRpcTypes> Yes

Params to send in the call.

encoding string Yes

The encoding to append to the generated XML document.

Returns : Observable<MethodResponse | MethodFault>

Properties

Private headers
Type : object
Private Readonly http
Type : HttpClient
Private url
Type : URL
import { Injectable } from '@angular/core';

import { HttpClient, HttpHeaders } from '@angular/common/http';
import { serializeMethodCall } from './serializer';
import { Utils } from './utils';
import {
  MethodFault,
  MethodResponse,
  XmlRpcArray,
  XmlRpcStruct,
  XmlRpcTypes,
} from './xmlrpc-types';
import { deserialize } from './deserializer';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

const BODY = 'body';
const TEXT = 'text';

@Injectable({
  providedIn: 'root',
})
export class AngularXmlrpcService {
  private readonly http: HttpClient;
  private url: URL;
  private headers: object;

  static instanceOfMethodResponse(object: object): object is MethodResponse {
    return Utils.instanceOfMethodResponse(object);
  }

  static instanceOfMethodFault(object: object): object is MethodFault {
    return Utils.instanceOfMethodFault(object);
  }

  static instanceOfXmlRpcStruct(object: XmlRpcTypes): object is XmlRpcStruct {
    return Utils.instanceOfXmlRpcStruct(object);
  }

  static instanceOfXmlRpcArray(object: XmlRpcTypes): object is XmlRpcArray {
    return Utils.instanceOfXmlRpcArray(object);
  }

  constructor(http: HttpClient) {
    this.headers = {};
    this.url = new URL('http://localhost');
    this.http = http;
  }

  /**
   * Call this method before any other to configure the service. Otherwise other methods may behave incorrectly.
   *
   * @param url The URL of the XMLRPC Service.
   */
  configureService(url: URL): void {
    this.url = url;
  }

  /**
   * Makes an XML-RPC call to the server specified by the constructor's options.
   *
   * @param method The method name.
   * @param params Params to send in the call.
   * @param encoding The encoding to append to the generated XML document.
   */
  methodCall(
    method: string,
    params?: Array<XmlRpcTypes>,
    encoding?: string,
  ): Observable<MethodResponse | MethodFault> {
    const xml = serializeMethodCall(method, params, encoding);
    const httpOptions = new HttpHeaders();
    httpOptions.set('Content-Type', 'text/xml');
    httpOptions.set('Accept', 'text/xml');
    const options: object = {
      headers: httpOptions,
      reportProgress: false,
      observe: BODY,
      responseType: TEXT,
      withCredentials: false,
    };

    return this.http.post<string>(this.url.toString(), xml, options).pipe(
      map<string, MethodResponse | MethodFault>((source: string) => {
        return deserialize(source);
      }),
    );
  }
}

results matching ""

    No results matching ""