//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 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> /// 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 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); }
/// <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 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 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); }
public async Task GetAsJson_WithResponse() { var client = new HttpClientInstance("https://postman-echo.com/get"); var request = BuildRequest(); var response = await client.GetAsync <Response>().ConfigureAwait(false); response.Url.Should().Be("https://postman-echo.com/get"); }
/// <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); }
/// <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); }
//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 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); }
/// <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); }
public async Task <UpdateDefinition> GetLatestReleaseDefinitionAsync() { var uri = GithubUriBuilder.BuildLatestReleaseUri(Configuration.GithubUserame, Configuration.GithubRepositoryName); var response = await HttpClientInstance.GetAsync(uri); }