Skip to content

AlamofireXMLRPC brings XML RPC functionalities to Alamofire

License

Notifications You must be signed in to change notification settings

mfMeds/AlamofireXMLRPC

 
 

Repository files navigation

AlamofireXMLRPC

License Platform Language Issues Cocoapod Build Status codecov codebeat badge

AlamofireXMLRPC brings XML RPC functionalities to Alamofire. It aims to provide an easy way to perform XMLRPC call and to retrieve smoothly the response.

XML is handled internally with AEXML.

Example

Take the following request and response handler:

let data: NSData = ...
let params: [Any] = [42, "text", 3.44, Date(), data]
AlamofireXMLRPC.request("http://localhost:8888/xmlrpc", methodName: "foo", parameters: params).responseXMLRPC { (response: DataResponse<XMLRPCNode>) -> Void in
	   switch response.result {
      case .success(let value):
      		if let message = value[0].string, age = value[1]["age"].int32  {
              ...
     		}
      case .failure:
            ...
      }

It will generate the following call and lets you parse the corresponding answer from the XMLRPC service:

<!-- request -->
<methodCall>
	<methodName>foo</methodName>
	<params>
		<param>
			<value>
				<int>42</int>
			</value>
		</param>
		<param>
			<value>
				<string>text</string>
			</value>
		</param>
		<param>
			<value>
				<double>3.44</double>
			</value>
		</param>
		<param>
			<value>
				<dateTime.iso8601>19980717T14:08:55</dateTime.iso8601>
			</value>
		</param>
		<param>
			<value>
				<base64>eW91IGNhbid0IHJlYWQgdGhpcyE=</base64>
			</value>
		</param>
	</params>
</methodCall>

<!-- response -->
<methodResponse>
  <params>
    <param>
      <value>
        <string>Hello world!</string>
      </value>
    </param>
    <param>
      <value>
        <struct>
          <member>
            <name>name</name>
            <value>
              <string>John Doe</string>
            </value>
          </member>
          <member>
            <name>age</name>
            <value>
              <int>36</int>
            </value>
          </member>
        </struct>
      </value>
    </param>
  </params>
</methodResponse>

Requirements

  • iOS 9.0+ / Mac OS X 10.11+
  • Xcode 9

Install CocoaPods

You can use CocoaPods to install AlamofireXMLRPCby adding it to your Podfile:

use_frameworks!

target 'MyApp' do
	pod 'AlamofireXMLRPC', :git => 'https://github.com/kodlian/AlamofireXMLRPC.git'
end

Request

Method request

// Call XMLRPC service with the sharedManager of Alamofire
AlamofireXMLRPC.request("https://xmlrpcservice", method:"foo" parameters: [1,2.0,"ddd",["key":"value"]])
// Call XMLRPC service with your custom manager
manager.requestXMLRPC("https://xmlrpcservice", method:"foo" parameters: [1,2.0,"ddd",["key":"value"]])

XMLRPC Request convertible

Types adopting the XMLRPCRequestConvertible protocol can be used to construct XMLRPC call.

public protocol XMLRPCRequestConvertible {
    var url: URLConvertible { get }
    var methodName: String { get }
    var parameters: [Any]? { get }
    var headers: [String : String]? { get }
}

struct MyRequest: XMLRPCRequestConvertible {
  ...
}

let request =  MyRequest(...)
Alamofire.request(request) // Call XMLRPC service with Alamofire

Values

Mapping

AlamofireXMLRPC uses the following mapping for swift values to XML RPC values:

Swift Example XMLRPC note
String "foo" <string>foo</string>
Int, Int32, Int16, Int8, UInt16, UInt8 42 <int>42</int> XML RPC Integer is 32bits, Int values are converted to Int32
Bool true <boolean>1</boolean>
Double, Float 3.44 <double>3.44</double>
Date Date() <dateTime.iso8601>19980717T14:08:55</dateTime.iso8601>
Data Data() <base64>eW91IGNhbid0IHJlYWQgdGhpcyE=</base64>

By default other types will be mapped as XML RPC String and use the default String representation. Bu you can provide your own mapping for custom Types by adopting the protocol XMLRPCRawValueRepresentable.

enum XMLRPCValueKind: String {
    case integer = "int"
    case double = "double"
    case boolean = "boolean"
    case string = "string"
    case dateTime = "dateTime.iso8601"
    case base64 = "base64"
}

protocol XMLRPCRawValueRepresentable {
    static var xmlrpcKind: XMLRPCValueKind { get }
    var xmlrpcRawValue: String { get }
    init?(xmlrpcRawValue: String)
}

Collection

Swift arrays [Any] are convertible to XMLRPC arrays.

[1,"too"]

As well dictionaries [String:Any] are convertible to XMLRPC structure.

["name":"John Doe","age":35]

Response

Response

XMLRPC Responses are handled with the method responseXMLRPC(completionHandler: DataResponse<XMLRPCNode> -> Void). The response's XMLRPC parameters are mapped to an XMLRPCNode. This allows you to fetch easily data within complex structure or tree.

Subscript

For each XMLRPCNode you can access subnode by index or by String key. Internally children of XMLRPC Array and Structure will be fetched.

aRequest.responseXMLRPC{ (response: DataResponse<XMLRPCNode>) -> Void in
      switch response.result {
      case .success(let value):
      		if let message = value[0]["aKey"][9].string  {
              ...
     		}
      case .failure:
            ...
      }
}

Don't worry about unwrapping things and checking the value type or presence. The optional management is solely done when you request the swift value with one of the optional getters.

For instance, you can call value[0]["aKey"][9] without worrying if the objects tree actually exists.

Optional getters

var array: [XMLRPCNode]?
var dictionary: [String:XMLRPCNode]?
var string: String?
var int32: Int32?
var double: Double?
var bool: Bool?
var date: Date?
var data: Data?
var count: Int?

License

AlamofireXMLRPC is released under the MIT license. See LICENSE for details.

About

AlamofireXMLRPC brings XML RPC functionalities to Alamofire

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 88.7%
  • Ruby 9.4%
  • Objective-C 1.9%