Exemplo n.º 1
0
        /// <summary>
        /// Get方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <FacebookResult <V> > GetAsync <T, K, V>(BaseRequest <T, K, V> request)
        {
            var url = "";

            FacebookResult <V> result = new FacebookResult <V>();

            #region 构建请求地址

            _client.DefaultRequestHeaders.Clear();

            _client.DefaultRequestHeaders.Add("Authorization", "Bearer " + request.Token);

            var filedsStr = "?fields=";

            var props = GetPropertyInfoArray <T, K, V>(request);

            filedsStr += props.Select(p => p.Name).Aggregate((x, y) => x + "," + y);

            var parameterDic = ToDictionary(request.Parameter);

            var paraStr = ConcatQueryString(parameterDic);

            if (string.IsNullOrEmpty(paraStr) == false)
            {
                url = "https://graph.facebook.com/v9.0/" + request.Url + filedsStr + "&" + paraStr;
            }
            else
            {
                url = "https://graph.facebook.com/v9.0/" + request.Url + filedsStr;
            }


            #endregion

            var httpResponse = await _client.GetAsync(url);

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

            V obj = JsonConvert.DeserializeObject <V>(content);

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                result.Failed(httpResponse.StatusCode.ToString());
            }
            else
            {
                result.Success(httpResponse.StatusCode.ToString());
            }
            result.Result = obj;

            return(await Task.FromResult(result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Post方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <FacebookResult <T> > PostAsync <T, K, V>(BaseRequest <T, K, V> request)
        {
            FacebookResult <T> result = new FacebookResult <T>();

            _client.DefaultRequestHeaders.Clear();

            _client.DefaultRequestHeaders.Add("Authorization", "Bearer " + request.Token);

            var url = "";

            var filedsStr = "?fields=";

            var props = GetPropertyInfoArray <T, K, V>(request);

            filedsStr += props.Select(p => p.Name).Aggregate((x, y) => x + "," + y);

            url = "https://graph.facebook.com/v9.0/" + request.Url + filedsStr;

            var httpResponse = await _client.PostAsync(url, new JsonContent(new { request.Parameter }));

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

            T obj = JsonConvert.DeserializeObject <T>(content);

            if (httpResponse.StatusCode != HttpStatusCode.OK)
            {
                result.Failed(httpResponse.StatusCode.ToString());
            }
            else
            {
                result.Success(httpResponse.StatusCode.ToString());
            }
            result.Result = obj;

            return(await Task.FromResult(result));
        }