Starting to implement nextcloud news Rest API

This commit is contained in:
Shinokuni 2019-05-10 15:41:11 +02:00
parent 60d968d367
commit 3903bb6eb1
20 changed files with 643 additions and 52 deletions

View File

@ -0,0 +1,63 @@
package com.readrops.app.repositories;
import android.app.Application;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.pojo.FeedWithFolder;
import com.readrops.app.utils.FeedInsertionResult;
import com.readrops.app.utils.ParsingResult;
import com.readrops.readropslibrary.services.nextcloudnews.Credentials;
import com.readrops.readropslibrary.services.nextcloudnews.NextNewsAPI;
import com.readrops.readropslibrary.utils.LibUtils;
import java.io.IOException;
import java.util.List;
import io.reactivex.Completable;
import io.reactivex.Observable;
import io.reactivex.Single;
public class NextNewsRepository extends ARepository {
public NextNewsRepository(Application application) {
super(application);
}
@Override
public Observable<Feed> sync(List<Feed> feeds) {
return Observable.create(emitter -> {
try {
NextNewsAPI newsAPI = new NextNewsAPI();
Credentials credentials = new Credentials("", LibUtils.NEXTCLOUD_PASSWORD, "");
newsAPI.sync(credentials, NextNewsAPI.SyncType.INITIAL_SYNC, null);
emitter.onComplete();
} catch (IOException e) {
e.printStackTrace();
emitter.onError(e);
}
});
}
@Override
public Single<List<FeedInsertionResult>> addFeeds(List<ParsingResult> results) {
return null;
}
@Override
public void updateFeedWithFolder(FeedWithFolder feedWithFolder) {
}
@Override
public Completable deleteFeed(int feedId) {
return null;
}
@Override
public Completable addFolder(Folder folder) {
return null;
}
}

View File

