/// <summary> /// Downloads the requested resource as a <see cref="string"/>. /// The resource to download is specified as a <see cref="string"/> containing the URI. /// </summary> /// <param name="address">A string containing the URI to download</param> /// <param name="headers">Defines header values to add to the request</param> /// <param name="userName">The user name associated with the credentials</param> /// <param name="password">The password for the user name associated with the credentials</param> /// <returns>The requested resource as a <see cref="string"/></returns> public string Download(string address, PyObject headers = null, string userName = null, string password = null) { var dict = new Dictionary <string, string>(); if (headers != null) { using (Py.GIL()) { // In python algorithms, headers must be a python dictionary // In order to convert it into a C# Dictionary if (PyDict.IsDictType(headers)) { foreach (PyObject pyKey in headers) { var key = (string)pyKey.AsManagedObject(typeof(string)); var value = (string)headers.GetItem(pyKey).AsManagedObject(typeof(string)); dict.Add(key, value); } } else { throw new ArgumentException($"QCAlgorithm.Fetch(): Invalid argument. {headers.Repr()} is not a dict"); } } } return(Download(address, dict, userName, password)); }
public bool TryDecode <T>(PyObject pyObj, out T value) { if (!PyDict.IsDictType(pyObj)) { value = default; return(false); } using (var pyDict = new PyDict(pyObj)) { if (typeof(T).IsGenericType) { value = pyDict.ToDictionary <T>(); } else { value = (T)pyDict.ToDictionary(); } return(true); } }