public static BaseUrl FromValue(Value v) { BaseUrl b = new BaseUrl(); b._root = v.struct_value["root"].string_value; b._page = v.struct_value["page"].string_value; b._data = v.struct_value["data"].string_value; return b; }
public ScrapEntry(Boolean p_Fault, Value p_FaultValue) : base(p_Fault, p_FaultValue) { }
public static Scrap FromValue(Value p_Value) { // value would be an struct // member -> scrap_id // value -> struct with scrapentry info Scrap s = new Scrap(); s._sql = p_Value.struct_value["sql"].string_value; foreach (KeyValuePair<String, Value> kvp in p_Value.struct_value["scrap"].struct_value) { Int32 scrap_id = 0; if (Int32.TryParse(kvp.Key, out scrap_id)) { ScrapEntry se = ScrapEntry.FromValue(kvp.Value, scrap_id); s._entries.Add(scrap_id, se); } else { throw new Exception("Invalid scrap_id -> [" + kvp.Key + "]"); } } return s; }
public static Msgs FromValue(Value p_Value) { // value would be an struct // member -> msg_id // value -> struct with msg info Msgs ms = new Msgs(); ms._sql = p_Value.struct_value["sql"].string_value; foreach (KeyValuePair<String, Value> kvp in p_Value.struct_value["msg"].struct_value) { Int32 msg_id = 0; if (Int32.TryParse(kvp.Key, out msg_id)) { Msg m = Msg.FromValue(kvp.Value, msg_id); ms._entries.Add(msg_id, m); } else { throw new Exception("Invalid msg_id -> [" + kvp.Key + "]"); } } return ms; }
private void Parse() { Boolean found = false; StringReader sr = new StringReader(_raw_response); XmlDocument xdoc = new XmlDocument(); xdoc.Load(sr); // won't use GetElementsByTagName to validate the actual document structure. // find methodResponse tag found = false; XmlNode methodResponseNode = null; foreach (XmlNode x in xdoc.ChildNodes) { if (x.NodeType == XmlNodeType.Element && x.Name == MethodResponse.METHOD_RESPONSE_TAG) { methodResponseNode = x; found = true; break; } } if (!found) throw new Exception(MethodResponse.METHOD_RESPONSE_TAG + " tag not found on response"); // find params or fault element found = false; foreach (XmlNode x in methodResponseNode.ChildNodes) { if (x.NodeType != XmlNodeType.Element) continue; if (x.Name == MethodResponse.FAULT_TAG) { foreach (XmlNode y in x.ChildNodes) { if (y.NodeType != XmlNodeType.Element) continue; if (y.Name == Value.VALUE_TAG) { _fault_value = Value.FromXml(y); found = true; _fault = true; } } break; } else if (x.Name == MethodResponse.PARAMS_TAG) { _fault = false; // read all parameters returned foreach (XmlNode y in x.ChildNodes) { if (y.NodeType != XmlNodeType.Element) continue; if (y.Name != MethodResponse.PARAM_TAG) throw new Exception("Invalid " + y.Name + " tag inside params"); else { // find value node foreach (XmlNode z in y.ChildNodes) { if (z.NodeType != XmlNodeType.Element) continue; if (z.Name == MethodResponse.VALUE_TAG) // add to values _values.Add(Value.FromXml(z)); else throw new Exception("Invalid tag " + z.Name + " inside param tag"); } } } found = true; break; } } if (!found) throw new Exception(MethodResponse.FAULT_TAG + " or " + MethodResponse.PARAMS_TAG + " not found!"); }
public Tocobject() { _fault = false; _fault_value = null; }
// Execute multi :( public List<Tocobject> ExecuteSystemMultiCall() { MethodCall mc = new MethodCall(Toco.SYSTEM_MULTI_CALL_METHOD); // prepare unique parameter with multiple calls Value varray = new Value(tocorre.XmlRpc.Type.XMLRPC_TYPE_ARRAY); foreach (MethodCall mcc in m_MethodCalls) { Value vcall = new Value(tocorre.XmlRpc.Type.XMLRPC_TYPE_STRUCT); vcall.AddToStruct("methodName", new Value(tocorre.XmlRpc.Type.XMLRPC_TYPE_STRING, mcc.method_name)); Value vparams = new Value(tocorre.XmlRpc.Type.XMLRPC_TYPE_ARRAY); foreach (Value param in mcc.parameters) { vparams.AddToArray(param); } vcall.AddToStruct("params", vparams); varray.AddToArray(vcall); } mc.AddParameter(varray); Call c = new Call("http://www.tocorre.com/ext/rpc/rpc_api.php", mc); MethodResponse mr = c.Execute(); // unique return is an array type Value. Each array element has a Value // corresponding to the return value of the nth method call queued List<Tocobject> ret = new List<Tocobject>(); Int32 i = 0; foreach (MethodCall mcc in m_MethodCalls) { Value v = mr.values[0].array_value[i]; switch (mcc.method_name) { case LOGIN_METHOD: if (v.type == tocorre.XmlRpc.Type.XMLRPC_TYPE_ARRAY) ret.Add(Session.FromValue(v.array_value[0])); else ret.Add(new Session(true, v)); break; case PROFILE_METHOD: if (v.type == tocorre.XmlRpc.Type.XMLRPC_TYPE_ARRAY) ret.Add(GenericValue.FromValue(v.array_value[0])); else ret.Add(new GenericValue(true, v)); break; case SCRAPS_METHOD: if (v.type == tocorre.XmlRpc.Type.XMLRPC_TYPE_ARRAY) ret.Add(Scrap.FromValue(v.array_value[0])); else ret.Add(new Scrap(true, v)); break; case MSG_METHOD: if (v.type == tocorre.XmlRpc.Type.XMLRPC_TYPE_ARRAY) ret.Add(Msgs.FromValue(v.array_value[0])); else ret.Add(new Msgs(true, v)); break; case FLUSH_METHOD: if (v.type == tocorre.XmlRpc.Type.XMLRPC_TYPE_ARRAY) ret.Add(GenericValue.FromValue(v.array_value[0])); else ret.Add(new GenericValue(true, v)); break; case SET_MOTD_METHOD: if (v.type == tocorre.XmlRpc.Type.XMLRPC_TYPE_ARRAY) ret.Add(GenericValue.FromValue(v.array_value[0])); else ret.Add(new GenericValue(true, v)); break; default: throw new Exception("Unexpected method name: " + mcc.method_name); } i++; } return ret; }
public Session(Boolean p_Fault, Value p_FaultValue) : base(p_Fault, p_FaultValue) { }
public Img(Boolean p_Fault, Value p_FaultValue) : base(p_Fault, p_FaultValue) { }
public static GenericValue FromValue(Value p_Value) { GenericValue gv = new GenericValue(); gv._value = p_Value; return gv; }
public GenericValue(Boolean p_Fault, Value p_FaultValue) : base(p_Fault, p_FaultValue) { }
public static Foto FromValue(Value v) { Foto f = new Foto(); f._ty = v.struct_value["ty"].string_value; f._sm = v.struct_value["sm"].string_value; f._lg = v.struct_value["lg"].string_value; return f; }
public void AddToStruct(String p_Member, Value p_Value) { if (_type != Type.XMLRPC_TYPE_STRUCT) throw new Exception("Value type is not STRUCT"); _struct_value[p_Member] = p_Value; }
public void AddToArray(Value p_Value) { if (_type != Type.XMLRPC_TYPE_ARRAY) throw new Exception("Value type is not ARRAY"); _array_value.Add(p_Value); }
public static Value FromXml(XmlNode p_Node) { Value v = null; foreach (XmlNode x in p_Node.ChildNodes) { if (x.NodeType != XmlNodeType.Element && x.NodeType != XmlNodeType.Text) continue; if (x.NodeType == XmlNodeType.Text) { // by default, it is a string v = new Value(Type.XMLRPC_TYPE_STRING, x.Value); } else { // it's an element if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_BOOLEAN)) { if (x.InnerText == "0") v = new Value(Type.XMLRPC_TYPE_BOOLEAN, false); else v = new Value(Type.XMLRPC_TYPE_BOOLEAN, true); } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_DOUBLE)) { Double d; if (Double.TryParse(x.InnerText, out d)) { v = new Value(Type.XMLRPC_TYPE_DOUBLE, d); } else { throw new Exception("Invalid double value: " + x.InnerText); } } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_INTEGER) || x.Name == "i4") // i4 is alternate typing for int { Int32 i; if (Int32.TryParse(x.InnerText, out i)) { v = new Value(Type.XMLRPC_TYPE_INTEGER, i); } else { throw new Exception("Invalid integer value: " + x.InnerText); } } else if ( x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_STRING) || x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_BASE64) || x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_DATETIME) ) { v = new Value(Value.GetType(x.Name), x.InnerText); } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_NIL)) { v = new Value(Type.XMLRPC_TYPE_NIL); } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_ARRAY)) { v = new Value(Type.XMLRPC_TYPE_ARRAY); foreach (XmlNode y in x.ChildNodes) { if (y.NodeType != XmlNodeType.Element) continue; if (y.Name != Value.DATA_TAG) throw new Exception("Got " + y.Name + " tag. Expected " + Value.DATA_TAG); foreach (XmlNode z in y.ChildNodes) { if (z.NodeType != XmlNodeType.Element) continue; if (z.Name != Value.VALUE_TAG) throw new Exception("Got " + z.Name + " tag. Expected " + Value.VALUE_TAG); v.AddToArray(Value.FromXml(z)); } } } else if (x.Name == Value.GetTypeName(Type.XMLRPC_TYPE_STRUCT)) { v = new Value(Type.XMLRPC_TYPE_STRUCT); foreach (XmlNode y in x.ChildNodes) { if (y.NodeType != XmlNodeType.Element) continue; if (y.Name != Value.MEMBER_TAG) throw new Exception("Got " + y.Name + " tag. Expected " + Value.DATA_TAG); String name = null; Value innerValue = null; foreach (XmlNode z in y.ChildNodes) { if (z.NodeType != XmlNodeType.Element) continue; if (z.Name == Value.NAME_TAG) name = z.InnerText; else if (z.Name == Value.VALUE_TAG) innerValue = Value.FromXml(z); else throw new Exception("Got " + z.Name + " tag. Expected " + Value.VALUE_TAG); } if (name != null && innerValue != null) v.AddToStruct(name, innerValue); else throw new Exception("name or value tags are missing"); } } else { throw new Exception("Invalid type name: " + p_Node.Name); } } } return v; }
public BaseUrl(Boolean p_Fault, Value p_FaultValue) : base(p_Fault, p_FaultValue) { }
public static ScrapEntry FromValue(Value p_Value, Int32 p_ScrapId) { ScrapEntry se = new ScrapEntry(); se._scrap_id = p_ScrapId; se._settime = DateTime.Parse(p_Value.struct_value["settime"].string_value); se._regtime = DateTime.Parse(p_Value.struct_value["regtime"].string_value); se._text = p_Value.struct_value["text"].string_value; Int32.TryParse(p_Value.struct_value["nid"].string_value, out se._nid); Int32.TryParse(p_Value.struct_value["flags"].string_value, out se._flags); return se; }
public static Img FromValue(Value v) { Img i = new Img(); i._icon = v.struct_value["icon"].string_value; i._foto = Foto.FromValue(v.struct_value["foto"]); return i; }
public static Session FromValue(Value v) { Session s = new Session(); // the answer comes as a struct s._sid_id = v.struct_value["sid_id"].string_value; s._nid = v.struct_value["nid"].int_value; s._nick = v.struct_value["nick"].string_value; s._fname = v.struct_value["fname"].string_value; s._lname = v.struct_value["lname"].string_value; s._flags = v.struct_value["flags"].int_value; s._gender = v.struct_value["gender"].string_value; s._locale = v.struct_value["locale"].string_value; s._lang = v.struct_value["lang"].int_value; s._prefs = v.struct_value["prefs"].int_value; s._base_url = BaseUrl.FromValue(v.struct_value["base_url"]); s._page = v.struct_value["page"].string_value; s._img = Img.FromValue(v.struct_value["img"]); return s; }
public static Msg FromValue(Value p_Value, Int32 p_MsgId) { Msg m = new Msg(); m._msg_id = p_MsgId; m._settime = DateTime.Parse(p_Value.struct_value["settime"].string_value); m._regtime = DateTime.Parse(p_Value.struct_value["regtime"].string_value); m._text = p_Value.struct_value["text"].string_value; Int32.TryParse(p_Value.struct_value["nid"].string_value, out m._nid); Int32.TryParse(p_Value.struct_value["flags"].string_value, out m._flags); return m; }
public Foto(Boolean p_Fault, Value p_FaultValue) : base(p_Fault, p_FaultValue) { }
public Msgs(Boolean p_Fault, Value p_FaultValue) : base(p_Fault, p_FaultValue) { }
public Tocobject(Boolean p_Fault, Value p_FaultValue) { this._fault = p_Fault; this._fault_value = p_FaultValue; }
public void AddParameter(Value p_Parameter) { _parameters.Add(p_Parameter); }