Created
February 1, 2011 07:05
-
-
Save regisbamba/805529 to your computer and use it in GitHub Desktop.
example XML-RPC client for Magento API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redstone.xmlrpc.XmlRpcClient; | |
import redstone.xmlrpc.XmlRpcException; | |
import redstone.xmlrpc.XmlRpcFault; | |
import java.net.MalformedURLException; | |
import java.util.HashMap; | |
import redstone.xmlrpc.XmlRpcArray; | |
import redstone.xmlrpc.XmlRpcStruct; | |
public class MagentoClient { | |
private String serverUrl; | |
private String apiUser; | |
private String apiKey; | |
private String sessionId; | |
public XmlRpcClient client; | |
public MagentoClient(String url, String user, String key) throws MalformedURLException { | |
this.serverUrl = url; | |
this.apiUser = user; | |
this.apiKey = key; | |
this.client = new XmlRpcClient(this.serverUrl, false); | |
} | |
public String login() throws XmlRpcFault { | |
this.sessionId = (String) this.client.invoke("login", new Object[] {this.apiUser, this.apiKey}); | |
return this.sessionId; | |
} | |
public XmlRpcArray orderList(HashMap filters) throws XmlRpcException, XmlRpcFault { | |
Object [] params = this.createParams("sales_order.list", filters); | |
XmlRpcArray orders = (XmlRpcArray) this.client.invoke("call", params); | |
return orders; | |
} | |
public XmlRpcStruct orderInfo(String incrementId) throws XmlRpcException, XmlRpcFault { | |
Object [] params = this.createParams("sales_order.info", incrementId); | |
XmlRpcStruct order = (XmlRpcStruct) this.client.invoke("call", params); | |
return order; | |
} | |
private Object[] createParams(String resourcePath, Object arguments) { | |
Object [] params = new Object [] {this.sessionId, resourcePath, new Object [] {arguments}}; | |
return params; | |
} | |
// Getters and Setters | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.MalformedURLException; | |
import java.util.HashMap; | |
import redstone.xmlrpc.XmlRpcArray; | |
import redstone.xmlrpc.XmlRpcFault; | |
import redstone.xmlrpc.XmlRpcStruct; | |
public class MagentoClientTest { | |
public static void main(String[] args) { | |
try { | |
MagentoClient client = new MagentoClient("http://yourmagentohost/api/xmlrpc", "apiUser", "apiKey"); | |
System.out.println("Session id is " + client.login()); | |
HashMap from = new HashMap(); | |
from.put("from", "2010"); | |
HashMap filters = new HashMap(); | |
filters.put("created_at", from); | |
XmlRpcArray orders = client.orderList(filters); | |
System.out.println("Latest orders are " + orders.size()); | |
if (orders.size() > 0) { | |
XmlRpcStruct firstOrder = orders.getStruct(0); | |
String incrementId = firstOrder.getString("increment_id"); | |
XmlRpcStruct orderInfo = client.orderInfo(incrementId); | |
System.out.println("Info for Order # " + incrementId); | |
System.out.println(orderInfo); | |
} | |
} catch (MalformedURLException ex) { | |
System.out.println(ex.getMessage()); | |
} | |
catch (XmlRpcFault ex) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment