Skip to content

Commit

Permalink
Added custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
francescogabbrielli committed Apr 1, 2021
1 parent 079441f commit 3c00c18
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
23 changes: 16 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ This library was generated with [Angular CLI](https://github.com/angular/angular

This is actually a port from the AngularJS library [Angular-Xmlrpc](https://github.com/ITrust/angular-xmlrpc). **NOTE**: IE support has been dropped altogether.

Installation
------------
## Installation

npm install angular2-xmlrpc --save


How to use it ?
---------------
## How to use it ?

### Configuration

First of all, add a dependency in your module:
``` typescript
``` ts
import { XmlrpcModule } from 'angular2-xmlrpc'

@NgModule({
Expand All @@ -30,12 +30,13 @@ import { XmlrpcModule } from 'angular2-xmlrpc'
export class AppModule { }
```
You can use it in your application as any other service, and inject it in the constructor as usual.
``` typescript
``` ts
import { XmlrpcService } from 'angular2-xmlrpc'

constructor(..., private xmlrpc:XmlrpcService, ...) {
}
```
### XML-RPC Call
There is only one main method to call an XML-RPC method. In order to pass parameters, you have to wrap them in an array:
``` typescript
const xmlrpcCall = this.xmlrpc.callMethod(
Expand All @@ -45,6 +46,14 @@ const xmlrpcCall = this.xmlrpc.callMethod(
)
```
The XML response from the server is wrapped in an `Observable`, and can be parsed to a JS object with the `parseResponse` method:
``` typescript
``` ts
xmlrpcCall.subscribe(data => console.log(this.xmlrpc.parseResponse(data)))
```

### Custom Headers
to set custom headers:
``` ts
this.xmlrpc.headers.set('Custom-Header', 'Value').
```
- _Note 1_: the field `headers` is of type [HttpHeaders](https://angular.io/api/common/http/HttpHeaders).
- _Note 2_: headers 'Content-Type' and 'Accept' cannot be set and will be overridden (for obvious reasons).
11 changes: 11 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
v1.0.2
------
- Added custom headers field

v1.0.1
------
- Basic unit testing

v1.0.0
------
- Port from angular-xmlrpc AngularJS package (with no IE support)
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "angular2-xmlrpc",
"description": "An Angular service which provides XML-RPC communication methods",
"version": "1.0.1",
"version": "1.0.2",
"author": "Francesco Gabbrielli",
"license": "ISC",
"homepage": "https://github.com/francescogabbrielli/angular2-xmlrpc",
"keywords": [
"xmlrpc",
"angular"
],
"peerDependencies": {
"@angular/common": ">=9",
"@angular/core": ">=9"
Expand Down
17 changes: 10 additions & 7 deletions src/lib/xmlrpc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ import { Xml2js } from './xml2js'
})
export class XmlrpcService {

helper:Helper
js2xml:Js2xml
xml2js:Xml2js
private readonly helper:Helper
private readonly js2xml:Js2xml
private readonly xml2js:Xml2js

/** HTTP headers */
readonly headers:HttpHeaders

constructor(private http:HttpClient) {
this.helper = new Helper()
this.js2xml = new Js2xml(this.helper)
this.xml2js = new Xml2js(this.helper)
this.headers = new HttpHeaders()
}

/**
Expand Down Expand Up @@ -58,11 +62,10 @@ export class XmlrpcService {
*/
callMethod(url:string, method:string, params:any[]):Observable<any> {
const xmlstr = this.createCall(method, params)
this.headers.set('Content-Type', 'application/xml')
this.headers.set('Accept', 'text/html, application/xhtml+xml, */*')
return this.http.post<any>(url, xmlstr, {
headers: new HttpHeaders({
'Content-Type': 'application/xml',
'Accept': 'text/html, application/xhtml+xml, */*'
}),
headers: this.headers,
observe: 'body',
responseType: 'text' as 'json'
})
Expand Down

0 comments on commit 3c00c18

Please sign in to comment.