blob: e2cfda22e7f9b8fe3f88481690452738679907cd (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
package it.niedermann.nextcloud.deck.api;
import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.api.NextcloudAPI;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import it.niedermann.nextcloud.deck.DeckLog;
import retrofit2.NextcloudRetrofitApiBuilder;
/**
* Created by david on 26.05.17.
*/
public class ApiProvider {
private static final String DECK_API_ENDPOINT = "/index.php/apps/deck/api/v1.0/";
private static final String NC_API_ENDPOINT = "/ocs/v2.php/";
private DeckAPI deckAPI;
private NextcloudServerAPI nextcloudAPI;
@NonNull
private Context context;
private SingleSignOnAccount ssoAccount;
@Nullable
private String ssoAccountName;
public ApiProvider(Context context) {
this(context, null);
}
public ApiProvider(@NonNull Context context, @Nullable String ssoAccountName) {
this.context = context;
this.ssoAccountName = ssoAccountName;
setAccount();
}
public void initSsoApi(final NextcloudAPI.ApiConnectedListener callback) {
final NextcloudAPI nextcloudAPI = new NextcloudAPI(context, ssoAccount, GsonConfig.getGson(), callback);
deckAPI = new NextcloudRetrofitApiBuilder(nextcloudAPI, DECK_API_ENDPOINT).create(DeckAPI.class);
this.nextcloudAPI = new NextcloudRetrofitApiBuilder(nextcloudAPI, NC_API_ENDPOINT).create(NextcloudServerAPI.class);
}
private void setAccount() {
try {
if (ssoAccountName == null) {
this.ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(context);
} else {
this.ssoAccount = AccountImporter.getSingleSignOnAccount(context, ssoAccountName);
}
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
DeckLog.logError(e);
}
}
public DeckAPI getDeckAPI() {
return deckAPI;
}
public NextcloudServerAPI getNextcloudAPI() {
return nextcloudAPI;
}
public String getServerUrl(){
if (ssoAccount == null) {
setAccount();
}
return ssoAccount.url;
}
public String getApiPath() {
return DECK_API_ENDPOINT;
}
public String getApiUrl() {
return getServerUrl() + getApiPath();
}
}
|