Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mpg mobile api #194

Merged
merged 13 commits into from
Aug 11, 2021
22 changes: 14 additions & 8 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@ A clear and concise description of what the bug is, with a copy/paste of Java ex

Depending your championship, join data files set in attachment.

1. The [MPG](https://mpg.football/) JSon *Response* of these *Request* on https://api.monpetitgazon.com (use the browser network monitor - F12, see wiki [Get MPG data for opening a bug](https://github.com/axel3rd/mpg-coach-bot/wiki/Get-MPG-data-for-opening-a-bug) for more details):

- From home : `GET /user/dashboard`
- From coach: `GET /league/[yourLeagueId]/coach`
- From transfer : `GET /league/[yourLeagueId]/transfer/buy`
- From mercato : `GET /league/[yourLeagueId]/mercato`
1. The MPG Mobile App JSon *Response* of these *Request* on https://api.mpg.football (use the browser network monitor - F12, see wiki [Get MPG data for opening a bug](https://github.com/axel3rd/mpg-coach-bot/wiki/Get-MPG-data-for-opening-a-bug) for more details):

| Feature | URL | Reason |
| --- | --- | --- |
| login | `GET /user/sign-in` | Retrieve **userId** |
| dashboard | `GET /dashboard/leagues` | Retrieve **divisionId** (~ `mpg_division_MLEXXXXX_3_1`) |
| division | `GET /division/mpg_division_MLEXXXXX_3_1` | Retrieve `mpg_team_MLEXXXXX_3_1_2` team for user league |
| team | `GET /team/mpg_team_MLEXXXXX_3_1_2` | Retrieve Team and Bonus for team |
| coach | `GET /division/mpg_division_MLEXXXXX_3_1/coach` | Retrieve formation |
| availablePlayers | `GET /division/mpg_division_MLEXXXXX_3_1/available-players` | Retrieves available players details for incoming mercato or trading |
| poolPlayer | `GET /championship-players-pool/X` | Retrieve league players details (With `X`: 1=Ligue-1 / 2=Premier-League / 3=Liga / 4=Ligue-2 / 5=Serie-A) |
| clubs | `GET /championship-clubs` | Retrieve club names |

2. The [Players statistics](https://www.mpgstats.fr/) data, one JSon from:

Expand All @@ -34,7 +40,7 @@ Depending your championship, join data files set in attachment.

4. The [Injury / Suspended](https://www.sportsgambler.com/injuries/football/) data, one full HTML from:

- https://maligue2.fr/2019/08/05/joueurs-blesses-et-suspendus/
- https://maligue2.fr/2020/08/20/joueurs-blesses-et-suspendus/
- https://www.sportsgambler.com/injuries/football/france-ligue-1/
- https://www.sportsgambler.com/injuries/football/england-premier-league/
- https://www.sportsgambler.com/injuries/football/spain-la-liga/
Expand All @@ -44,4 +50,4 @@ Depending your championship, join data files set in attachment.

A clear and concise description of what you expected to happen.

If problem on *update team* feature, please join the *Request* and *Response* of `POST /league/[yourLeagueId]/coach` when you save your team in [MPG](https://mpg.football/).
If problem on *update team* feature, please join the *Request* and *Response* of `PUT /match-team-formation/['matchTeamFormation.id' from 'coach' request, start with 'mpg_match_team_formation_']` when you save your team in MPG Mobile App.
41 changes: 16 additions & 25 deletions src/main/java/org/blondin/mpg/AbstractClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,43 +84,35 @@ protected void setUrl(String url) {
}

protected <T> T get(String path, Class<T> entityResponse) {
return get(path, null, entityResponse, false);
return get(path, null, entityResponse, -1);
}

protected <T> T get(String path, Class<T> entityResponse, long cacheTimeMilliSecond) {
return get(path, null, entityResponse, false, cacheTimeMilliSecond);
return get(path, null, entityResponse, cacheTimeMilliSecond);
}

protected <T> T get(String path, MultivaluedMap<String, Object> headers, Class<T> entityResponse) {
return get(path, headers, entityResponse, false, -1);
return get(path, headers, entityResponse, -1);
}

protected <T> T get(String path, MultivaluedMap<String, Object> headers, Class<T> entityResponse, long cacheTimeMilliSecond) {
return get(path, headers, entityResponse, false, cacheTimeMilliSecond);
}

protected <T> T get(String path, MultivaluedMap<String, Object> headers, Class<T> entityResponse, boolean wrapRoot) {
return call(path, headers, null, entityResponse, wrapRoot, -1);
}

protected <T> T get(String path, MultivaluedMap<String, Object> headers, Class<T> entityResponse, boolean wrapRoot, long cacheTimeMilliSecond) {
return call(path, headers, null, entityResponse, wrapRoot, cacheTimeMilliSecond);
return call(path, headers, null, entityResponse, cacheTimeMilliSecond, false);
}

protected <T> T post(String path, Object entityRequest, Class<T> entityResponse) {
return post(path, null, entityRequest, entityResponse, false);
return post(path, null, entityRequest, entityResponse);
}

protected <T> T post(String path, MultivaluedMap<String, Object> headers, Object entityRequest, Class<T> entityResponse) {
return post(path, headers, entityRequest, entityResponse, false);
return call(path, headers, entityRequest, entityResponse, -1, false);
}

protected <T> T post(String path, MultivaluedMap<String, Object> headers, Object entityRequest, Class<T> entityResponse, boolean wrapRoot) {
return call(path, headers, entityRequest, entityResponse, wrapRoot, -1);
protected <T> T put(String path, MultivaluedMap<String, Object> headers, Object entityRequest, Class<T> entityResponse) {
return call(path, headers, entityRequest, entityResponse, -1, true);
}

private <T> T call(String path, MultivaluedMap<String, Object> headers, Object entityRequest, Class<T> entityResponse, boolean wrapRoot,
long cacheTimeMilliSecond) {
private <T> T call(String path, MultivaluedMap<String, Object> headers, Object entityRequest, Class<T> entityResponse, long cacheTimeMilliSecond,
boolean entityRequestPut) {
long start = System.currentTimeMillis();
try {
LOG.debug("Call URL: {}/{} (cache duration ms: {})", url, path, cacheTimeMilliSecond);
Expand All @@ -146,14 +138,11 @@ private <T> T call(String path, MultivaluedMap<String, Object> headers, Object e
}
Client client = clientBuilder.build();

if (wrapRoot) {
client = client.register(ObjectMapperContextResolver.class);
}
WebTarget webTarget = client.target(url).path(path);
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON).headers(headers);

waitBeforeNextRequest();
Response response = invokeWithRetry(invocationBuilder, entityRequest, url, path, 0);
Response response = invokeWithRetry(invocationBuilder, entityRequest, entityRequestPut, url, path, 0);
if (Response.Status.FORBIDDEN.getStatusCode() == response.getStatus()) {
throw new UrlForbiddenException(String.format("Forbidden URL: %s", url));
}
Expand Down Expand Up @@ -181,12 +170,14 @@ private <T> T call(String path, MultivaluedMap<String, Object> headers, Object e
}
}

private static Response invokeWithRetry(Invocation.Builder invocationBuilder, Object entityRequest, final String url, final String path,
int retryCount) {
private static Response invokeWithRetry(Invocation.Builder invocationBuilder, Object entityRequest, boolean entityRequestPut, final String url,
final String path, int retryCount) {
Response response = null;
try {
if (entityRequest == null) {
response = invocationBuilder.get();
} else if (entityRequestPut) {
response = invocationBuilder.put(Entity.entity(entityRequest, MediaType.APPLICATION_JSON));
} else {
response = invocationBuilder.post(Entity.entity(entityRequest, MediaType.APPLICATION_JSON));
}
Expand All @@ -198,7 +189,7 @@ private static Response invokeWithRetry(Invocation.Builder invocationBuilder, Ob
} catch (InterruptedException e1) { // NOSONAR : Sleep wanted
throw new UnsupportedOperationException(e1);
}
return invokeWithRetry(invocationBuilder, entityRequest, url, path, ++retryCount);
return invokeWithRetry(invocationBuilder, entityRequest, entityRequestPut, url, path, ++retryCount);
}
throw e;
}
Expand Down
Loading