-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAddressRepo.java
More file actions
119 lines (103 loc) · 4.36 KB
/
Copy pathAddressRepo.java
File metadata and controls
119 lines (103 loc) · 4.36 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Copyright (c) 2016 - Bernie 2016, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.berniesanders.fieldthebern.repositories;
import android.content.Context;
import com.berniesanders.fieldthebern.config.Config;
import com.berniesanders.fieldthebern.exceptions.NetworkUnavailableException;
import com.berniesanders.fieldthebern.models.MultiAddressResponse;
import com.berniesanders.fieldthebern.models.SingleAddressResponse;
import com.berniesanders.fieldthebern.network.NetChecker;
import com.berniesanders.fieldthebern.repositories.auth.ApiAuthenticator;
import com.berniesanders.fieldthebern.repositories.interceptors.AddTokenInterceptor;
import com.berniesanders.fieldthebern.repositories.interceptors.UserAgentInterceptor;
import com.berniesanders.fieldthebern.repositories.specs.AddressSpec;
import com.f2prateek.rx.preferences.RxSharedPreferences;
import com.google.gson.Gson;
import javax.inject.Inject;
import javax.inject.Singleton;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.GsonConverterFactory;
import retrofit2.Retrofit;
import retrofit2.RxJavaCallAdapterFactory;
import rx.Observable;
import timber.log.Timber;
/**
* For loading address canvass data from the API
*/
@Singleton
public class AddressRepo {
final Gson gson;
private final TokenRepo tokenRepo;
private final RxSharedPreferences rxPrefs;
private OkHttpClient client;
private final Config config;
private final Context context;
@Inject
public AddressRepo(Gson gson, TokenRepo tokenRepo, RxSharedPreferences rxPrefs, Config config,
Context context) {
this.gson = gson;
this.tokenRepo = tokenRepo;
this.rxPrefs = rxPrefs;
this.config = config;
this.context = context;
HttpLoggingInterceptor.Logger logger = message -> Timber.v(message);
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(logger);
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
client =
new OkHttpClient.Builder().addInterceptor(new UserAgentInterceptor(config.getUserAgent()))
.addInterceptor(new AddTokenInterceptor(tokenRepo))
.addInterceptor(loggingInterceptor)
.authenticator(new ApiAuthenticator(tokenRepo))
.build();
}
/**
*
*/
public Observable<MultiAddressResponse> getMultiple(final AddressSpec spec) {
Timber.v("getMultiple()");
if (!NetChecker.connected(context)) {
return Observable.error(new NetworkUnavailableException("No internet available"));
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(config.getCanvassUrl())
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
AddressSpec.AddressEndpoint endpoint = retrofit.create(AddressSpec.AddressEndpoint.class);
return endpoint.getMultiple(spec.multipleAddresses().latitude(),
spec.multipleAddresses().longitude(), spec.multipleAddresses().radius());
}
/**
*
*/
public Observable<SingleAddressResponse> getSingle(final AddressSpec spec) {
Timber.v("getSingle()");
if (!NetChecker.connected(context)) {
return Observable.error(new NetworkUnavailableException("No internet available"));
}
Retrofit retrofit = new Retrofit.Builder().baseUrl(config.getCanvassUrl())
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.client(client)
.build();
AddressSpec.AddressEndpoint endpoint = retrofit.create(AddressSpec.AddressEndpoint.class);
return endpoint.getSingle(spec.singleAddress().latitude(), spec.singleAddress().longitude(),
spec.singleAddress().street1(), spec.singleAddress().street2(), spec.singleAddress().city(),
spec.singleAddress().state(), spec.singleAddress().zip());
}
}