//fetching a post by slug uses the post list API, so we are getting an EntitySet with one single entry here... /// <summary> /// gets a single blog post by its slug - fetching a post by slug uses the post list API, so /// we are getting an EntitySet with one single entry /// </summary> /// <param name="baseUrl"> /// the web site's address /// </param> /// <param name="slug"> /// the slug of the post to fetch /// </param> public async Task <WordPressEntitySet <BlogPost> > GetPostBySlugAsync(string baseUrl, string slug) { if (string.IsNullOrWhiteSpace(baseUrl)) { throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty"); } if (string.IsNullOrWhiteSpace(slug)) { throw new NullReferenceException($"parameter {nameof(slug)} MUST not be null or empty"); } var response = await HttpClientInstance.GetAsync( baseUrl.GetEntitySetApiUrl(Resource.Posts, 1, 1) .AddParameterToUrl(nameof(slug), slug)) .ConfigureAwait(false); WordPressEntitySet <BlogPost> result; if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <BlogPost>(response.Content, false, this.ThrowSerializationExceptions); } else { result = new WordPressEntitySet <BlogPost>(response.Content, true, this.ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a list of posts from the WordPress site, results can be controlled with the parameters. /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="perPage">how many items per page should be fetched</param> /// <param name="count">max items to be fetched</param> /// <param name="pageNr">used for paged requests</param> /// <param name="categories">array of category ids</param> /// <param name="order">option to sort the result</param> public async Task <WordPressEntitySet <BlogPost> > GetPostsAsync(string baseUrl, int perPage = 10, int count = 10, int pageNr = 1, int[] categories = null, OrderBy order = OrderBy.Date) { WordPressEntitySet <BlogPost> result = null; var additionalParams = new Dictionary <string, string>(); if (categories != null) { additionalParams.Add(Constants.ParameterCategories, categories.ToArrayString()); } var response = await HttpClientInstance .GetAsync(baseUrl.GetEntitySetApiUrl(Resource.Posts, perPage, count, pageNr, order) .AddParametersToUrl(additionalParams)) .ConfigureAwait(false); if (response.IsSuccessStatusCode) { var json = await response.Content.ReadAsStringAsync(); result = new WordPressEntitySet <BlogPost>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntitySet <BlogPost>(response.Content, true, ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a single blog post from the WordPress site by Id /// </summary> /// <param name="baseUrl"> /// the web site's address /// </param> /// <param name="postId"> /// id of the post to fetch /// </param> public async Task <WordPressEntity <BlogPost> > GetPostAsync(string baseUrl, int postId) { if (string.IsNullOrWhiteSpace(baseUrl)) { throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty"); } if (postId == -1 || postId == default) { throw new ArgumentException($"parameter {nameof(postId)} MUST be a valid post id"); } var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(postId)).ConfigureAwait(false); WordPressEntity <BlogPost> result; if (response.IsSuccessStatusCode) { result = new WordPressEntity <BlogPost>(response.Content, false, this.ThrowSerializationExceptions); } else { result = new WordPressEntity <BlogPost>(response.Content, true, this.ThrowSerializationExceptions); } return(result); }
private async Task Login() { try { HubConnection = new HubConnectionBuilder() .WithUrl($"{Constants.BASE_URL}/chat", options => { var stringData = JsonConvert.SerializeObject(new { Username, Password }); options.AccessTokenProvider = async() => { var content = new StringContent(stringData); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await HttpClientInstance.PostAsync($"{Constants.BASE_URL}/api/token", content); response.EnsureSuccessStatusCode(); return(await response.Content.ReadAsStringAsync()); }; }) .Build(); Email = Username; await _navigationService.NavigateAsync(nameof(MainPage)); } catch (Exception ex) { await PageDialogService.DisplayAlertAsync("Connection Error", ex.Message, "OK", null); } }
/// <summary> /// gets a list of posts with the specified tag ids from the WordPress site. results can be /// controlled with the parameters. /// </summary> /// <param name="tagIds"> /// int array of tag ids /// </param> /// <param name="baseUrl"> /// the web site's address /// </param> /// <param name="perPage"> /// how many items per page should be fetched /// </param> /// <param name="count"> /// max items to be fetched /// </param> /// <param name="pageNr"> /// used for paged requests /// </param> /// <param name="categories"> /// array of category ids /// </param> /// <param name="order"> /// option to sort the result /// </param> public async Task <WordPressEntitySet <BlogPost> > GetPostsByTagsAsync(string baseUrl, int[] tagIds, int perPage = 10, int count = 10, int pageNr = 1, OrderBy order = OrderBy.Date) { if (string.IsNullOrWhiteSpace(baseUrl)) { throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty"); } if (tagIds == null || tagIds.Count() == 0) { throw new ArgumentException($"parameter {nameof(tagIds)} MUST not be null or empty and MUST have Count() > 0"); } var response = await HttpClientInstance.GetAsync( baseUrl.GetEntitySetApiUrl(Resource.Posts, perPage, count, pageNr, order) .AddParameterToUrl("tags", tagIds.ToArrayString())) .ConfigureAwait(false); WordPressEntitySet <BlogPost> result; if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <BlogPost>(response.Content, false, this.ThrowSerializationExceptions); } else { result = new WordPressEntitySet <BlogPost>(response.Content, true, this.ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a list of media from the WordPress site, results can be controlled with the parameters. /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="mediaType">the type of media to fetch</param> /// <param name="mimeType">the mime-type of media to fetch</param> /// <param name="perPage">how many items per page should be fetched</param> /// <param name="count">max items to be fetched</param> /// <param name="pageNr">used for paged requests</param> /// <param name="orderby">option to sort ascending or descending</param> /// <param name="order">option to sort the result</param> public async Task <WordPressEntitySet <Media> > GetMediaAsync(string baseUrl, MediaType mediaType = MediaType.All, string mimeType = null, int perPage = 10, int count = 10, int pageNr = 1, OrderBy orderby = OrderBy.Date, Order order = Order.Desc) { WordPressEntitySet <Media> result = null; var mediaParams = new Dictionary <string, string>(); if (mediaType != MediaType.All) { mediaParams.Add(Constants.MediaTypeParameter, mediaType.ToString().ToLower()); } if (!string.IsNullOrEmpty(mimeType)) { mediaParams.Add(Constants.MediaMimeTypeParameter, mimeType); } var response = await HttpClientInstance.GetAsync( baseUrl.GetEntitySetApiUrl(Resource.Media, perPage, count, pageNr, orderby, order) .AddParametersToUrl(mediaParams)) .ConfigureAwait(false); if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <Media>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntitySet <Media>(response.Content, true, ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a list of tags from the WordPress site by search terms, results can be controlled /// with the parameters. /// </summary> /// <param name="baseUrl"> /// the web site's address /// </param> /// <param name="searchTerm"> /// the terms for tag search /// </param> /// <param name="perPage"> /// how many items per page should be fetched /// </param> /// <param name="count"> /// max items to be fetched /// </param> /// <param name="page"> /// </param> /// <param name="pageNr"> /// used for paged requests /// </param> public async Task <WordPressEntitySet <Tag> > SearchTagsAsync(string baseUrl, string searchTerm, int perPage = 20, int count = 20, int page = 1) { if (string.IsNullOrWhiteSpace(baseUrl)) { throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty"); } if (string.IsNullOrWhiteSpace(searchTerm)) { throw new NullReferenceException($"parameter {nameof(searchTerm)} MUST not be null or empty"); } var response = await HttpClientInstance.GetAsync( baseUrl.GetEntitySetApiUrl(Resource.Tags, perPage, count, page, OrderBy.Name) .AddParameterToUrl("search", WebUtility.UrlEncode(searchTerm))) .ConfigureAwait(false); WordPressEntitySet <Tag> result; if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <Tag>(response.Content, false, this.ThrowSerializationExceptions); } else { result = new WordPressEntitySet <Tag>(response.Content, true, this.ThrowSerializationExceptions); } return(result); }
private async Task Login() { try { HubConnection = new HubConnectionBuilder() .WithUrl($"{BaseUrl}/chat", options => { var stringData = JsonConvert.SerializeObject(new { Email, Password }); options.AccessTokenProvider = async() => { var content = new StringContent(stringData); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await HttpClientInstance.PostAsync($"{BaseUrl}/api/token", content); response.EnsureSuccessStatusCode(); return(await response.Content.ReadAsStringAsync()); }; }) .Build(); UpdateConnectionState(ConnectionState.Connected); HubConnection.On <string, string>("newMessage", ReceiveMessage); await HubConnection.StartAsync(); UpdateConnectionState(ConnectionState.Connected); } catch (Exception ex) { UpdateConnectionState(ConnectionState.Disconnected); await PageDialogService.DisplayAlertAsync("Connection Error", ex.Message, "OK", null); } }
/// <summary> /// gets a list of comments from the WordPress site, results can be controlled with the parameters. /// </summary> /// <param name="commentsUrl"> /// the post's comment url (normally returned by the API) /// </param> /// <param name="perPage"> /// how many items per page should be fetched /// </param> /// <param name="pageNr"> /// used for paged requests /// </param> public async Task <WordPressEntitySet <Comment> > GetCommentsAsync(string commentsUrl, int perPage = 50, int pageNr = 1) { if (string.IsNullOrWhiteSpace(commentsUrl)) { throw new NullReferenceException($"parameter {nameof(commentsUrl)} MUST not be null or empty"); } var response = await HttpClientInstance.GetAsync(commentsUrl.AddParametersToUrl( new Dictionary <string, string>() { ["type"] = "comment", ["per_page"] = perPage.ToString(), ["page"] = pageNr.ToString() })).ConfigureAwait(false); WordPressEntitySet <Comment> result; if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <Comment>(response.Content, false, this.ThrowSerializationExceptions); } else { result = new WordPressEntitySet <Comment>(response.Content, true, this.ThrowSerializationExceptions); } return(result); }
/// <summary> /// creates a new comment with an anonymous user. Requires the site to be set up for this /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="authorName">name of the comment author</param> /// <param name="email">email of the comment authro</param> /// <param name="content">comment text</param> /// <param name="postId">the post id this comment should be posted to</param> /// <param name="parentId">id of the comment that receives the response</param> /// <returns></returns> public async Task <WordPressEntity <Comment> > CreateAnonymousComment(string baseUrl, string authorName, string email, string content, long postId, long parentId) { WordPressEntity <Comment> result = null; var postCommentUrl = baseUrl.GetPostApiUrl(Resource.Comments) .AddParameterToUrl("post", postId.ToString()) .AddParameterToUrl("author_name", WebUtility.UrlEncode(authorName)) .AddParameterToUrl("author_email", WebUtility.UrlEncode(email)) .AddParameterToUrl("author_name", WebUtility.UrlEncode(authorName)) .AddParameterToUrl("content", WebUtility.UrlEncode(content)); if (parentId != 0) { postCommentUrl = postCommentUrl.AddParameterToUrl("parent", parentId.ToString()); } var response = await HttpClientInstance.PostAsync(postCommentUrl, null).ConfigureAwait(false); if (response.IsSuccessStatusCode) { result = new WordPressEntity <Comment>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntity <Comment>(response.Content, true, ThrowSerializationExceptions); } return(result); }
public async Task PostAsJson_WithRequest() { var client = new HttpClientInstance("https://postman-echo.com/post"); var request = BuildRequest(); await client.PostAsync(request).ConfigureAwait(false); }
public async Task PostAsJson_WithResponse() { var client = new HttpClientInstance("https://postman-echo.com/post"); var response = await client.PostAsync <Response>().ConfigureAwait(false); response.Url.Should().Be("https://postman-echo.com/post"); }
public async Task DeleteAsJson_WithResponse() { var client = new HttpClientInstance("https://postman-echo.com/delete"); var request = BuildRequest(); var response = await client.DeleteAsync <Response>().ConfigureAwait(false); response.Url.Should().Be("https://postman-echo.com/delete"); }
public async Task GetAsJson_WithRequestAndResponse() { var client = new HttpClientInstance("https://postman-echo.com/get"); var request = BuildRequest(); var response = await client.GetAsync <SimpleRequest, Response>(request).ConfigureAwait(false); response.Url.Should().Be("https://postman-echo.com/get?Foo=Foo&Bar=Bar"); }
/// <summary> /// creates a new comment with an anonymous user. Requires the site to be set up for this /// </summary> /// <param name="baseUrl"> /// the web site's address /// </param> /// <param name="authorName"> /// name of the comment author /// </param> /// <param name="email"> /// email of the comment authro /// </param> /// <param name="content"> /// comment text /// </param> /// <param name="postId"> /// the post id this comment should be posted to /// </param> /// <param name="parentId"> /// id of the comment that receives the response /// </param> /// <returns> /// </returns> public async Task <WordPressEntity <Comment> > CreateAnonymousCommentAsync(string baseUrl, string authorName, string email, string content, long postId, long parentId) { if (string.IsNullOrWhiteSpace(baseUrl)) { throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty"); } if (string.IsNullOrWhiteSpace(authorName)) { throw new NullReferenceException($"parameter {nameof(authorName)} MUST not be null or empty"); } if (string.IsNullOrWhiteSpace(email)) { throw new NullReferenceException($"parameter {nameof(email)} MUST not be null or empty"); } if (string.IsNullOrWhiteSpace(content)) { throw new NullReferenceException($"parameter {nameof(content)} MUST not be null or empty"); } if (postId == -1 || postId == default) { throw new ArgumentException($"parameter {nameof(postId)} MUST be a valid post id"); } var postCommentUrl = baseUrl.GetPostApiUrl(Resource.Comments) .AddParameterToUrl("post", postId.ToString()) .AddParameterToUrl("author_name", WebUtility.UrlEncode(authorName)) .AddParameterToUrl("author_email", WebUtility.UrlEncode(email)) .AddParameterToUrl("author_name", WebUtility.UrlEncode(authorName)) .AddParameterToUrl("content", WebUtility.UrlEncode(content)); if (parentId != 0) { postCommentUrl = postCommentUrl.AddParameterToUrl("parent", parentId.ToString()); } var response = await HttpClientInstance.PostAsync(postCommentUrl, null).ConfigureAwait(false); WordPressEntity <Comment> result; if (response.IsSuccessStatusCode) { result = new WordPressEntity <Comment>(response.Content, false, this.ThrowSerializationExceptions); } else { result = new WordPressEntity <Comment>(response.Content, true, this.ThrowSerializationExceptions); } return(result); }
private static async Task <(bool Success, string Content)> CreateIssue(RedmineIssueWrapper issue) { //var issuesEndPoint = "issues.json"; // Read values from config // https://docs.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings // https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-csharp#environment-variables var config = new CreateRedmineIssueConfiguration(); var headers = new List <KeyValuePair <string, string> >(new KeyValuePair <string, string>[] { new KeyValuePair <string, string>(config.RedmineApiTokenHeaderName, config.RedmineApiTokenValue) }); HttpClientInstance.Init(config.RedmineBaseUrl, headers); var _client = HttpClientInstance.Instance; var resp = await _client.PostAsync(config.RedmineIssuesEndPoint, new StringContent(issue.SerializeToJson(), System.Text.Encoding.UTF8, "application/json")); var success = resp.IsSuccessStatusCode; string ret; if (success) { ret = await resp.Content.ReadAsStringAsync(); } else { ret = resp.ReasonPhrase + ". "; var content = await resp.Content.ReadAsStringAsync(); if (!String.IsNullOrWhiteSpace(content)) { try { dynamic json = JObject.Parse(content); ret += json?.errors[0]; } catch { } } } return(Success : success, Content : ret); }
/// <summary> /// gets a single blog page from the WordPress site by Id /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="postId">id of the page to fetch</param> public async Task <WordPressEntity <Page> > GetPageAsync(string baseUrl, int id) { WordPressEntity <Page> result = null; var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(id, Resource.Pages)); if (response.IsSuccessStatusCode) { result = new WordPressEntity <Page>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntity <Page>(response.Content, true, ThrowSerializationExceptions); } return(result); }
//tag collection can be very very big (my personal blog has over 600...) //just having this in place doesn't mean one should use it... //better using the SearchTags Method to obtain only a subset/one specific tag /// <summary> /// gets a list of tags from the WordPress site, results can be controlled with the parameters. /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="perPage">how many items per page should be fetched</param> /// <param name="count">max items to be fetched</param> /// <param name="pageNr">used for paged requests</param> public async Task <WordPressEntitySet <Tag> > GetTagsAsync(string baseUrl, int perPage = 100, int count = 100, int page = 1) { WordPressEntitySet <Tag> result = null; var response = await HttpClientInstance.GetAsync(baseUrl.GetEntitySetApiUrl(Resource.Tags, perPage, count, page, OrderBy.Name)); if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <Tag>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntitySet <Tag>(response.Content, true, ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a list of categories from the WordPress site, results can be controlled with the parameters. /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="perPage">how many items per page should be fetched</param> /// <param name="count">max items to be fetched</param> /// <param name="pageNr">used for paged requests</param> /// <param name="order">option to sort the result</param> public async Task <WordPressEntitySet <Category> > GetCategoriesAsync(string baseUrl, int perPage = 10, int count = 10, int pageNr = 1, OrderBy order = OrderBy.Date) { WordPressEntitySet <Category> result = null; var response = await HttpClientInstance.GetAsync(baseUrl.GetEntitySetApiUrl(Resource.Categories, perPage, count, pageNr, order)).ConfigureAwait(false); if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <Category>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntitySet <Category>(response.Content, true, ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a medium by id /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="mediaId">the id of the medium to fetch</param> public async Task <WordPressEntity <Media> > GetMediumAsync(string baseUrl, long mediaId) { WordPressEntity <Media> result = null; var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(mediaId, Resource.Media)); if (response.IsSuccessStatusCode) { result = new WordPressEntity <Media>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntity <Media>(response.Content, true, ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a single blog post from the WordPress site by Id /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="postId">id of the post to fetch</param> public async Task <WordPressEntity <BlogPost> > GetPostAsync(string baseUrl, int postId) { WordPressEntity <BlogPost> result = null; var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(postId)).ConfigureAwait(false); if (response.IsSuccessStatusCode) { result = new WordPressEntity <BlogPost>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntity <BlogPost>(response.Content, true, ThrowSerializationExceptions); } return(result); }
private async Task <ValueTuple <HttpResponseMessage, string> > FetchConfigHttpResponseImplAsync(HttpRequestMessage httpRequest) { _logger.Trace()?.Log("Making HTTP request to APM Server... Request: {HttpRequest}.", httpRequest); var httpResponse = await HttpClientInstance.SendAsync(httpRequest, HttpCompletionOption.ResponseContentRead, CtsInstance.Token); // ReSharper disable once InvertIf if (httpResponse == null) { throw new FailedToFetchConfigException("HTTP client API call for request to APM Server returned null." + $" Request:{Environment.NewLine}{TextUtils.Indent(httpRequest.ToString())}"); } _logger.Trace()?.Log("Reading HTTP response body... Response: {HttpResponse}.", httpResponse); var httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); return(httpResponse, httpResponseBody); }
/// <summary> /// gets a list of posts with the specified tag ids from the WordPress site. results can be controlled with the parameters. /// </summary> /// <param name="tagIds">int array of tag ids</param> /// <param name="baseUrl">the web site's address</param> /// <param name="perPage">how many items per page should be fetched</param> /// <param name="count">max items to be fetched</param> /// <param name="pageNr">used for paged requests</param> /// <param name="categories">array of category ids</param> /// <param name="order">option to sort the result</param> public async Task <WordPressEntitySet <BlogPost> > GetPostsByTagsAsync(string baseUrl, int[] tagIds, int perPage = 10, int count = 10, int pageNr = 1, OrderBy order = OrderBy.Date) { WordPressEntitySet <BlogPost> result = null; var response = await HttpClientInstance.GetAsync( baseUrl.GetEntitySetApiUrl(Resource.Posts, perPage, count, pageNr, order) .AddParameterToUrl("tags", tagIds.ToArrayString())) .ConfigureAwait(false); if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <BlogPost>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntitySet <BlogPost>(response.Content, true, ThrowSerializationExceptions); } return(result); }
private static async Task <(bool Success, string ResponseMessage, RedmineFullIssue CreatedIssue)> CreateIssue(DtoIssue issue) { // TODO: Call CreateRedmineIssue HttpClientInstance.Init(_config.CreateRedmineIssueUrl, null); var _client = HttpClientInstance.Instance; var resp = await _client.PostAsJsonAsync(_config.CreateRedmineIssueEndPoint, issue); var success = resp.IsSuccessStatusCode; string json = null; if (success) { json = await resp.Content.ReadAsStringAsync(); if (!String.IsNullOrWhiteSpace(json)) { try { var ret = JsonConvert.DeserializeObject <RedmineFullIssue>(json); return(Success : success, ResponseMessage : "Successful", CreatedIssue : ret); } catch (Exception ex) { success = false; return(Success : success, ResponseMessage : ex.Message, CreatedIssue : null); } } else { success = false; return(Success : success, ResponseMessage : "Empty Content from Redmine.", CreatedIssue : null); } } else { var error = await resp.Content.ReadAsStringAsync(); return(Success : success, ResponseMessage : error, CreatedIssue : null); } }
/// <summary> /// gets a list of tags from the WordPress site by search terms, results can be controlled with the parameters. /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="searchTerm">the terms for tag search</param> /// <param name="perPage">how many items per page should be fetched</param> /// <param name="count">max items to be fetched</param> /// <param name="page"></param> /// <param name="pageNr">used for paged requests</param> public async Task <WordPressEntitySet <Tag> > SearchTagsAsync(string baseUrl, string searchTerm, int perPage = 20, int count = 20, int page = 1) { WordPressEntitySet <Tag> result = null; var response = await HttpClientInstance.GetAsync( baseUrl.GetEntitySetApiUrl(Resource.Tags, perPage, count, page, OrderBy.Name) .AddParameterToUrl("search", WebUtility.UrlEncode(searchTerm))) .ConfigureAwait(false); if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <Tag>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntitySet <Tag>(response.Content, true, ThrowSerializationExceptions); } return(result); }
//fetching a post by slug uses the post list API, so we are getting an EntitySet with one single entry here... /// <summary> /// gets a single blog post by its slug - fetching a post by slug uses the post list API, so we are getting an EntitySet with one single entry /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="slug">the slug of the post to fetch</param> public async Task <WordPressEntitySet <BlogPost> > GetPostBySlugAsync(string baseUrl, string slug) { WordPressEntitySet <BlogPost> result = null; var response = await HttpClientInstance.GetAsync( baseUrl.GetEntitySetApiUrl(Resource.Posts, 1, 1) .AddParameterToUrl(nameof(slug), slug)) .ConfigureAwait(false); if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <BlogPost>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntitySet <BlogPost>(response.Content, true, ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a single category from the WordPress site by Id /// </summary> /// <param name="baseUrl">the web site's address</param> /// <param name="postId">id of the post to fetch</param> public async Task <WordPressEntity <Category> > GetCategoryAsync(string baseUrl, int categoryId) { WordPressEntity <Category> result = null; var response = await HttpClientInstance.GetAsync(baseUrl.GetEntityApiUrl(categoryId)).ConfigureAwait(false); if (response.IsSuccessStatusCode) { var responseJson = await response.Content.ReadAsStringAsync(); result = new WordPressEntity <Category>(response.Content, false, ThrowSerializationExceptions); } else { var errorJson = await response.Content.ReadAsStringAsync(); result = new WordPressEntity <Category>(response.Content, true, ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a list of posts with the specified search terms from the WordPress site. results /// can be controlled with the parameters. /// </summary> /// <param name="baseUrl"> /// the web site's address /// </param> /// <param name="searchTerms"> /// search terms for blog search /// </param> /// <param name="perPage"> /// how many items per page should be fetched /// </param> /// <param name="count"> /// max items to be fetched /// </param> /// <param name="pageNr"> /// used for paged requests /// </param> /// <param name="categories"> /// array of category ids /// </param> /// <param name="order"> /// option to sort the result /// </param> public async Task <WordPressEntitySet <BlogPost> > SearchPostsAsync(string baseUrl, string searchTerms, int perPage = 10, int count = 10, int pageNr = 1, int[]?categories = null, OrderBy order = OrderBy.Date) { if (string.IsNullOrWhiteSpace(baseUrl)) { throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty"); } if (string.IsNullOrWhiteSpace(searchTerms)) { throw new NullReferenceException($"parameter {nameof(searchTerms)} MUST not be null or empty"); } var additionalParams = new Dictionary <string, string>() { ["search"] = WebUtility.UrlEncode(searchTerms), }; if (categories != null) { additionalParams.Add(Constants.ParameterCategories, categories.ToArrayString()); } var response = await HttpClientInstance.GetAsync( baseUrl.GetEntitySetApiUrl(Resource.Posts, perPage, count, pageNr, order) .AddParametersToUrl(additionalParams)) .ConfigureAwait(false); WordPressEntitySet <BlogPost> result; if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <BlogPost>(response.Content, false, this.ThrowSerializationExceptions); } else { result = new WordPressEntitySet <BlogPost>(response.Content, true, this.ThrowSerializationExceptions); } return(result); }
//tag collection can be very very big (my personal blog has over 600...) //just having this in place doesn't mean one should use it... //better using the SearchTags Method to obtain only a subset/one specific tag #region Public Methods /// <summary> /// gets a list of tags from the WordPress site, results can be controlled with the parameters. /// </summary> /// <param name="baseUrl"> /// the web site's address /// </param> /// <param name="perPage"> /// how many items per page should be fetched /// </param> /// <param name="count"> /// max items to be fetched /// </param> /// <param name="pageNr"> /// used for paged requests /// </param> public async Task <WordPressEntitySet <Tag> > GetTagsAsync(string baseUrl, int perPage = 100, int count = 100, int page = 1) { if (string.IsNullOrWhiteSpace(baseUrl)) { throw new NullReferenceException($"parameter {nameof(baseUrl)} MUST not be null or empty"); } var response = await HttpClientInstance.GetAsync(baseUrl.GetEntitySetApiUrl(Resource.Tags, perPage, count, page, OrderBy.Name)); WordPressEntitySet <Tag> result; if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <Tag>(response.Content, false, this.ThrowSerializationExceptions); } else { result = new WordPressEntitySet <Tag>(response.Content, true, this.ThrowSerializationExceptions); } return(result); }
/// <summary> /// gets a list of comments from the WordPress site, results can be controlled with the parameters. /// </summary> /// <param name="commentsUrl">the post's comment url (normally returned by the API)</param> /// <param name="perPage">how many items per page should be fetched</param> /// <param name="pageNr">used for paged requests</param> public async Task <WordPressEntitySet <Comment> > GetCommentsAsync(string commentsUrl, int perPage = 50, int pageNr = 1) { WordPressEntitySet <Comment> result = null; var response = await HttpClientInstance.GetAsync(commentsUrl.AddParametersToUrl( new Dictionary <string, string>() { ["type"] = "comment", ["per_page"] = perPage.ToString(), ["page"] = pageNr.ToString() })).ConfigureAwait(false); if (response.IsSuccessStatusCode) { result = new WordPressEntitySet <Comment>(response.Content, false, ThrowSerializationExceptions); } else { result = new WordPressEntitySet <Comment>(response.Content, true, ThrowSerializationExceptions); } return(result); }