Skip to content

Commit 6bc810e

Browse files
authored
[DISCO-4151] Merino: add client for wcs (#7345)
* [DISCO-4151] Merino: add client for wcs * add accept language header
1 parent 4809c2f commit 6bc810e

10 files changed

Lines changed: 1110 additions & 0 deletions

File tree

components/merino/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
2121
pub mod curated_recommendations;
2222
pub mod suggest;
23+
pub mod worldcup;
2324
uniffi::setup_scaffolding!("merino");
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
pub use error_support::error;
6+
use error_support::{ErrorHandling, GetErrorHandling};
7+
8+
pub type Result<T> = std::result::Result<T, Error>;
9+
pub type ApiResult<T> = std::result::Result<T, MerinoWorldCupApiError>;
10+
11+
#[derive(Debug, thiserror::Error, uniffi::Error)]
12+
pub enum MerinoWorldCupApiError {
13+
/// A network-level failure.
14+
#[error("WorldCup network error: {reason}")]
15+
Network { reason: String },
16+
17+
/// Any other error, e.g. HTTP errors, validation errors.
18+
#[error("WorldCup error: code {code:?}, reason: {reason}")]
19+
Other { code: Option<u16>, reason: String },
20+
}
21+
22+
#[derive(Debug, thiserror::Error)]
23+
pub enum Error {
24+
/// Failed to parse a URL.
25+
#[error("URL parse error: {0}")]
26+
UrlParse(#[from] url::ParseError),
27+
28+
/// Failed to send the HTTP request.
29+
#[error("Error sending request: {0}")]
30+
Request(#[from] viaduct::ViaductError),
31+
32+
/// The server rejected the request due to malformed syntax (HTTP 400).
33+
#[error("Bad request ({code}): {message}")]
34+
BadRequest { code: u16, message: String },
35+
36+
/// The server rejected the request due to invalid input (HTTP 422).
37+
#[error("Validation error ({code}): {message}")]
38+
Validation { code: u16, message: String },
39+
40+
/// The server encountered an internal error (HTTP 5xx).
41+
#[error("Server error ({code}): {message}")]
42+
Server { code: u16, message: String },
43+
44+
/// An unexpected HTTP status code was received.
45+
#[error("Unexpected error ({code}): {message}")]
46+
Unexpected { code: u16, message: String },
47+
}
48+
49+
impl GetErrorHandling for Error {
50+
type ExternalError = MerinoWorldCupApiError;
51+
52+
fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
53+
match self {
54+
Self::Request { .. } => ErrorHandling::convert(MerinoWorldCupApiError::Network {
55+
reason: self.to_string(),
56+
})
57+
.log_warning(),
58+
59+
Self::Validation { code, .. }
60+
| Self::Server { code, .. }
61+
| Self::Unexpected { code, .. }
62+
| Self::BadRequest { code, .. } => {
63+
ErrorHandling::convert(MerinoWorldCupApiError::Other {
64+
code: Some(*code),
65+
reason: self.to_string(),
66+
})
67+
.report_error("merino-http-error")
68+
}
69+
70+
Self::UrlParse(_) => ErrorHandling::convert(MerinoWorldCupApiError::Other {
71+
code: None,
72+
reason: self.to_string(),
73+
})
74+
.report_error("merino-unexpected"),
75+
}
76+
}
77+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"current": [
3+
{
4+
"date": "2026-04-30T14:00:00+00:00",
5+
"global_event_id": 1002,
6+
"home_team": {
7+
"key": "ENG",
8+
"global_team_id": 90000005,
9+
"name": "England",
10+
"region": "ENG",
11+
"colors": [
12+
"White",
13+
"Red"
14+
],
15+
"icon_url": "https://storage.googleapis.com/merino-images-prod/logos/nations/nations_gb-eng.png",
16+
"group": "Group C",
17+
"eliminated": false,
18+
"standing": {
19+
"wins": 0,
20+
"losses": 0,
21+
"draws": 0,
22+
"points": 0
23+
}
24+
},
25+
"away_team": {
26+
"key": "USA",
27+
"global_team_id": 90000006,
28+
"name": "United States",
29+
"region": "USA",
30+
"colors": [
31+
"Navy",
32+
"White",
33+
"Red"
34+
],
35+
"icon_url": "https://storage.googleapis.com/merino-images-prod/logos/nations/nations_us.png",
36+
"group": "Group C",
37+
"eliminated": false,
38+
"standing": {
39+
"wins": 0,
40+
"losses": 0,
41+
"draws": 0,
42+
"points": 0
43+
}
44+
},
45+
"period": "2",
46+
"home_score": 1,
47+
"away_score": 0,
48+
"home_extra": null,
49+
"away_extra": null,
50+
"home_penalty": null,
51+
"away_penalty": null,
52+
"clock": "67",
53+
"updated": 1777554000,
54+
"status": "In Progress",
55+
"status_type": "live",
56+
"query": null,
57+
"sport": "soccer"
58+
}
59+
]
60+
}

0 commit comments

Comments
 (0)