Upgrade the lookup example #227

Merged
wojtek merged 1 commits from 160---provide-a-keyboard-shortcut-to-pull-all-release-groups-of-an-artist into main 2024-09-29 15:23:32 +02:00
3 changed files with 85 additions and 44 deletions
Showing only changes of commit 67239c75e3 - Show all commits

View File

@ -48,8 +48,8 @@ name = "musichoard-edit"
required-features = ["bin", "database-json"]
[[example]]
name = "musicbrainz-api---lookup-artist"
path = "examples/musicbrainz_api/lookup_artist.rs"
name = "musicbrainz-api---lookup"
path = "examples/musicbrainz_api/lookup.rs"
required-features = ["bin", "musicbrainz"]
[[example]]

View File

@ -0,0 +1,83 @@
#![allow(non_snake_case)]
use musichoard::{
collection::musicbrainz::Mbid,
external::musicbrainz::{
api::{
lookup::{LookupArtistRequest, LookupReleaseGroupRequest},
MusicBrainzClient,
},
http::MusicBrainzHttp,
},
};
use structopt::StructOpt;
use uuid::Uuid;
const USER_AGENT: &str = concat!(
"MusicHoard---examples---musicbrainz-api---lookup/",
env!("CARGO_PKG_VERSION"),
" ( musichoard@thenineworlds.net )"
);
#[derive(StructOpt)]
struct Opt {
#[structopt(subcommand)]
entity: OptEntity,
}
#[derive(StructOpt)]
enum OptEntity {
#[structopt(about = "Lookup artist")]
Artist(OptArtist),
#[structopt(about = "Lookup release group")]
ReleaseGroup(OptReleaseGroup),
}
#[derive(StructOpt)]
struct OptArtist {
#[structopt(help = "Artist MBID to lookup")]
mbid: Uuid,
#[structopt(long, help = "Include release groups in lookup")]
release_groups: bool,
}
#[derive(StructOpt)]
struct OptReleaseGroup {
#[structopt(help = "Release group MBID to lookup")]
mbid: Uuid,
}
fn main() {
let opt = Opt::from_args();
println!("USER_AGENT: {USER_AGENT}");
let http = MusicBrainzHttp::new(USER_AGENT).expect("failed to create API client");
let mut client = MusicBrainzClient::new(http);
match opt.entity {
OptEntity::Artist(opt_artist) => {
let mbid: Mbid = opt_artist.mbid.into();
let mut request = LookupArtistRequest::new(&mbid);
if opt_artist.release_groups {
request.include_release_groups();
}
let response = client
.lookup_artist(request)
.expect("failed to make API call");
println!("{response:#?}");
}
OptEntity::ReleaseGroup(opt_release_group) => {
let mbid: Mbid = opt_release_group.mbid.into();
let request = LookupReleaseGroupRequest::new(&mbid);
let response = client
.lookup_release_group(request)
.expect("failed to make API call");
println!("{response:#?}");
}
}
}

View File

@ -1,42 +0,0 @@
#![allow(non_snake_case)]
use musichoard::{
collection::musicbrainz::Mbid,
external::musicbrainz::{
api::{lookup::LookupArtistRequest, MusicBrainzClient},
http::MusicBrainzHttp,
},
};
use structopt::StructOpt;
use uuid::Uuid;
const USER_AGENT: &str = concat!(
"MusicHoard---examples---musicbrainz-api---lookup-artist/",
env!("CARGO_PKG_VERSION"),
" ( musichoard@thenineworlds.net )"
);
#[derive(StructOpt)]
struct Opt {
#[structopt(help = "Artist MBID to lookup")]
mbid: Uuid,
}
fn main() {
let opt = Opt::from_args();
println!("USER_AGENT: {USER_AGENT}");
let http = MusicBrainzHttp::new(USER_AGENT).expect("failed to create API client");
let mut client = MusicBrainzClient::new(http);
let mbid: Mbid = opt.mbid.into();
let mut request = LookupArtistRequest::new(&mbid);
request.include_release_groups();
let response = client
.lookup_artist(request)
.expect("failed to make API call");
println!("{response:#?}");
}