Skip to content

Commit 5fe1e53

Browse files
committed
Download rest specs from tarball
This commit updates howrest specs are downloaded by streaming the tarball for a specific branch, and extracting rest specs from the tarball. This is much faster than downloading separate files,
1 parent 9dd32b4 commit 5fe1e53

File tree

5 files changed

+48
-242
lines changed

5 files changed

+48
-242
lines changed

api_generator/Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,21 @@ publish = false
1111
array_tool = "1.0.3"
1212
dialoguer = "0.3.0"
1313
failure = "0.1.5"
14+
flate2 = "~1"
15+
globset = "~0.4"
1416
Inflector = "0.11.4"
1517
indicatif = "0.12.0"
1618
lazy_static = "1.4.0"
1719
quote = "~0.3"
1820
reduce = "0.1.2"
1921
regex = "1.3.1"
20-
reqwest = { version = "~0.10", features = ["blocking", "json", "gzip"] }
22+
reqwest = "~0.9"
2123
rustfmt-nightly = "1.4.17"
2224
semver = "0.9.0"
2325
serde = "~1"
2426
serde_json = "~1"
2527
serde_derive = "~1"
2628
syn = { version = "~0.11", features = ["full"] }
27-
rayon = "1.2.0"
29+
tar = "~0.4"
2830
url = "2.1.1"
2931
void = "1.0.2"

api_generator/src/bin/run.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ fn main() -> Result<(), failure::Error> {
6969
.unwrap();
7070

7171
fs::remove_dir_all(&download_dir)?;
72+
fs::create_dir_all(&download_dir)?;
7273
rest_spec::download_specs(&branch, &download_dir)?;
7374
File::create(&last_downloaded_version)?.write_all(branch.as_bytes())?;
7475
}

api_generator/src/rest_spec/mod.rs

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,78 +17,58 @@
1717
* under the License.
1818
*/
1919
extern crate reqwest;
20+
use self::reqwest::header::{HeaderMap, HeaderValue, USER_AGENT};
21+
use flate2::read::GzDecoder;
22+
use globset::Glob;
23+
use reqwest::Response;
24+
use std::{fs::File, io, path::PathBuf};
25+
use tar::{Archive, Entry};
2026

21-
mod parallel_downloading;
22-
23-
use parallel_downloading::download_specs_to_dir;
24-
use serde::Deserialize;
25-
use std::{fs, path::PathBuf};
26-
27-
struct GitHubSpec {
28-
dir: String,
29-
branch: String,
30-
url: String,
31-
}
32-
33-
#[derive(Deserialize, Debug)]
34-
struct Links {
35-
#[serde(rename = "self")]
36-
self_link: String,
37-
git: String,
38-
html: String,
39-
}
27+
pub fn download_specs(branch: &str, download_dir: &PathBuf) -> Result<(), failure::Error> {
28+
let url = format!(
29+
"https://api.github.com/repos/elastic/elasticsearch/tarball/{}",
30+
branch
31+
);
4032

41-
#[derive(Deserialize, Debug)]
42-
struct RestApiSpec {
43-
name: String,
44-
path: String,
45-
sha: String,
46-
size: i32,
47-
url: String,
48-
html_url: String,
49-
git_url: String,
50-
download_url: String,
51-
#[serde(rename = "type")]
52-
ty: String,
53-
#[serde(rename = "_links")]
54-
links: Links,
55-
}
33+
let mut headers = HeaderMap::new();
34+
headers.append(
35+
USER_AGENT,
36+
HeaderValue::from_str(&format!("elasticsearch-rs/{}", env!("CARGO_PKG_NAME")))?,
37+
);
38+
let client = reqwest::ClientBuilder::new()
39+
.default_headers(headers)
40+
.build()
41+
.unwrap();
5642

57-
pub fn download_specs(branch: &str, download_dir: &PathBuf) -> Result<(), failure::Error> {
58-
let spec_urls = [
59-
("core".to_string(), "https://api.github.com/repos/elastic/elasticsearch/contents/rest-api-spec/src/main/resources/rest-api-spec/api".to_string()),
60-
("xpack".to_string(), "https://api.github.com/repos/elastic/elasticsearch/contents/x-pack/plugin/src/test/resources/rest-api-spec/api".to_string())];
43+
let response = client.get(&url).send()?;
44+
let tar = GzDecoder::new(response);
45+
let mut archive = Archive::new(tar);
6146

62-
let specs: Vec<GitHubSpec> = spec_urls
63-
.iter()
64-
.map(|(dir, template_url)| {
65-
let url = format!("{}?ref={}", template_url, branch);
66-
GitHubSpec {
67-
dir: dir.to_string(),
68-
branch: branch.to_string(),
69-
url,
70-
}
71-
})
72-
.collect();
47+
let oss_spec = Glob::new("**/rest-api-spec/src/main/resources/rest-api-spec/api/*.json")?
48+
.compile_matcher();
49+
let xpack_spec = Glob::new("**/x-pack/plugin/src/test/resources/rest-api-spec/api/*.json")?
50+
.compile_matcher();
7351

74-
fs::create_dir_all(download_dir)?;
75-
for spec in specs {
76-
download_endpoints(&spec, &download_dir)?;
52+
for entry in archive.entries()? {
53+
let file = entry?;
54+
let path = file.path()?;
55+
if oss_spec.is_match(&path) || xpack_spec.is_match(&path) {
56+
write_spec_file(download_dir, file)?;
57+
}
7758
}
7859

7960
Ok(())
8061
}
8162

82-
fn download_endpoints(spec: &GitHubSpec, download_dir: &PathBuf) -> Result<(), failure::Error> {
83-
let client = reqwest::blocking::ClientBuilder::new()
84-
.user_agent(concat!("RustApiGenerator/", env!("CARGO_PKG_VERSION")))
85-
.build()
86-
.unwrap();
63+
fn write_spec_file(
64+
download_dir: &PathBuf,
65+
mut entry: Entry<GzDecoder<Response>>,
66+
) -> Result<(), failure::Error> {
67+
let path = entry.path()?;
68+
let mut dir = download_dir.clone();
69+
dir.push(path.file_name().unwrap());
70+
let mut file = File::create(&dir)?;
71+
io::copy(&mut entry, &mut file)?;
8772

88-
let response = client.get(&spec.url).send().unwrap();
89-
let rest_api_specs: Vec<RestApiSpec> = response.json().unwrap();
90-
println!("Downloading {} specs from {}", spec.dir, spec.branch);
91-
download_specs_to_dir(client, rest_api_specs.as_slice(), download_dir).unwrap();
92-
println!("Done downloading {} specs from {}", spec.dir, spec.branch);
9373
Ok(())
9474
}

api_generator/src/rest_spec/parallel_downloading.rs

Lines changed: 0 additions & 177 deletions
This file was deleted.

yaml_test_runner/src/github.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn download_test_suites(branch: &str, download_dir: &PathBuf) -> Result<(),
4747
let mut headers = HeaderMap::new();
4848
headers.append(
4949
USER_AGENT,
50-
HeaderValue::from_str("elasticsearch-rs/yaml_test_runner")?,
50+
HeaderValue::from_str(&format!("elasticsearch-rs/{}", env!("CARGO_PKG_NAME")))?,
5151
);
5252
let client = reqwest::ClientBuilder::new()
5353
.default_headers(headers)

0 commit comments

Comments
 (0)