Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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}"));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates the endpoint type string from the given enum if possible.
        /// </summary>
        public static string BuildEndpoint(this Enum endpoint)
        {
            Type   type = endpoint.GetType();
            string name = string.Empty;

            // Get the enum endpoint name.
            TypeInfo info = type.GetTypeInfo();
            EndpointNameAttribute enumName = info.GetCustomAttribute <EndpointNameAttribute>();

            if (enumName == null)
            {
                throw new InvalidOperationException($"{type.Name} does not have any EndpointNameAttribute.");
            }

            if (enumName.Name != null)
            {
                name += enumName;
            }

            // Get the endpoint value attribute.
            FieldInfo             fields       = type.GetField(endpoint.ToString());
            EndpointNameAttribute endpointName = fields.GetCustomAttribute <EndpointNameAttribute>();

            if (endpointName == null)
            {
                throw new InvalidOperationException($"{endpoint.ToString()} does not have any EndpointNameAttribute.");
            }

            if (endpointName.Name == null)
            {
                throw new InvalidOperationException($"{endpoint.ToString()} does not give any endpoint name in EndpointNameAttribute.");
            }

            name += endpointName.Name;
            return(name);
        }