Add basic functionality to pull purchased products

This commit is contained in:
James Skemp 2022-09-10 18:27:55 -05:00
parent d54c520a10
commit 5257d2cc86
4 changed files with 114 additions and 4 deletions

View File

@ -41,4 +41,49 @@ public class ApiClient
return false;
}
public ApiProductResponse GetProducts(int page = 1, int perPage = 15) {
// TODO validate access token
var headers = httpClient.DefaultRequestHeaders;
headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", GetAccessToken());
var queryParameters = new Dictionary<string, string> {
{ "page", page.ToString() },
{ "per_page", perPage.ToString() }
};
var dictFormUrlEncoded = new FormUrlEncodedContent(queryParameters);
var queryString = dictFormUrlEncoded.ReadAsStringAsync().Result;
var request = new HttpRequestMessage(HttpMethod.Get, apiBaseUrl + $"customers/{apiTokenResponse.CustomersId}/products?{queryString}");
var response = httpClient.Send(request);
if (!response.IsSuccessStatusCode) {
throw new Exception($"DriveThruRPG API failed to get products: {(int)response.StatusCode} {response.ReasonPhrase}");
}
//Console.WriteLine(JsonSerializer.Serialize(response));
//Console.WriteLine(response.Content.ReadAsStringAsync().Result);
var productResponse = JsonSerializer.Deserialize<ApiProductResponse>(response.Content.ReadAsStringAsync().Result);
if (productResponse != null && productResponse.Message != null) {
return productResponse;
}
return null;
}
public string GetCustomerId() {
if (apiTokenResponse == null) {
return "";
}
return apiTokenResponse.CustomersId;
}
private string GetAccessToken() {
return apiTokenResponse?.AccessToken;
}
}

View File

@ -0,0 +1,26 @@
using System.Text.Json.Serialization;
namespace DriveThruRpgApi.Models
{
public class ApiProductResponse
{
[JsonPropertyName("status")]
public string? Status { get; set; }
[JsonPropertyName("message")]
public List<ApiProductMessageResponse>? Message { get; set; }
}
public class ApiProductMessageResponse
{
[JsonPropertyName("products_id")]
public string? ProductsId { get; set; }
[JsonPropertyName("products_name")]
public string? ProductsName { get; set; }
[JsonPropertyName("is_archived")]
public string? IsArchived { get; set; }
[JsonPropertyName("cover_url")]
public string? CoverUrl { get; set; }
[JsonPropertyName("date_purchased")]
public string? DatePurchased { get; set; }
}
}

View File

@ -1,4 +1,6 @@
using Microsoft.Extensions.Configuration;
using System.Text.Json;
using DriveThruRpgApi.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
@ -28,6 +30,26 @@ if (!apiClient.GetToken()) {
Console.WriteLine("Unable to get DriveThruRPG access token.");
}
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
Console.WriteLine(apiApplicationKey);
var getProducts = true;
var startPage = 1;
var productsPerPage = 25;
var products = new List<ApiProductMessageResponse>();
while (getProducts) {
var pulledProducts = apiClient.GetProducts(startPage, productsPerPage);
if (pulledProducts != null && pulledProducts.Message != null && pulledProducts.Message.Any()) {
products.AddRange(pulledProducts.Message);
startPage++;
} else {
getProducts = false;
}
if (startPage >= 5) {
getProducts = false;
}
}
Console.WriteLine(JsonSerializer.Serialize(products));
Console.WriteLine("Application complete. Press Enter to exit.");
_ = Console.ReadLine();

View File

@ -4,4 +4,21 @@ This is a .NET 6 console application for working with the DriveThruRPG API.
## Resources
- [DriveThruRPG](https://www.drivethrurpg.com/)
- [Create an Application Key in Account Settings](https://www.drivethrurpg.com/account_edit.php)
- https://github.com/jramboz/DTRPG_API
- https://github.com/glujan/drpg
## Building
- Requires [the .NET 6 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) for your operating system of choice.
- Run `dotnet build` or `dotnet publish` from the **DriveThruRpgApi** directory.
## Running
- Update `DriveThruRpgApplicationKey` in appsettings.json with the Application Key you've created on DriveThruRPG. If you do not want to enter an application key in this file, you will be prompted for one when the application starts.
- Run `dotnet run` from the **DriveThruRpgApi** directory.
- Or `dotnet publish` and run the application from the published directory.
## Another option
From a web browser, when logged in, you can also access the following page and copy the returned JSON. This should contain all your purchased products, with some handy links that include order number information.
```
https://www.drivethrurpg.com/api/products/mylibrary/search?show_all=1&draw=9&columns[0][data]=null&columns[0][name]=&columns[0][searchable]=false&columns[0][orderable]=false&columns[0][search][value]=&columns[0][search][regex]=false&columns[1][data]=product.title&columns[1][name]=name&columns[1][searchable]=true&columns[1][orderable]=true&columns[1][search][value]=&columns[1][search][regex]=false&order[0][column]=1&order[0][dir]=asc&start=0&length=-1&search[value]=&search[regex]=false&show_archived=false&show_all=0&show_new=&show_updated=0&filter_string=&oneclick=true
```