Example #1
0
        private string GetEndpoint <TEntity>()
        {
            var endpoint = EndpointNameAttribute.GetEndpointNameOfType <TEntity>();

            if (endpoint == null)
            {
                throw new ArgumentException($"nameof(T) has no EndpointName Attribute defined.");
            }
            return(endpoint);
        }
Example #2
0
        public async Task <T> Update <T>(T value) where T : IUniqueID
        {
            var endpoint = EndpointNameAttribute.GetEndpointNameOfType <T>();

            if (endpoint == null)
            {
                return(default(T));
            }

            if (value.id == 0)
            {
                //cannot update a record with id =0 , you probably mean ot Create
                return(default(T));
            }

            var url = $"api/{endpoint}/{value.id}";


            var serializesettings = new JsonSerializerSettings();

            serializesettings.NullValueHandling    = NullValueHandling.Ignore;
            serializesettings.DefaultValueHandling = DefaultValueHandling.Ignore;

            var json = JsonConvert.SerializeObject(value, serializesettings);

            var request = new HttpRequestMessage
            {
                Method     = HttpMethod.Put,
                RequestUri = new Uri($"{BaseURL}{url}"),
                Headers    =
                {
                    { HttpRequestHeader.Authorization.ToString(), $"Token token={apikey}" },
                },
                Content = new StringContent(json, Encoding.UTF8, "application/json")
            };

            var resp = await client.SendAsync(request);

            if (!resp.IsSuccessStatusCode)
            {
                throw new Exception(resp.ReasonPhrase);
            }

            var content = await resp.Content.ReadAsStringAsync();

            JsonSerializerSettings settings = new JsonSerializerSettings
            {
                ContractResolver = new CustomResolver()
            };

            var intermediateObject = JsonConvert.DeserializeObject <SingleRecordResponse <T> >(content, settings);

            return(intermediateObject.item);
        }
Example #3
0
        public async Task <T> GetSelectorsAsync <T>() where T : ISelector
        {
            var endpoint = EndpointNameAttribute.GetEndpointNameOfType <T>();

            if (endpoint == null)
            {
                return(default(T));
            }

            return(await GetApiRequest <T>($"api/selector/{endpoint}"));
        }