示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:FuelSDK.PatchReturn"/> class.
        /// </summary>
        /// <param name="objs">Objects.</param>
        public PatchReturn(APIObject objs)
        {
            if (objs == null)
            {
                throw new ArgumentNullException("objs");
            }
            var response = ExecuteAPI((client, o) =>
            {
                string requestID;
                string overallStatus;
                return(new ExecuteAPIResponse <UpdateResult>(client.SoapClient.Update(new UpdateOptions(), o, out requestID, out overallStatus), requestID, overallStatus));
            }, objs);

            if (response != null)
            {
                if (response.GetType() == typeof(UpdateResult[]) && response.Length > 0)
                {
                    Results = response.Cast <UpdateResult>().Select(x => new ResultDetail
                    {
                        StatusCode    = x.StatusCode,
                        StatusMessage = x.StatusMessage,
                        Object        = (x.Object != null ? (objs.GetType().ToString().Contains("ET_") ? TranslateObject2(x.Object) : TranslateObject(x.Object)) : null),
                        OrdinalID     = x.OrdinalID,
                        ErrorCode     = x.ErrorCode,
                    }).ToArray();
                }
                else
                {
                    Results = new ResultDetail[0];
                }
            }
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:FuelSDK.DeleteReturn"/> class.
        /// </summary>
        /// <param name="objs">APIObject</param>
        public DeleteReturn(APIObject objs)
        {
            if (objs == null)
            {
                throw new ArgumentNullException("objs");
            }

            var response = ExecuteAPI((client, o) =>
            {
                var deleteRequest  = new DeleteRequest(new DeleteOptions(), o);
                var deleteResponse = client.SoapClient.DeleteAsync(deleteRequest).Result;


                return(new ExecuteAPIResponse <DeleteResult>(deleteResponse.Results, deleteResponse.RequestID, deleteResponse.OverallStatus));
            }, objs);

            if (response != null)
            {
                if (response.GetType() == typeof(DeleteResult[]) && response.Length > 0)
                {
                    Results = response.Cast <DeleteResult>().Select(x => new ResultDetail
                    {
                        StatusCode    = x.StatusCode,
                        StatusMessage = x.StatusMessage,
                        Object        = (x.Object != null ? (objs.GetType().ToString().Contains("ET_") ? TranslateObject2(x.Object) : TranslateObject(x.Object)) : null),
                        OrdinalID     = x.OrdinalID,
                        ErrorCode     = x.ErrorCode,
                    }).ToArray();
                }
                else
                {
                    Results = new ResultDetail[0];
                }
            }
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:FuelSDK.InfoReturn"/> class.
        /// </summary>
        /// <param name="objs">Objects.</param>
        public InfoReturn(APIObject objs)
        {
            if (objs == null)
            {
                throw new ArgumentNullException("objs");
            }
            var response = ExecuteAPI(x => new ObjectDefinitionRequest
            {
                ObjectType = TranslateObject(x).GetType().ToString().Replace("FuelSDK.", string.Empty)
            }, (client, o) =>
            {
                string requestID;
                return(new ExecuteAPIResponse <ObjectDefinition>(client.SoapClient.Describe(o, out requestID), requestID, "OK"));
            }, objs);

            if (response != null)
            {
                if (response.Length > 0)
                {
                    if (objs.GetType().ToString().Contains("ET_"))
                    {
                        Results = response[0].Properties.Select(x => (ET_PropertyDefinition)(TranslateObject2(x))).ToArray();
                    }
                    else
                    {
                        Results = response[0].Properties.Select(x => (ETPropertyDefinition)(TranslateObject(x))).ToArray();
                    }
                }
                else
                {
                    Status = false;
                }
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="T:FuelSDK.PerformReturn"/> class.
        /// </summary>
        /// <param name="objs">Objects.</param>
        /// <param name="performAction">Perform action.</param>
        public PerformReturn(APIObject objs, string performAction)
        {
            if (objs == null)
            {
                throw new ArgumentNullException("objs");
            }

            var response = ExecuteAPI((client, o) =>
            {
                var options         = new PerformOptions();
                var performRequest  = new PerformRequest(options, performAction, o);
                var performResponse = client.SoapClient.PerformAsync(performRequest).Result;

                return(new ExecuteAPIResponse <PerformResult>(performResponse.Results, performResponse.RequestID, performResponse.OverallStatus)
                {
                    OverallStatusMessage = performResponse.OverallStatusMessage
                });
            }, objs);

            if (response != null)
            {
                if (response.GetType() == typeof(PerformResult[]) && response.Length > 0)
                {
                    Results = response.Cast <PerformResult>().Select(cr => new ResultDetail
                    {
                        StatusCode    = cr.StatusCode,
                        StatusMessage = cr.StatusMessage,
                        Object        = (cr.Object != null ? (objs.GetType().ToString().Contains("ET_") ? TranslateObject2(cr.Object) : TranslateObject(cr.Object)) : null),
                        Task          = cr.Task,
                        OrdinalID     = cr.OrdinalID,
                        ErrorCode     = cr.ErrorCode,
                    }).ToArray();
                }
                else
                {
                    Results = new ResultDetail[0];
                }
            }
        }
示例#5
0
 /// <summary>
 /// Translates the object.
 /// </summary>
 /// <returns>The object.</returns>
 /// <param name="inputObject">Input <see cref="T:FuelSDK.APIObject"/> object.</param>
 public APIObject TranslateObject2(APIObject inputObject)
 {
     if (_translators2.ContainsKey(inputObject.GetType()))
     {
         var returnObject = (APIObject)Activator.CreateInstance(_translators2[inputObject.GetType()]);
         foreach (var prop in inputObject.GetType().GetProperties())
         {
             if (prop.Name == "UniqueID")
             {
                 continue;
             }
             var propValue = prop.GetValue(inputObject, null);
             if ((prop.PropertyType.IsSubclassOf(typeof(APIObject)) || prop.PropertyType == typeof(APIObject)) && propValue != null)
             {
                 prop.SetValue(returnObject, TranslateObject2(propValue), null);
             }
             else if (_translators2.ContainsKey(prop.PropertyType) && propValue != null)
             {
                 prop.SetValue(returnObject, TranslateObject2(propValue), null);
             }
             else if (prop.PropertyType.IsArray && propValue != null)
             {
                 var   a = (Array)propValue;
                 Array outArray;
                 if (a.Length > 0)
                 {
                     if (_translators2.ContainsKey(a.GetValue(0).GetType()))
                     {
                         outArray = Array.CreateInstance(_translators2[a.GetValue(0).GetType()], a.Length);
                         for (int i = 0; i < a.Length; i++)
                         {
                             if (_translators2.ContainsKey(a.GetValue(i).GetType()))
                             {
                                 outArray.SetValue(TranslateObject2(a.GetValue(i)), i);
                             }
                         }
                         if (outArray.Length > 0)
                         {
                             prop.SetValue(returnObject, outArray, null);
                         }
                     }
                 }
             }
             else if (prop.Name == "FolderID" && propValue != null)
             {
                 if (inputObject.GetType().GetProperty("Category") != null)
                 {
                     var categoryIDProp = inputObject.GetType().GetProperty("Category");
                     categoryIDProp.SetValue(returnObject, propValue, null);
                 }
                 else if (inputObject.GetType().GetProperty("CategoryID") != null)
                 {
                     var categoryIDProp = inputObject.GetType().GetProperty("CategoryID");
                     categoryIDProp.SetValue(returnObject, propValue, null);
                 }
             }
             else if ((prop.Name == "CategoryIDSpecified" || prop.Name == "CategorySpecified") && propValue != null)
             {
                 // Do nothing
             }
             else if ((prop.Name == "CategoryID" || prop.Name == "Category") && propValue != null)
             {
                 if (returnObject.GetType().GetProperty("FolderID") != null)
                 {
                     var folderIDProp = returnObject.GetType().GetProperty("FolderID");
                     folderIDProp.SetValue(returnObject, Convert.ToInt32(propValue), null);
                 }
             }
             else if (propValue != null && returnObject.GetType().GetProperty(prop.Name) != null)
             {
                 prop.SetValue(returnObject, propValue, null);
             }
         }
         return(returnObject);
     }
     return(inputObject);
 }
示例#6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:FuelSDK.GetReturn"/> class.
        /// </summary>
        /// <param name="objs">Objects.</param>
        /// <param name="continueRequest">If set to <c>true</c> continue request.</param>
        /// <param name="overrideObjectType">Override object type.</param>
        public GetReturn(APIObject objs, bool continueRequest, string overrideObjectType)
        {
            if (objs == null)
            {
                throw new ArgumentNullException("objs");
            }
            var response = ExecuteAPI(x =>
            {
                var rr = new RetrieveRequest();

                // Handle RetrieveAllSinceLastBatch
                if (x.GetType().GetProperty("GetSinceLastBatch") != null)
                {
                    rr.RetrieveAllSinceLastBatch = (bool)x.GetType().GetProperty("GetSinceLastBatch").GetValue(x, null);
                }

                if (continueRequest)
                {
                    if (x.LastRequestID == null)
                    {
                        throw new Exception("Unable to call GetMoreResults without first making successful Get() request");
                    }
                    rr.ContinueRequest = x.LastRequestID;
                }
                else
                {
                    if (x.SearchFilter != null)
                    {
                        rr.Filter = x.SearchFilter;
                    }

                    // Use the name from the object passed in unless an override is passed (Used for DataExtensionObject)
                    if (string.IsNullOrEmpty(overrideObjectType))
                    {
                        if (x.GetType().ToString().Contains("ET_"))
                        {
                            rr.ObjectType = TranslateObject2(x).GetType().ToString().Replace("FuelSDK.", string.Empty);
                        }
                        else
                        {
                            rr.ObjectType = TranslateObject(x).GetType().ToString().Replace("FuelSDK.", string.Empty);
                        }
                    }
                    else
                    {
                        rr.ObjectType = overrideObjectType;
                    }

                    // If they didn't specify Props then we look them up using Info()
                    if (x.Props == null && x.GetType().GetMethod("Info") != null)
                    {
                        var ir = new InfoReturn(x);

                        if (!ir.Status)
                        {
                            throw new Exception("Unable to find properties for object in order to perform Get() request");
                        }

                        rr.Properties = ir.Results.Where(y => y.IsRetrievable).Select(y => y.Name).ToArray();
                    }
                    else
                    {
                        rr.Properties = x.Props;
                    }
                }

                return(rr);
            }, (client, o) =>
            {
                var retrieveRequest  = new RetrieveRequest1(o[0]);
                var retrieveResponse = client.SoapClient.RetrieveAsync(retrieveRequest).Result;

                return(new ExecuteAPIResponse <APIObject>(retrieveResponse.Results, retrieveResponse.RequestID, retrieveResponse.OverallStatus)
                {
                    OverallStatusMessage = retrieveResponse.OverallStatus
                });
            }, objs);

            if (response != null)
            {
                if (response.Length > 0)
                {
                    if (objs.GetType().ToString().Contains("ET_"))
                    {
                        Results = response.Select(TranslateObject2).ToArray();
                    }
                    else
                    {
                        Results = response.Select(TranslateObject).ToArray();
                    }
                }
                else
                {
                    Results = new APIObject[0];
                }
            }
        }
        public GetReturn(APIObject theObject, Boolean Continue, String OverrideObjectType)
        {
            string OverallStatus = string.Empty, RequestID = string.Empty;
            APIObject[] objectResults = new APIObject[0];
            theObject.AuthStub.refreshToken();
            this.Results = new APIObject[0];
            using (var scope = new OperationContextScope(theObject.AuthStub.soapclient.InnerChannel))
            {
                //Add oAuth token to SOAP header.
                XNamespace ns = "http://exacttarget.com";
                var oauthElement = new XElement(ns + "oAuthToken", theObject.AuthStub.internalAuthToken);
                var xmlHeader = MessageHeader.CreateHeader("oAuth", "http://exacttarget.com", oauthElement);
                OperationContext.Current.OutgoingMessageHeaders.Add(xmlHeader);

                var httpRequest = new System.ServiceModel.Channels.HttpRequestMessageProperty();
                OperationContext.Current.OutgoingMessageProperties.Add(System.ServiceModel.Channels.HttpRequestMessageProperty.Name, httpRequest);
                httpRequest.Headers.Add(HttpRequestHeader.UserAgent, theObject.AuthStub.SDKVersion);

                RetrieveRequest rr = new RetrieveRequest();
                if (Continue)
                {
                    if (theObject.LastRequestID == null)
                    {
                        throw new Exception("Unable to call GetMoreResults without first making successful Get() request");
                    }
                    rr.ContinueRequest = theObject.LastRequestID;
                }
                else
                {
                    if (theObject.SearchFilter != null)
                    {
                        rr.Filter = theObject.SearchFilter;
                    }

                    // Use the name from the object passed in unless an override is passed (Used for DataExtensionObject)
                    if (OverrideObjectType == null)
                        rr.ObjectType = this.TranslateObject(theObject).GetType().ToString().Replace("FuelSDK.", "");
                    else
                        rr.ObjectType = OverrideObjectType;

                    //If they didn't specify Props then we look them up using Info()
                    if (theObject.Props == null && theObject.GetType().GetMethod("Info") != null)
                    {
                        InfoReturn ir = new InfoReturn(theObject);
                        List<string> lProps = new List<string>();
                        if (ir.Status)
                        {
                            foreach (ET_PropertyDefinition pd in ir.Results)
                            {
                                if (pd.IsRetrievable)
                                    lProps.Add(pd.Name);
                            }
                        }
                        else
                        {
                            throw new Exception("Unable to find properties for object in order to perform Get() request");
                        }
                        rr.Properties = lProps.ToArray();
                    }
                    else
                        rr.Properties = theObject.Props;
                }
                OverallStatus = theObject.AuthStub.soapclient.Retrieve(rr, out RequestID, out objectResults);

                this.RequestID = RequestID;

                if (objectResults.Length > 0)
                {
                    List<APIObject> cleanedObjectResults = new List<APIObject>();
                    foreach (APIObject obj in objectResults)
                    {
                        cleanedObjectResults.Add(this.TranslateObject(obj));
                    }
                    this.Results = cleanedObjectResults.ToArray();
                }

                this.Status = true;
                this.Code = 200;
                this.MoreResults = false;
                this.Message = "";

                if (OverallStatus != "OK" && OverallStatus != "MoreDataAvailable")
                {
                    this.Status = false;
                    this.Message = OverallStatus;
                }
                else if (OverallStatus == "MoreDataAvailable")
                {
                    this.MoreResults = true;
                }
            }
        }
        public APIObject TranslateObject(APIObject inputObject)
        {
            if (this.translator.ContainsKey(inputObject.GetType()))
            {
                APIObject returnObject = (APIObject)Activator.CreateInstance(translator[inputObject.GetType()]);

                foreach (PropertyInfo prop in inputObject.GetType().GetProperties())
                {
                    if (prop.PropertyType.IsSubclassOf(typeof(APIObject)) && prop.GetValue(inputObject, null) != null)
                    {
                        prop.SetValue(returnObject, this.TranslateObject(prop.GetValue(inputObject, null)), null);
                    }
                    else if (translator.ContainsKey(prop.PropertyType) && prop.GetValue(inputObject, null) != null)
                    {
                        prop.SetValue(returnObject, this.TranslateObject(prop.GetValue(inputObject, null)), null);
                    }
                    else if (prop.PropertyType.IsArray && prop.GetValue(inputObject, null) != null)
                    {
                        Array a = (Array)prop.GetValue(inputObject, null);
                        Array outArray;

                        if (a.Length > 0)
                        {
                            if (translator.ContainsKey(a.GetValue(0).GetType()))
                            {
                                outArray = Array.CreateInstance(translator[a.GetValue(0).GetType()], a.Length);

                                for (int i = 0; i < a.Length; i++)
                                {
                                    if (translator.ContainsKey(a.GetValue(i).GetType()))
                                    {
                                        outArray.SetValue(TranslateObject(a.GetValue(i)), i);
                                    }
                                }
                                if (outArray.Length > 0)
                                {
                                    prop.SetValue(returnObject, outArray, null);
                                }
                            }
                        }
                    }
                    else if (prop.GetValue(inputObject, null) != null && returnObject.GetType().GetProperty(prop.Name) != null)
                    {
                        prop.SetValue(returnObject, prop.GetValue(inputObject, null), null);
                    }
                }
                return returnObject;

            }
            else
            {
                return inputObject;
            }
        }