Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameter as an object[]

I wish to use this API with a c# application: http://www.affjet.com/2012/11/26/4-4-affjet-api/#more-3099

After adding the wsdl to my projcet i wrote this simple code : (getTransactions gets a object[] @params and returns a string)

Ws_ApiService service = new Ws_ApiService();
string apiKey = "*************";
var response = service.getTransactions(new object[] { apiKey });

i tried few more ways but couldnt get a right response, i tried :

var response = service.getTransactions(new object[] { "apiKey:****"});

and

var response = service.getTransactions(new object[] { "apiKey","******"});

Here is the php code that does the same from their docs :

<?php

$nameSpace = "https://secure.affjet.com/ws/api";

//Creating AffJet client for SOAP
$client = new SoapClient($nameSpace."?wsdl");

$pageNumber = 0;
//Setting up parameters
$params = array();
$params["apiKey"] = "MY_API_KEY";
//Value for parameters (optional)
//$params["networkId"] = array(1,2);
//$params["pageNumber"] = 0;
//$params["pageSize"] = 10;
//Making Request
$response = $client->getNetworks($params);
//XML to SimpleXMLElement Object
$xmlResponse = new SimpleXMLElement($response);
if ($xmlResponse->success == "true"){
    while (isset($xmlResponse->dataList->data)) {
        //Iterate the results
        foreach ($xmlResponse->dataList->data as $data){
            var_dump(xml2array($data));
        }
        //Requesting next page of data
        $pageNumber++;
        $params["pageNumber"] = $pageNumber;
        //Making Request
        $response = $client->getNetworks($params);
        //XML to SimpleXMLElement Object
        $xmlResponse = new SimpleXMLElement($response);
    }
} else {
    //Error somewhere
    echo $xmlResponse->errorMessage;
}

/**
* Transforms the object SimpleXmlElement into an array, easier to handle
*/
function xml2array($xml) {
    $arr = array();
    foreach ($xml as $element) {
        $tag = $element->getName();
        $e = get_object_vars($element);
        if (!empty($e)) {
            $arr[$tag] = $element instanceof SimpleXMLElement ? xml2array($element) : $e;
        } else {
            $arr[$tag] = trim($element);
        }
    }
    return $arr;
}


?>

this was the response for what i tried :

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns1="http://secure.affjet.com/ws/api"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
          SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:getTransactionsResponse>
        <return xsi:type="xsd:string">
            &lt;response&gt;&lt;success&gt;false&lt;/success&gt;&lt;errorMessage&gt;
            API Key not provided&lt;/errorMessage&gt;&lt;dataList&gt;
            &lt;/dataList&gt;&lt;/response&gt;
        </return>
        </ns1:getTransactionsResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

you can see :

API Key not provided

And the response should be something like this:

<response>
    <success>true</success>
    <errorMessage></errorMessage>
    <dataList>
        <data>
            <date>2012-11-05 15:02:41</date>//Transaction Date
            <amount>81.67</amount>
            <commission>15.86</commission>
            <status>confirmed</status>//Status, could be: declined, pending, confirmed or paid
            <clickDate></clickDate>
            <ip></ip>
            <custom_id>MyCustomId</custom_id>//Custom Id for the transactions (SID, SubId,clickRef....)
            <unique_id>2548462</unique_id>//Unique Id given from the network to this transaction
            <merchantId>1</merchantId>//Id for the Merchant on AffJet
            <networkId>1</networkId>//Id for the Network on AffJet
        </data>
    </dataList>
</response>

all i need to supply is a parameter named "apiKey" and its value

EDIT :

after contacting their support, they said the request should look like this :

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
                   xmlns:ns1="http://secure.affjet.com/ws/api" 
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
                   xmlns:ns2="http://xml.apache.org/xml-soap" 
                   xmlns:SOAP- ENC="http://schemas.xmlsoap.org/soap/encoding/" 
            SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns1:getTransactions>
            <params xsi:type="ns2:Map">
                <item>
                    <key xsi:type="xsd:string">apiKey</key>
                    <value xsi:type="xsd:string">YOURAPIKEY</value>
                </item>
            </params>
        </ns1:getTransactions>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Any ideas ?

like image 847
Matan L Avatar asked Apr 25 '13 17:04

Matan L


People also ask

How do you pass an object as a parameter?

