private static async Task <int> Main(string[] args) { CookieContainer container = new CookieContainer(); CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); WordpressClient client = new WordpressClient("https://www.example.com/wp-json/", maxConcurrentRequestsPerInstance: 8, timeout: 60) .WithCookieContainer(ref container) .WithActivityCallback((type) => { Console.WriteLine(type); return(Task.CompletedTask); }) .WithJsonSerializerSetting(new JsonSerializerSettings() { ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor, MissingMemberHandling = MissingMemberHandling.Ignore }) .WithGlobalResponseProcessor((response) => { Console.WriteLine(response); return(true); }); redo: WordpressAuthorization auth = new WordpressAuthorization("username", "password", WordpressClient.AuthorizationType.Jwt); if (await client.IsLoggedInAsync()) { Console.WriteLine("Already logged in."); } else { if (await client.LoginAsync(auth).ConfigureAwait(false)) { Console.WriteLine("Logged in now"); } } var currentUser = await client.GetCurrentUserAsync((builder) => builder.Create()); Console.WriteLine(currentUser.Message); Console.ReadKey(); goto redo; Console.ReadKey(); return(0); }
/// <summary> /// Sets the featured image of the post by first uploading the image on the path and then using its returned imageId /// </summary> /// <param name="client"></param> /// <param name="imagePath"></param> /// <returns></returns> public async Task <PostBuilder> WithFeaturedImage(WordpressClient client, string imagePath) { if (client == null) { throw new ArgumentNullException(nameof(client)); } if (string.IsNullOrEmpty(imagePath)) { throw new ArgumentNullException(nameof(imagePath)); } if (!File.Exists(imagePath)) { throw new FileNotFoundException(nameof(imagePath)); } Responses.Response <Responses.Media> featuredMedia = await client.CreateMediaAsync((builder) => builder.WithHttpBody <MediaBuilder, HttpContent>((media) => media.WithFile(imagePath).Create()).Create()); FeaturedImageId = featuredMedia.Status ? featuredMedia.Value.Id : 0; return(this); }