Пример #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.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;
                }
            }
        }
Пример #3
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];
                }
            }
        }
        public DeleteReturn(APIObject theObject)
        {
            string OverallStatus = string.Empty, RequestID = string.Empty;
            Result[] requestResults = new Result[0];

            theObject.AuthStub.refreshToken();
            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);
                theObject = this.TranslateObject(theObject);
                requestResults = theObject.AuthStub.soapclient.Delete(new DeleteOptions(), new APIObject[] { theObject }, out RequestID, out OverallStatus);

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

                if (OverallStatus != "OK")
                {
                    this.Status = false;
                }

                if (requestResults.GetType() == typeof(DeleteResult[]) && requestResults.Length > 0)
                {
                    List<ResultDetail> results = new List<ResultDetail>();
                    foreach (DeleteResult cr in requestResults)
                    {
                        ResultDetail detail = new ResultDetail();
                        if (cr.StatusCode != null)
                            detail.StatusCode = cr.StatusCode;
                        if (cr.StatusMessage != null)
                            detail.StatusMessage = cr.StatusMessage;
                        if (cr.Object != null)
                            detail.Object = this.TranslateObject(cr.Object);
                        detail.OrdinalID = cr.OrdinalID;
                        detail.ErrorCode = cr.ErrorCode;
                        results.Add(detail);
                    }
                    this.Results = results.ToArray();
                }
            }
        }
Пример #5
0
        /// <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];
                }
            }
        }
Пример #6
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);
 }
Пример #7
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];
                }
            }
        }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:FuelSDK.GetReturn"/> class.
 /// </summary>
 /// <param name="objs">Objects.</param>
 public GetReturn(APIObject objs) : this(objs, false, null)
 {
 }
 public SendReturn(APIObject theObject)
     : base(theObject)
 {
 }
        public InfoReturn(APIObject theObject)
        {
            string RequestID = string.Empty;
            theObject.AuthStub.refreshToken();
            this.Results = new ET_PropertyDefinition[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);

                ObjectDefinitionRequest odr = new ObjectDefinitionRequest();
                odr.ObjectType = this.TranslateObject(theObject).GetType().ToString().Replace("FuelSDK.", "");
                ObjectDefinition[] definitionResults = theObject.AuthStub.soapclient.Describe(new ObjectDefinitionRequest[] { odr }, out RequestID);

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

                if (definitionResults.Length > 0)
                {

                    List<ET_PropertyDefinition> cleanedObjectResults = new List<ET_PropertyDefinition>();
                    foreach (PropertyDefinition obj in definitionResults[0].Properties)
                    {
                        cleanedObjectResults.Add((ET_PropertyDefinition)(this.TranslateObject(obj)));
                    }
                    this.Results = cleanedObjectResults.ToArray();
                }
                else
                {
                    this.Status = false;
                }
            }
        }
Пример #11
0
 public HelperReturn(APIObject obj)
     : base(obj)
 {
 }
        private void restGet(ref FuelObject theObject, string url)
        {
            //Build the request
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.Trim());
            request.Method = "GET";
            request.ContentType = "application/json";
            request.UserAgent = theObject.AuthStub.SDKVersion;

            //Get the response
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Stream dataStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(dataStream);
                string responseFromServer = reader.ReadToEnd();
                reader.Close();
                dataStream.Close();

                if (response != null)
                    this.Code = (int)response.StatusCode;
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        this.Status = true;
                        if (responseFromServer != null)
                        {
                            JObject parsedResponse = JObject.Parse(responseFromServer);
                            //Check on the paging information from response
                            if (parsedResponse["page"] != null)
                            {
                                this.LastPageNumber = int.Parse(parsedResponse["page"].Value<string>().Trim());
                                int pageSize = int.Parse(parsedResponse["pageSize"].Value<string>().Trim());

                                int count = -1;
                                if (parsedResponse["count"] != null)
                                {
                                    count = int.Parse(parsedResponse["count"].Value<string>().Trim());
                                }
                                else if (parsedResponse["totalCount"] != null)
                                {
                                    count = int.Parse(parsedResponse["totalCount"].Value<string>().Trim());
                                }

                                if (count != -1 && (count > (this.LastPageNumber * pageSize)))
                                {
                                    this.MoreResults = true;
                                }
                            }

                            APIObject[] getResults = new APIObject[] { };

                            if (parsedResponse["items"] != null)
                                getResults = processResults(parsedResponse["items"].ToString().Trim(), theObject.GetType());
                            else if (parsedResponse["entities"] != null)
                                getResults = processResults(parsedResponse["entities"].ToString().Trim(), theObject.GetType());
                            else
                                getResults = processResults(responseFromServer.Trim(), theObject.GetType());

                            this.Results = getResults.ToArray();
                        }
                    }
                    else
                    {
                        this.Status = false;
                        this.Message = response.ToString();
                    }
                }
                response.Close();
            }
            catch (WebException we)
            {
                this.Code = (int)((HttpWebResponse)we.Response).StatusCode;
                this.Status = false;
                using (var stream = we.Response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    this.Message = reader.ReadToEnd();
                }
            }
        }
        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 GetReturn(APIObject theObject)
     : this(theObject, false, null)
 {
 }
        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;
            }
        }
 public HelperReturn(APIObject theObject)
     : base(theObject)
 {
 }
Пример #17
0
 public SendReturn(APIObject obj)
     : base(obj)
 {
 }