To pass an object as an argument we write the object name as the argument while calling the function the same way we do it for other variables. Syntax: function_name(object_name); Example: In this Example there is a class which has an integer variable 'a' and a function 'add' which takes an object as argument.

What is object [] in Java?

A Java object is a member (also called an instance) of a Java class. Each object has an identity, a behavior and a state. The state of an object is stored in fields (variables), while methods (functions) display the object's behavior. Objects are created at runtime from templates, which are also known as classes.

Can we pass object as parameter in Java?

In Java, we can pass a reference to an object (also called a "handle")as a parameter. We can then change something inside the object; we just can't change what object the handle refers to.

Can we pass object as parameter in C#?

We can pass the data to the methods in form of arguments and an object is an instance of a class that is created dynamically. The basic data types can be passed as arguments to the C# methods in the same way the object can also be passed as an argument to a method.


1 Answers

I dug a little deeper into this fascinating topic and have to say that working with what amounts to an associative array is just a pain in the .NET SOAP implementation. An associative array is represented as an IDictionary (e.g. Hashtable) – but refuses to be serialized (try it!).

The below code is the closest I could get – and for some reason (bug?) it does not work on the .NET framework, but does work on Mono.

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Text;

public class Test
{
  /*
   *  as per http://stackoverflow.com/a/1072815/2348103
   */
  public class Item
  {
    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public string key;

    [XmlElement(Form = XmlSchemaForm.Unqualified)]
    public string value;
  }

  [SoapType(TypeName = "Map", Namespace = "http://xml.apache.org/xml-soap")]
  public class Map : List<Item>
  {
  }

  public static void Main()
  {
    Map map = new Map();
    map.Add( new Item { key="foo", value="bar" } );
    map.Add( new Item { key="quux", value="barf" } );

    XmlTypeMapping mapping = 
         (new SoapReflectionImporter()).ImportTypeMapping( map.GetType() );
    XmlSerializer serializer = new XmlSerializer( mapping );
    XmlTextWriter writer = new XmlTextWriter( System.Console.Out );
    writer.Formatting = Formatting.Indented;
    writer.WriteStartElement( "root" );
    serializer.Serialize( writer, map );
    writer.WriteEndElement();
    writer.Close();
  }
}

/*
  // 
  // does not work - but see http://msdn.microsoft.com/en-us/magazine/cc164135.aspx
  // 
  public class Map : IXmlSerializable
  {
    const string NS = "http://xml.apache.org/xml-soap";
    public IDictionary dictionary;
    public Map()
    {
      dictionary = new Hashtable();
    }

    public Map(IDictionary dictionary)
    {
      this.dictionary = dictionary;
    }

    public void WriteXml(XmlWriter w)
    {
      w.WriteStartElement("Map", NS);
      foreach (object key in dictionary.Keys)
      {
        object value = dictionary[key];
        w.WriteStartElement("item", NS);
        w.WriteElementString("key", NS, key.ToString());
        w.WriteElementString("value", NS, value.ToString());
        w.WriteEndElement();
      }
      w.WriteEndElement();
    }

    public void ReadXml(XmlReader r)
    {
      r.Read(); // move past container
      r.ReadStartElement("dictionary");
      while (r.NodeType != XmlNodeType.EndElement)
      {
        r.ReadStartElement("item", NS);
        string key = r.ReadElementString("key", NS);
        string value = r.ReadElementString("value", NS);
        r.ReadEndElement();
        r.MoveToContent();
        dictionary.Add(key, value);
      }
    }
    public System.Xml.Schema.XmlSchema GetSchema() { return null; }
  }
*/

Sample output from Mono:

<q1:Map xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" d2p1:arrayType="Item[2]" xmlns:d2p1="http://schemas.xmlsoap.org/soap/encoding/" xmlns:q1="http://xml.apache.org/xml-soap">
  <Item href="#id2" />
  <Item href="#id3" />
</q1:Map>
[...]

Sample output from .NET:

<q1:Array xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="id1" q1:arrayType="Item[2]" xmlns:q1="http://schemas.xmlsoap.org/soap/encoding/">
  <Item href="#id2" />
  <Item href="#id3" />
</q1:Array>
[...]
like image 76
Stefan Paletta Avatar answered Nov 14 '22 06:11

Stefan Paletta