|
1 | 1 | package fr.free.nrw.commons.wikidata;
|
2 | 2 |
|
3 |
| -import org.wikipedia.csrf.CsrfTokenClient; |
4 |
| -import org.wikipedia.dataclient.Service; |
| 3 | +import org.jetbrains.annotations.NotNull; |
5 | 4 |
|
6 | 5 | import javax.inject.Inject;
|
7 |
| -import javax.inject.Named; |
8 | 6 | import javax.inject.Singleton;
|
9 | 7 |
|
| 8 | +import fr.free.nrw.commons.wikidata.model.AddEditTagResponse; |
10 | 9 | import io.reactivex.Observable;
|
11 |
| - |
12 |
| -import static fr.free.nrw.commons.di.NetworkingModule.NAMED_WIKI_DATA_CSRF; |
| 10 | +import io.reactivex.ObservableSource; |
| 11 | +import okhttp3.MediaType; |
| 12 | +import okhttp3.RequestBody; |
13 | 13 |
|
14 | 14 | @Singleton
|
15 | 15 | public class WikidataClient {
|
16 | 16 |
|
17 |
| - private final Service service; |
18 |
| - private final CsrfTokenClient csrfTokenClient; |
| 17 | + |
| 18 | + private final WikidataInterface wikidataInterface; |
19 | 19 |
|
20 | 20 | @Inject
|
21 |
| - public WikidataClient(@Named("wikidata-service") Service service, |
22 |
| - @Named(NAMED_WIKI_DATA_CSRF) CsrfTokenClient csrfTokenClient) { |
23 |
| - this.service = service; |
24 |
| - this.csrfTokenClient = csrfTokenClient; |
| 21 | + public WikidataClient(WikidataInterface wikidataInterface) { |
| 22 | + this.wikidataInterface = wikidataInterface; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Create wikidata claim to add P18 value |
| 27 | + * @param entityId wikidata entity ID |
| 28 | + * @param value value of the P18 edit |
| 29 | + * @return revisionID of the edit |
| 30 | + */ |
| 31 | + Observable<Long> createClaim(String entityId, String value) { |
| 32 | + return getCsrfToken() |
| 33 | + .flatMap(csrfToken -> wikidataInterface.postCreateClaim(toRequestBody(entityId), |
| 34 | + toRequestBody("value"), |
| 35 | + toRequestBody("P18"), |
| 36 | + toRequestBody(value), |
| 37 | + toRequestBody("en"), |
| 38 | + toRequestBody(csrfToken))) |
| 39 | + .map(mwPostResponse -> mwPostResponse.getPageinfo().getLastrevid()); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Converts string value to RequestBody for multipart request |
| 44 | + */ |
| 45 | + private RequestBody toRequestBody(String value) { |
| 46 | + return RequestBody.create(MediaType.parse("text/plain"), value); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Get csrf token for wikidata edit |
| 51 | + */ |
| 52 | + @NotNull |
| 53 | + private Observable<String> getCsrfToken() { |
| 54 | + return wikidataInterface.getCsrfToken().map(mwQueryResponse -> mwQueryResponse.query().csrfToken()); |
25 | 55 | }
|
26 | 56 |
|
27 |
| - public Observable<Long> createClaim(String entityId, String property, String snaktype, String value) { |
28 |
| - try { |
29 |
| - return service.postCreateClaim(entityId, snaktype, property, value, "en", csrfTokenClient.getTokenBlocking()) |
30 |
| - .map(mwPostResponse -> { |
31 |
| - if (mwPostResponse.getSuccessVal() == 1) { |
32 |
| - return 1L; |
33 |
| - } |
34 |
| - return -1L; |
35 |
| - }); |
36 |
| - } catch (Throwable throwable) { |
37 |
| - return Observable.just(-1L); |
38 |
| - } |
| 57 | + /** |
| 58 | + * Add edit tag for a given revision ID. The app currently uses this to tag P18 edits |
| 59 | + * @param revisionId revision ID of the page edited |
| 60 | + * @param tag to be added |
| 61 | + * @param reason to be mentioned |
| 62 | + */ |
| 63 | + ObservableSource<AddEditTagResponse> addEditTag(Long revisionId, String tag, String reason) { |
| 64 | + return getCsrfToken() |
| 65 | + .flatMap(csrfToken -> wikidataInterface.addEditTag(String.valueOf(revisionId), |
| 66 | + tag, |
| 67 | + reason, |
| 68 | + csrfToken)); |
39 | 69 | }
|
40 | 70 | }
|
0 commit comments