Code Monkey home page Code Monkey logo

alamofirexmlrpc's Introduction

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 8.0+ / Mac OS X 10.10+
  • Xcode 7

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.