public PostReturn(FuelObject theObject) { this.Message = ""; this.Status = true; this.MoreResults = false; string completeURL = theObject.Endpoint; string additionalQS; theObject.AuthStub.refreshToken(); foreach (PropertyInfo prop in theObject.GetType().GetProperties()) { if (theObject.URLProperties.Contains(prop.Name) && prop.GetValue(theObject, null) != null) if (prop.GetValue(theObject, null).ToString().Trim() != "" && prop.GetValue(theObject, null).ToString().Trim() != "0") completeURL = completeURL.Replace("{" + prop.Name + "}", prop.GetValue(theObject, null).ToString()); } bool match; if (theObject.RequiredURLProperties != null) { foreach (string urlProp in theObject.RequiredURLProperties) { match = false; foreach (PropertyInfo prop in theObject.GetType().GetProperties()) { if (theObject.URLProperties.Contains(prop.Name)) if (prop.GetValue(theObject, null) != null) { if (prop.GetValue(theObject, null).ToString().Trim() != "" && prop.GetValue(theObject, null).ToString().Trim() != "0") match = true; } } if (match == false) throw new Exception("Unable to process request due to missing required property: " + urlProp); } } // Clean up not required URL parameters int j = 0; if (theObject.URLProperties != null) { foreach (string urlProp in theObject.URLProperties) { completeURL = completeURL.Replace("{" + urlProp + "}", ""); j++; } } additionalQS = "access_token=" + theObject.AuthStub.authToken; completeURL = completeURL + "?" + additionalQS; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(completeURL.Trim()); request.Method = "POST"; request.ContentType = "application/json"; request.UserAgent = theObject.AuthStub.SDKVersion; using (var streamWriter = new StreamWriter(request.GetRequestStream())) { string jsonPayload = JsonConvert.SerializeObject(theObject); streamWriter.Write(jsonPayload); } 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; List<ResultDetail> AllResults = new List<ResultDetail>(); if (responseFromServer.ToString().StartsWith("[")) { JArray jsonArray = JArray.Parse(responseFromServer.ToString()); foreach (JObject obj in jsonArray) { APIObject currentObject = (APIObject)Activator.CreateInstance(theObject.GetType(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new object[] { obj }, null); ResultDetail result = new ResultDetail(); result.Object = currentObject; AllResults.Add(result); } this.Results = AllResults.ToArray(); } else { JObject jsonObject = JObject.Parse(responseFromServer.ToString()); ResultDetail result = new ResultDetail(); APIObject currentObject = (APIObject)Activator.CreateInstance(theObject.GetType(), System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new object[] { jsonObject }, null); result.Object = currentObject; AllResults.Add(result); this.Results = AllResults.ToArray(); } } else { this.Status = false; this.Message = response.ToString(); } } response.Close(); } catch (WebException we) { this.Code = (int)((HttpWebResponse)we.Response).StatusCode; this.Status = false; this.Results = new ResultDetail[] { }; using (var stream = we.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) { Message = reader.ReadToEnd(); } } }
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(); } } }
protected APIObject[] processResults(string restResponse, Type fuelType) { List<APIObject> allObjects = new System.Collections.Generic.List<APIObject>(); if (restResponse != null) { if (JsonConvert.DeserializeObject(restResponse.ToString()) != null && JsonConvert.DeserializeObject(restResponse.ToString()).ToString() != "") { if (restResponse.ToString().StartsWith("[")) { JArray jsonArray = JArray.Parse(restResponse.ToString()); foreach (JObject obj in jsonArray) { APIObject currentObject = (APIObject)Activator.CreateInstance(fuelType, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new object[] { obj }, null); allObjects.Add(currentObject); } return allObjects.ToArray(); } else { JObject jsonObject = JObject.Parse(restResponse.ToString()); ResultDetail result = new ResultDetail(); APIObject currentObject = (APIObject)Activator.CreateInstance(fuelType, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance, null, new object[] { jsonObject }, null); allObjects.Add(currentObject); return allObjects.ToArray(); } } else return allObjects.ToArray(); } else return allObjects.ToArray(); }