1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
pub mod blob;
pub mod errors;
pub mod manifest;
use blob::Blob;
use errors::{ErrorList, ErrorResponse};
use manifest::{Manifest, ManifestList};
use reqwest::{Method, StatusCode};
static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
#[derive(Clone, Debug)]
pub struct DockerRegistryClientV2 {
service: String,
api_url: String,
oauth_url: String,
auth_token: Option<AuthToken>,
client: reqwest::Client,
}
#[derive(serde::Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Version {}
const MEDIA_TYPE_JSON: &str = "applicatin/json";
const MEDIA_TYPE_MANIFEST_LIST_V2: &str =
"application/vnd.docker.distribution.manifest.list.v2+json";
const MEDIA_TYPE_MANIFEST_V2: &str = "application/vnd.docker.distribution.manifest.v2+json";
impl DockerRegistryClientV2 {
pub fn new<T: Into<String>>(service: T, api_url: T, oauth_url: T) -> Self {
let client = reqwest::Client::builder()
.user_agent(USER_AGENT)
.build()
.unwrap();
Self {
service: service.into(),
api_url: api_url.into(),
oauth_url: oauth_url.into(),
auth_token: None,
client,
}
}
pub fn set_auth_token(&mut self, token: Option<AuthToken>) {
self.auth_token = token;
}
pub async fn auth(
&self,
r#type: &str,
name: &str,
action: &str,
) -> Result<AuthToken, ErrorResponse> {
let response = self
.client
.get(&self.oauth_url)
.query(&[
("service", self.service.clone()),
("scope", format!("{}:{}:{}", r#type, name, action)),
])
.send()
.await?;
match response.status() {
StatusCode::OK => Ok(response.json::<AuthToken>().await?),
_ => Err(ErrorResponse::APIError(response.json::<ErrorList>().await?)),
}
}
pub async fn version(&self) -> Result<Version, ErrorResponse> {
let url = format!("{}/v2", self.api_url);
self.request(Method::GET, &url, MEDIA_TYPE_JSON).await
}
pub async fn list_manifests(
&self,
image: &str,
reference: &str,
) -> Result<ManifestList, ErrorResponse> {
let url = format!("{}/v2/{}/manifests/{}", &self.api_url, image, reference);
self.request(Method::GET, &url, MEDIA_TYPE_MANIFEST_LIST_V2)
.await
}
pub async fn manifest(&self, image: &str, reference: &str) -> Result<Manifest, ErrorResponse> {
let url = format!("{}/v2/{}/manifests/{}", &self.api_url, image, reference);
self.request(Method::GET, &url, MEDIA_TYPE_MANIFEST_V2)
.await
}
pub async fn blob(&self, image: &str, digest: &str) -> Result<Blob, ErrorResponse> {
let url = format!("{}/v2/{}/blobs/{}", &self.api_url, image, digest);
let mut request = self.client.get(&url);
if let Some(token) = self.auth_token.clone() {
request = request.bearer_auth(token.access_token);
}
let response = request.send().await?;
match response.status() {
StatusCode::OK => Ok(Blob::from(response)),
_ => {
println!("{:?}", response.status());
Err(ErrorResponse::APIError(response.json::<ErrorList>().await?))
}
}
}
async fn request<T: serde::de::DeserializeOwned>(
&self,
method: Method,
url: &str,
accept: &str,
) -> Result<T, ErrorResponse> {
let mut request = self
.client
.request(method, url)
.header(reqwest::header::ACCEPT, accept);
if let Some(token) = self.auth_token.clone() {
request = request.bearer_auth(token.access_token);
}
let response = request.send().await?;
println!("{}: {:?}", url, response.status());
match response.status() {
StatusCode::OK => Ok(response.json::<T>().await?),
_ => Err(ErrorResponse::APIError(response.json::<ErrorList>().await?)),
}
}
}
#[derive(serde::Deserialize, Clone, Debug)]
pub struct AuthToken {
access_token: String,
expires_in: i32,
issued_at: String,
}