@ -20,8 +20,8 @@ import com.readrops.app.database.ItemsListQueryBuilder;
import com.readrops.app.database.entities.Feed;
import com.readrops.app.database.entities.Folder;
import com.readrops.app.database.pojo.ItemWithFeed;
import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.repositories.ARepository;
import com.readrops.app.repositories.NextNewsRepository;
import java.util.ArrayList;
import java.util.List;
@ -49,7 +49,7 @@ public class MainViewModel extends AndroidViewModel {
queryBuilder.setFilterType(FilterType.NO_FILTER);
queryBuilder.setSortType(MainActivity.ListSortType.NEWEST_TO_OLDEST);
repository = new LocalFeedRepository(application);
repository = new NextNewsRepository(application);
db = Database.getInstance(application);
itemsWithFeed = new MediatorLiveData<>();

View File

@ -4,7 +4,7 @@ import android.accounts.NetworkErrorException;
import android.util.Log;
import com.google.gson.Gson;
import com.readrops.readropslibrary.utils.LibOkHttpClient;
import com.readrops.readropslibrary.utils.HttpBuilder;
import com.readrops.readropslibrary.utils.LibUtils;
import com.readrops.readropslibrary.utils.UnknownFormatException;
import com.readrops.readropslibrary.localfeed.atom.ATOMFeed;
@ -73,7 +73,7 @@ public class RSSQuery {
}
private Response query(String url, Map<String, String> headers) throws IOException {
OkHttpClient okHttpClient = LibOkHttpClient.getInstance();
OkHttpClient okHttpClient = HttpBuilder.getBuilder().build();
Request.Builder builder = new Request.Builder().url(url);
for (String header : headers.keySet()) {

View File

@ -1,11 +0,0 @@
package com.readrops.readropslibrary.services;
import com.readrops.readropslibrary.services.nextcloudnews.Folders;
import retrofit2.http.GET;
public interface NextCloudNewsAPI {
@GET("folders")
Folders getFolders();
}

View File

@ -0,0 +1,45 @@
package com.readrops.readropslibrary.services;
import androidx.annotation.CallSuper;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeed;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolders;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsItems;
import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.PUT;
import retrofit2.http.Path;
public interface NextNewsService {
String ENDPOINT = "/index.php/apps/news/api/v1-2/";
@GET("folders")
Call<NextNewsFolders> getFolders();
@GET("feeds")
Call<NextNewsFeeds> getFeeds();
@GET("items?type={type}&getRead={read}&batchSize={batchSize}")
Call<NextNewsItems> getItems(@Path("type") int type, @Path("read") boolean read, @Path("batchSize") int batchSize);
@GET("items/updated?lastModified={lastModified}&type=3")
Call<NextNewsItems> getNewItems(@Path("lastModified") long lastModified);
@PUT("items/read/multiple")
Call<ResponseBody> setReadArticles();
@PUT("items/unread/multiple")
Call<ResponseBody> setUnreadArticles();
@PUT("items/starred/multiple")
Call<ResponseBody> setStarredArticles();
@PUT("items/unstarred/multiple")
Call<ResponseBody> setUnstarredArticles();
}

View File

@ -0,0 +1,44 @@
package com.readrops.readropslibrary.services.nextcloudnews;
public class Credentials {
private String login;
private String password;
private String url;
public Credentials(String login, String password, String url) {
this.login = login;
this.password = password;
this.url = url;
}
public String toBase64() {
return okhttp3.Credentials.basic(login, password);
}
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

View File

@ -1,15 +0,0 @@
package com.readrops.readropslibrary.services.nextcloudnews;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class Folders {
@SerializedName("folders")
private List<Folder> folders;
public List<Folder> getFolders() {
return folders;
}
}

View File

@ -0,0 +1,42 @@
package com.readrops.readropslibrary.services.nextcloudnews;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.readrops.readropslibrary.services.NextNewsService;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFeeds;
import com.readrops.readropslibrary.services.nextcloudnews.json.NextNewsFolders;
import com.readrops.readropslibrary.utils.HttpManager;
import java.io.IOException;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class NextNewsAPI {
public NextNewsAPI() {
}
private Retrofit getConfiguredRetrofitInstance(@NonNull HttpManager httpManager) {
return new Retrofit.Builder()
.baseUrl(httpManager.getCredentials().getUrl() + NextNewsService.ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
.client(httpManager.getOkHttpClient())
.build();
}
public void sync(@NonNull Credentials credentials, @NonNull SyncType syncType, @Nullable SyncData data) throws IOException {
HttpManager httpManager = new HttpManager(credentials);
Retrofit retrofit = getConfiguredRetrofitInstance(httpManager);
NextNewsService api = retrofit.create(NextNewsService.class);
}
public enum SyncType {
INITIAL_SYNC,
CLASSIC_SYNC
}
}

View File

@ -0,0 +1,54 @@
package com.readrops.readropslibrary.services.nextcloudnews;
import java.util.ArrayList;
import java.util.List;
public class SyncData {
private List<Integer> unreadItems;
private List<Integer> readItems;
private List<Integer> starredItems;
private List<Integer> unstarredItems;
public SyncData() {
unreadItems = new ArrayList<>();
readItems = new ArrayList<>();
starredItems = new ArrayList<>();
unstarredItems = new ArrayList<>();
}
public void addUnreadItem(int itemId) {
unreadItems.add(itemId);
}
public void addReadItem(int itemId) {
readItems.add(itemId);
}
public void addStarredItem(int itemId) {
starredItems.add(itemId);
}
public void addUnstarredItem(int itemId) {
unstarredItems.add(itemId);
}
public List<Integer> getUnreadItems() {
return unreadItems;
}
public List<Integer> getReadItems() {
return readItems;
}
public List<Integer> getStarredItems() {
return starredItems;
}
public List<Integer> getUnstarredItems() {
return unstarredItems;
}
}

View File

@ -0,0 +1,104 @@
package com.readrops.readropslibrary.services.nextcloudnews.json;
public class NextNewsFeed {
private int id;
private String url;
private String title;
private String faviconLink;
private float added;
private float folderId;
private float unreadCount;
private float ordering;
private String link;
private boolean pinned;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getFaviconLink() {
return faviconLink;
}
public void setFaviconLink(String faviconLink) {
this.faviconLink = faviconLink;
}
public float getAdded() {
return added;
}
public void setAdded(float added) {
this.added = added;
}
public float getFolderId() {
return folderId;
}
public void setFolderId(float folderId) {
this.folderId = folderId;
}
public float getUnreadCount() {
return unreadCount;
}
public void setUnreadCount(float unreadCount) {
this.unreadCount = unreadCount;
}
public float getOrdering() {
return ordering;
}
public void setOrdering(float ordering) {
this.ordering = ordering;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public boolean isPinned() {
return pinned;
}
public void setPinned(boolean pinned) {
this.pinned = pinned;
}
}

View File

@ -0,0 +1,20 @@
package com.readrops.readropslibrary.services.nextcloudnews.json;
import java.util.List;
public class NextNewsFeeds {
private List<NextNewsFeed> feeds;
public NextNewsFeeds() {
}
public List<NextNewsFeed> getFeeds() {
return feeds;
}
public void setFeeds(List<NextNewsFeed> feeds) {
this.feeds = feeds;
}
}

View File

@ -1,8 +1,8 @@
package com.readrops.readropslibrary.services.nextcloudnews;
package com.readrops.readropslibrary.services.nextcloudnews.json;
import com.google.gson.annotations.SerializedName;
public class Folder {
public class NextNewsFolder {
@SerializedName("id")
private int id;

View File

@ -0,0 +1,16 @@
package com.readrops.readropslibrary.services.nextcloudnews.json;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class NextNewsFolders {
@SerializedName("folders")
private List<NextNewsFolder> folders;
public List<NextNewsFolder> getFolders() {
return folders;
}
}

View File

@ -0,0 +1,154 @@
package com.readrops.readropslibrary.services.nextcloudnews.json;
public class NextNewsItem {
private int id;
private String guid;
private String guidHash;
private String url;
private String title;
private String author;
private float pubDate;
private String body;
private String enclosureMime;
private String enclosureLink;
private float feedId;
private boolean unread;
private boolean starred;
private float lastModified;
private String fingerprint;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public String getGuidHash() {
return guidHash;
}
public void setGuidHash(String guidHash) {
this.guidHash = guidHash;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public float getPubDate() {
return pubDate;
}
public void setPubDate(float pubDate) {
this.pubDate = pubDate;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getEnclosureMime() {
return enclosureMime;
}
public void setEnclosureMime(String enclosureMime) {
this.enclosureMime = enclosureMime;
}
public String getEnclosureLink() {
return enclosureLink;
}
public void setEnclosureLink(String enclosureLink) {
this.enclosureLink = enclosureLink;
}
public float getFeedId() {
return feedId;
}
public void setFeedId(float feedId) {
this.feedId = feedId;
}
public boolean isUnread() {
return unread;
}
public void setUnread(boolean unread) {
this.unread = unread;
}
public boolean isStarred() {
return starred;
}
public void setStarred(boolean starred) {
this.starred = starred;
}
public float getLastModified() {
return lastModified;
}
public void setLastModified(float lastModified) {
this.lastModified = lastModified;
}
public String getFingerprint() {
return fingerprint;
}
public void setFingerprint(String fingerprint) {
this.fingerprint = fingerprint;
}
}

View File

@ -0,0 +1,16 @@
package com.readrops.readropslibrary.services.nextcloudnews.json;
import java.util.List;
public class NextNewsItems {
private List<NextNewsItem> items;
public NextNewsItems(List<NextNewsItem> items) {
this.items = items;
}
public List<NextNewsItem> getItems() {
return items;
}
}

View File

@ -0,0 +1,6 @@
package com.readrops.readropslibrary.services.nextcloudnews.json;
public class SyncResults {
}

View File

@ -0,0 +1,20 @@
package com.readrops.readropslibrary.utils;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
public final class HttpBuilder {
private static OkHttpClient.Builder builder;
public static OkHttpClient.Builder getBuilder() {
if (builder == null)
builder = createOkHttpBuilder();
return builder;
}
private static OkHttpClient.Builder createOkHttpBuilder() {
return new OkHttpClient.Builder();
}
}

View File

@ -0,0 +1,49 @@
package com.readrops.readropslibrary.utils;
import com.readrops.readropslibrary.services.nextcloudnews.Credentials;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class HttpManager {
private OkHttpClient okHttpClient;
private Credentials credentials;
public HttpManager(final Credentials credentials) {
this.credentials = credentials;
okHttpClient = HttpBuilder.getBuilder()
.callTimeout(20, TimeUnit.SECONDS)
.readTimeout(1, TimeUnit.HOURS)
.addInterceptor(new AuthInterceptor())
.build();
}
public OkHttpClient getOkHttpClient() {
return okHttpClient;
}
public Credentials getCredentials() {
return credentials;
}
public class AuthInterceptor implements Interceptor {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
request = request.newBuilder()
.addHeader("Authorization", credentials.toBase64())
.build();
return chain.proceed(request);
}
}
}

View File

@ -1,20 +0,0 @@
package com.readrops.readropslibrary.utils;
import okhttp3.OkHttpClient;
public final class LibOkHttpClient {
private static OkHttpClient okhttpClient;
public static OkHttpClient getInstance() {
if (okhttpClient == null)
okhttpClient = createOkHttpInstance();
return okhttpClient;
}
private static OkHttpClient createOkHttpInstance() {
return new OkHttpClient.Builder()
.build();
}
}

View File

@ -5,6 +5,8 @@ import java.util.Scanner;
public final class LibUtils {
public static final String NEXTCLOUD_PASSWORD = "";
public static final String RSS_DEFAULT_CONTENT_TYPE = "application/rss+xml";
public static final String RSS_TEXT_CONTENT_TYPE = "text/xml";
public static final String RSS_APPLICATION_CONTENT_TYPE = "application/xml";
@ -23,4 +25,6 @@ public final class LibUtils {
return scanner.hasNext() ? scanner.next() : "";
}
}