Exemplo n.º 1
0
        public async Task<RestResponse> Execute(RestRequest request, bool errorOnNonSuccess = true)
        {
            var address = new Uri(BaseUrl + "/" + request.Url);

            var message = new HttpRequestMessage(request.Method, address);

            message.Headers.Add("User-Agent", UserAgent);

            // Add the request headers
            foreach (var header in request.Headers) {
                message.Headers.Add(header.Key, header.Value);
            }

            // Add the URL-encoded data
            if (request.Parameters.Count > 0)
            {
                message.Content = new FormUrlEncodedContent(request.Parameters);
            }

            var response = await _client.SendAsync(message);

            if (errorOnNonSuccess)
            {
                response.EnsureSuccessStatusCode();
            }

            return new RestResponse(response);
        }
Exemplo n.º 2
0
        public async Task<RestResponse> ExecuteTextRequest(RestRequest request, bool errorOnNonSuccess = true)
        {
            var client = GenerateDiffbotClient();

            var response = await client.Execute(request, errorOnNonSuccess);

            return response;
        }
Exemplo n.º 3
0
        public async Task<ReadingList> GetReadingList()
        {
            var request = new RestRequest("get");

            var response = await Execute(request);

            var json = await response.HttpResponseMessage.Content.ReadAsStringAsync();

            return new ReadingList(json);
        }
Exemplo n.º 4
0
        public async Task<RestResponse> Execute(RestRequest request, bool errorOnNonSuccess = true)
        {
            var client = GenerateClient();

            AddDefaultParameters(ref request);

            var response = await client.Execute(request, errorOnNonSuccess);

            return response;
        }
Exemplo n.º 5
0
        private void AddDefaultParameters(ref RestRequest request)
        {
            request.Method = HttpMethod.Post;

            request.Parameters["username"] = Username;
            request.Parameters["password"] = Password;

            request.Parameters["apikey"] =  API_KEY;

            request.Headers["Accept"] = "*/*";
        }
Exemplo n.º 6
0
        public async Task<DiffbotArticle> GetText(string url)
        {
            var queryString = string.Format("?token={0}&url={1}&html=true&tags=true", DIFFBOT_TOKEN, url);

            var request = new RestRequest("article" + queryString);

            var result = await ExecuteTextRequest(request, false);

            var content = await result.HttpResponseMessage.Content.ReadAsStringAsync();

            return new DiffbotArticle(content);
        }
Exemplo n.º 7
0
        public async Task<bool> CreateAccount()
        {
            var request = new RestRequest("signup");

            request.Parameters["username"] = Username;
            request.Parameters["password"] = Password;

            //if (response.Result.StatusCode == HttpStatusCode.Forbidden && 
            //    response.Result.Headers.Contains("X-Error"))
            //{
            //    // Alert if the username was already taken
            //}

            var response = await Execute(request, false);
            
            return response.HttpResponseMessage.StatusCode == HttpStatusCode.OK;
        }
Exemplo n.º 8
0
        public async Task<bool> VerifyCredentials()
        {
            var request = new RestRequest("auth");

            request.Parameters["username"] = Username;
            request.Parameters["password"] = Password;

            var response = await Execute(request);
            
            return response.HttpResponseMessage.StatusCode == HttpStatusCode.OK;
        }