hangar_api/
api.rs

1use constcat::concat;
2use serde::{Deserialize, Serialize};
3use typed_builder::TypedBuilder;
4
5use crate::object::*;
6
7/// base url for normal api calls
8const BASE_API_URL: &str = "https://hangar.papermc.io/api/v1";
9
10/// Trait implemented on all request structs.
11pub trait HangarRequest {
12	/// Gets the URL this request should be sent to.
13	fn url(&self) -> String;
14}
15
16/// Searches all the projects on Hangar, or for a single user. Requires the `view_public_info` permission.
17#[derive(Debug, Default, Serialize, TypedBuilder)]
18#[serde(rename_all = "camelCase")]
19#[builder(field_defaults(default, setter(into)))]
20pub struct ProjectsRequest {
21	/// Whether to prioritize the project with an exact name match if present
22	prioritize_exact_match: Option<bool>,
23	/// Pagination information
24	#[builder(!default)]
25	#[serde(flatten)]
26	pagination: Pagination,
27	/// Used to sort the result
28	sort: Option<ProjectsSort>,
29	/// A category to filter for
30	category: Option<Category>,
31	/// A platform to filter for
32	platform: Option<Platform>,
33	/// The author of the project
34	owner: Option<String>,
35	/// The query to use when searching
36	query: Option<String>,
37	/// A license to filter for
38	license: Option<String>,
39	/// A platform version to filter for
40	version: Option<String>,
41	/// A tag to filter for
42	tag: Option<String>,
43	/// The member of the project
44	member: Option<String>,
45}
46
47impl HangarRequest for ProjectsRequest {
48	fn url(&self) -> String {
49		concat!(BASE_API_URL, "/projects").to_string()
50	}
51}
52
53#[derive(Debug, Deserialize)]
54pub struct ProjectsResponse {
55	pub pagination: PaginationResponse,
56	pub result: Vec<Project>,
57}
58
59#[derive(Debug, Deserialize)]
60pub struct PaginationResponse {
61	/// The maximum amount of items to return
62	pub limit: i64,
63	/// Where to start searching
64	pub offset: i64,
65	/// The total number of records
66	pub count: i64,
67}
68
69/// Returns info on a specific project. Requires the `view_public_info` permission.
70#[derive(Debug, Serialize, TypedBuilder)]
71#[builder(field_defaults(setter(into)))]
72pub struct ProjectRequest {
73	/// The slug of the project to return
74	#[serde(skip)]
75	pub slug: String,
76}
77
78impl HangarRequest for ProjectRequest {
79	fn url(&self) -> String {
80		format!("{}/projects/{}", BASE_API_URL, self.slug)
81	}
82}
83
84/// Returns a page of a project. Requires visibility of the page.
85#[derive(Debug, Serialize, TypedBuilder)]
86#[builder(field_defaults(setter(into)))]
87pub struct PageRequest {
88	/// The slug of the project to return the page for
89	#[serde(skip)]
90	pub slug: String,
91	/// The path of the page
92	pub path: String,
93}
94
95impl HangarRequest for PageRequest {
96	fn url(&self) -> String {
97		format!("{}/pages/page/{}", BASE_API_URL, self.slug)
98	}
99}
100
101/// Returns all versions of a project. Requires the `view_public_info` permission in the project or owning organization.
102#[derive(Debug, Serialize, TypedBuilder)]
103#[builder(field_defaults(default, setter(into)))]
104#[serde(rename_all = "camelCase")]
105pub struct VersionsRequest {
106	/// The slug of the project to return versions for
107	#[builder(!default)]
108	#[serde(skip)]
109	pub slug: String,
110	#[builder(!default)]
111	#[serde(flatten)]
112	pub pagination: Pagination,
113	/// Whether to include hidden-by-default channels in the result, defaults to try
114	pub include_hidden_channels: Option<bool>,
115	/// A name of a version channel to filter for
116	pub channel: Option<String>,
117	/// A platform name to filter for
118	pub platform: Option<Platform>,
119	/// A platform version to filter for
120	pub platform_version: Option<String>,
121}
122
123impl HangarRequest for VersionsRequest {
124	fn url(&self) -> String {
125		format!("{}/projects/{}/versions", BASE_API_URL, self.slug)
126	}
127}
128
129#[derive(Debug, Deserialize)]
130pub struct VersionsResponse {
131	pub pagination: PaginationResponse,
132	pub result: Vec<Version>,
133}
134
135/// Returns a specific version of a project. Requires the `view_public_info` permission in the project or owning organization.
136#[derive(Debug, Serialize, TypedBuilder)]
137pub struct VersionRequest {
138	/// The slug of the project to return the version for
139	#[serde(skip)]
140	pub slug: String,
141	/// The name of the version to return
142	#[serde(skip)]
143	pub name: String,
144}
145
146impl HangarRequest for VersionRequest {
147	fn url(&self) -> String {
148		format!(
149			"{}/projects/{}/versions/{}",
150			BASE_API_URL, self.slug, self.name
151		)
152	}
153}