コード例 #1
0
        /// <summary>
        /// Converts the given <paramref name="entity"/> to a hubspot data entity.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="preformConversion">This by default performs a data conversion so that the HubSpot "property-value" syntax will be used for serialization. If this parameter is set to false no data conversion is performed (such that a standard object serialization can be performed). </param>
        /// <returns></returns>
        public dynamic ToHubspotDataEntity(IHubSpotEntity entity, bool preformConversion = true)
        {
            _logger.LogDebug("Convert ToHubspotDataEntity");
            dynamic mapped = new ExpandoObject();

            if (preformConversion)
            {
                mapped.Properties = new List <HubspotDataEntityProp>();

                _logger.LogDebug("Use nameValue mapping?: {0}", entity.IsNameValue);

                var allProps = entity.GetType().GetProperties();
                _logger.LogDebug("Have {0} props to map", allProps.Length);

                foreach (var prop in allProps)
                {
                    if (prop.HasIgnoreDataMemberAttribute())
                    {
                        continue;
                    }

                    var propSerializedName = prop.GetPropSerializedName();
                    _logger.LogDebug("Mapping prop: '{0}' with serialization name: '{1}'", prop.Name, propSerializedName);
                    if (prop.Name.Equals("RouteBasePath") || prop.Name.Equals("IsNameValue"))
                    {
                        continue;
                    }

                    // IF we have an complex type on the entity that we are trying to convert, let's NOT get the
                    // string value of it, but simply pass the object along - it will be serialized later as JSON...
                    var propValue = prop.GetValue(entity);
                    var value     = propValue.IsComplexType() ? propValue : propValue?.ToString();
                    var item      = new HubspotDataEntityProp
                    {
                        Property = propSerializedName,
                        Value    = value
                    };

                    if (entity.IsNameValue)
                    {
                        item.Property = null;
                        item.Name     = propSerializedName;
                    }
                    if (item.Value == null)
                    {
                        continue;
                    }

                    mapped.Properties.Add(item);
                }

                _logger.LogDebug("Mapping complete, returning data");
            }
            else
            {
                mapped = entity;
            }

            return(mapped);
        }
コード例 #2
0
        /// <summary>
        /// Send a post request
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="absoluteUriPath"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        protected async Task <T> PostAsync <T>(string absoluteUriPath, IHubSpotEntity entity)
            where T : IHubSpotEntity, new()
        {
            Logger.LogDebug("Post async for uri path: '{0}' with type: '{1}'", absoluteUriPath, entity.GetType().Name);
            var httpMethod = HttpMethod.Post;

            return(await PutOrPost <T>(absoluteUriPath, entity, true));
        }
コード例 #3
0
        /// <summary>
        /// Internal method to allow support for PUT and POST
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="absoluteUriPath"></param>
        /// <param name="entity"></param>
        /// <param name="usePost"></param>
        /// <returns></returns>
        private async Task <T> PutOrPost <T>(string absoluteUriPath, IHubSpotEntity entity, bool usePost)
            where T : IHubSpotEntity, new()
        {
            var json = _serializer.SerializeEntity(entity);

            var data = await SendRequestAsync <T>(
                absoluteUriPath,
                usePost?HttpMethod.Post : HttpMethod.Put,
                json,
                responseData => (T)_serializer.DeserializeEntity <T>(responseData));

            return(data);
        }
コード例 #4
0
        /// <summary>
        /// Add Client or Company to the Hubspot
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public void Add(IHubSpotEntity item)
        {
            var request = new RestRequest(item.GetPath() + @"?hapikey=" + _apiKey, Method.POST);

            request.Method = Method.POST;
            request.AddHeader("Accept", "application/json");
            request.AddParameter("application/json", item.ToJson(), ParameterType.RequestBody);

            var response = _restClient.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                var jsonResponse = JsonConvert.DeserializeObject <Error>(response.Content);
                throw new RestServerException(jsonResponse.message);
            }
        }