From b728d8744e00892cd030c100b7e253a9ab88998a Mon Sep 17 00:00:00 2001 From: Mariotaku Lee Date: Tue, 11 Apr 2017 09:21:15 +0800 Subject: [PATCH] fixed reply highlighting --- build.gradle | 4 +- twidere/build.gradle | 3 +- twidere/src/.google.commit-id | 2 +- .../util/LoganSquareConverterFactory.java | 138 ------------------ .../twitter/util/TwitterConverterFactory.java | 36 ----- .../twidere/util/MicroBlogAPIFactory.java | 122 ---------------- .../twidere/activity/ComposeActivity.kt | 1 + .../extension/model/CredentialsExtensions.kt | 14 +- .../util/api/TwidereExceptionFactory.kt | 66 +++++++++ .../util/api/TwidereHttpRequestFactory.kt | 58 ++++++++ .../util/api/TwidereRestRequestFactory.kt | 57 ++++++++ .../util/api/TwitterConverterFactory.kt | 78 ++++++++++ .../mariotaku/twidere/util/qr/QrCodeData.kt | 14 +- 13 files changed, 277 insertions(+), 316 deletions(-) delete mode 100644 twidere/src/main/java/org/mariotaku/microblog/library/twitter/util/LoganSquareConverterFactory.java delete mode 100644 twidere/src/main/java/org/mariotaku/microblog/library/twitter/util/TwitterConverterFactory.java create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereExceptionFactory.kt create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereHttpRequestFactory.kt create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereRestRequestFactory.kt create mode 100644 twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwitterConverterFactory.kt diff --git a/build.gradle b/build.gradle index e36093b35..a5d798a84 100644 --- a/build.gradle +++ b/build.gradle @@ -36,7 +36,7 @@ subprojects { Kotlin : '1.1.1', SupportLib : '25.3.1', MariotakuCommons : '0.9.13', - RestFu : '0.9.44', + RestFu : '0.9.47', ObjectCursor : '0.9.16', PlayServices : '10.2.1', MapsUtils : '0.4.4', @@ -62,7 +62,7 @@ subprojects { Kovenant : '3.3.0', ParcelablePlease : '1.0.2', Chameleon : '0.9.17', - UniqR : '0.9', + UniqR : '0.9.1', SQLiteQB : '0.9.15', ] } diff --git a/twidere/build.gradle b/twidere/build.gradle index bbef1cd81..97f9dcd06 100644 --- a/twidere/build.gradle +++ b/twidere/build.gradle @@ -165,6 +165,7 @@ dependencies { compile "com.github.mariotaku:PickNCrop:${libVersions['PickNCrop']}" compile "com.github.mariotaku.RestFu:library:${libVersions['RestFu']}" compile "com.github.mariotaku.RestFu:okhttp3:${libVersions['RestFu']}" + compile "com.github.mariotaku.RestFu:logansquare:${libVersions['RestFu']}" compile 'com.squareup.okhttp3:okhttp:3.6.0' compile 'com.lnikkila:extendedtouchview:0.1.0' compile 'com.google.dagger:dagger:2.8' @@ -189,7 +190,7 @@ dependencies { compile "com.github.mariotaku:KPreferences:${libVersions['KPreferences']}" compile "com.github.mariotaku:Chameleon:${libVersions['Chameleon']}" compile 'com.github.mariotaku.QR-Code-generator:core:fcab3ea7f4' - compile 'com.github.mariotaku.UniqR:android:0.9' + compile "com.github.mariotaku.UniqR:android:${libVersions['UniqR']}" compile "org.jetbrains.kotlin:kotlin-stdlib:${libVersions['Kotlin']}" compile "nl.komponents.kovenant:kovenant:${libVersions['Kovenant']}" diff --git a/twidere/src/.google.commit-id b/twidere/src/.google.commit-id index a77e28d9f..c8ceadfab 100644 --- a/twidere/src/.google.commit-id +++ b/twidere/src/.google.commit-id @@ -1 +1 @@ -e174801d3bd84f433aaac0c7d1cff2b6b8c43744 +541542795b5b0b81f05a93400d7099cc877d2c84 diff --git a/twidere/src/main/java/org/mariotaku/microblog/library/twitter/util/LoganSquareConverterFactory.java b/twidere/src/main/java/org/mariotaku/microblog/library/twitter/util/LoganSquareConverterFactory.java deleted file mode 100644 index add5d3cc6..000000000 --- a/twidere/src/main/java/org/mariotaku/microblog/library/twitter/util/LoganSquareConverterFactory.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * Twidere - Twitter client for Android - * - * Copyright (C) 2012-2017 Mariotaku Lee - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.mariotaku.microblog.library.twitter.util; - -import android.support.annotation.NonNull; -import android.support.v4.util.SimpleArrayMap; - -import com.bluelinelabs.logansquare.Commons_ParameterizedTypeAccessor; -import com.bluelinelabs.logansquare.ParameterizedType; -import com.fasterxml.jackson.core.JsonParseException; - -import org.mariotaku.microblog.library.twitter.model.TwitterResponse; -import org.mariotaku.restfu.RestConverter; -import org.mariotaku.restfu.http.ContentType; -import org.mariotaku.restfu.http.HttpResponse; -import org.mariotaku.restfu.http.mime.Body; -import org.mariotaku.restfu.http.mime.SimpleBody; -import org.mariotaku.restfu.http.mime.StringBody; -import org.mariotaku.twidere.util.JsonSerializer; - -import java.io.IOException; -import java.lang.reflect.Type; -import java.util.List; -import java.util.Map; - -/** - * Created by mariotaku on 2017/3/23. - */ - -public class LoganSquareConverterFactory extends RestConverter.SimpleFactory { - - protected SimpleArrayMap> responseConverters = new SimpleArrayMap<>(); - protected SimpleArrayMap> sBodyConverters = new SimpleArrayMap<>(); - - @Override - public RestConverter forResponse(Type type) throws RestConverter.ConvertException { - RestConverter converter = responseConverters.get(type); - if (converter != null) { - return converter; - } - return new JsonResponseConverter<>(type); - } - - @Override - public RestConverter forRequest(Type type) throws RestConverter.ConvertException { - final RestConverter converter = sBodyConverters.get(type); - if (converter != null) { - return converter; - } - if (SimpleBody.supports(type)) { - return new SimpleBodyConverter<>(type); - } - return new JsonRequestConverter<>(type); - } - - @NonNull - private static Object parseOrThrow(HttpResponse response, ParameterizedType type) - throws IOException, RestConverter.ConvertException { - try { - final Object parsed; - if (type.rawType == List.class) { - final Class cls = type.typeParameters.get(0).rawType; - parsed = JsonSerializer.parseList(response.getBody().stream(), cls); - } else if (type.rawType == Map.class) { - final Class cls = type.typeParameters.get(1).rawType; - parsed = JsonSerializer.parseMap(response.getBody().stream(), cls); - } else { - parsed = JsonSerializer.parse(response.getBody().stream(), type); - } - if (parsed == null) { - throw new IOException("Empty data"); - } - return parsed; - } catch (JsonParseException e) { - throw new RestConverter.ConvertException("Malformed JSON Data"); - } - } - - private static class JsonResponseConverter implements RestConverter { - private final ParameterizedType type; - - JsonResponseConverter(Type type) { - this.type = Commons_ParameterizedTypeAccessor.create(type); - } - - @Override - public Object convert(HttpResponse httpResponse) throws IOException, ConvertException, E { - final Object object = parseOrThrow(httpResponse, type); - if (object instanceof TwitterResponse) { - ((TwitterResponse) object).processResponseHeader(httpResponse); - } - return object; - } - } - - private static class JsonRequestConverter implements RestConverter { - private final ParameterizedType type; - - JsonRequestConverter(Type type) { - this.type = Commons_ParameterizedTypeAccessor.create(type); - } - - @Override - public Body convert(Object request) throws IOException, ConvertException, E { - final String json; - if (type.rawType == List.class) { - final Class cls = type.typeParameters.get(0).rawType; - //noinspection unchecked - json = JsonSerializer.serializeList((List) request, cls); - } else if (type.rawType == Map.class) { - final Class cls = type.typeParameters.get(1).rawType; - //noinspection unchecked - json = JsonSerializer.serializeMap((Map) request, cls); - } else { - //noinspection unchecked - json = JsonSerializer.serialize(request, (ParameterizedType) type); - } - return new StringBody(json, ContentType.parse("application/json")); - } - } -} diff --git a/twidere/src/main/java/org/mariotaku/microblog/library/twitter/util/TwitterConverterFactory.java b/twidere/src/main/java/org/mariotaku/microblog/library/twitter/util/TwitterConverterFactory.java deleted file mode 100644 index 60ed10c5e..000000000 --- a/twidere/src/main/java/org/mariotaku/microblog/library/twitter/util/TwitterConverterFactory.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Twidere - Twitter client for Android - * - * Copyright (C) 2012-2015 Mariotaku Lee - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package org.mariotaku.microblog.library.twitter.util; - -import org.mariotaku.microblog.library.MicroBlogException; -import org.mariotaku.microblog.library.twitter.model.ResponseCode; -import org.mariotaku.restfu.oauth.OAuthToken; - -/** - * Created by mariotaku on 15/5/5. - */ -public class TwitterConverterFactory extends LoganSquareConverterFactory { - - public TwitterConverterFactory() { - responseConverters.put(ResponseCode.class, new ResponseCode.ResponseConverter()); - responseConverters.put(OAuthToken.class, new OAuthTokenResponseConverter()); - } - -} diff --git a/twidere/src/main/java/org/mariotaku/twidere/util/MicroBlogAPIFactory.java b/twidere/src/main/java/org/mariotaku/twidere/util/MicroBlogAPIFactory.java index 8ae4d64bc..be16cb042 100644 --- a/twidere/src/main/java/org/mariotaku/twidere/util/MicroBlogAPIFactory.java +++ b/twidere/src/main/java/org/mariotaku/twidere/util/MicroBlogAPIFactory.java @@ -11,27 +11,9 @@ import android.support.annotation.WorkerThread; import android.text.TextUtils; import android.webkit.URLUtil; -import com.fasterxml.jackson.core.JsonParseException; - import org.mariotaku.microblog.library.MicroBlog; -import org.mariotaku.microblog.library.MicroBlogException; -import org.mariotaku.microblog.library.twitter.util.TwitterConverterFactory; -import org.mariotaku.restfu.ExceptionFactory; -import org.mariotaku.restfu.RestConverter; -import org.mariotaku.restfu.RestFuUtils; -import org.mariotaku.restfu.RestMethod; -import org.mariotaku.restfu.RestRequest; -import org.mariotaku.restfu.annotation.HttpMethod; -import org.mariotaku.restfu.http.Authorization; -import org.mariotaku.restfu.http.BodyType; import org.mariotaku.restfu.http.Endpoint; -import org.mariotaku.restfu.http.HttpRequest; -import org.mariotaku.restfu.http.HttpResponse; -import org.mariotaku.restfu.http.MultiValueMap; -import org.mariotaku.restfu.http.RawValue; import org.mariotaku.restfu.http.SimpleValueMap; -import org.mariotaku.restfu.http.ValueMap; -import org.mariotaku.restfu.http.mime.Body; import org.mariotaku.restfu.oauth.OAuthEndpoint; import org.mariotaku.restfu.oauth.OAuthToken; import org.mariotaku.twidere.TwidereConstants; @@ -44,9 +26,7 @@ import org.mariotaku.twidere.model.util.AccountUtils; import org.mariotaku.twidere.util.api.TwitterAndroidExtraHeaders; import org.mariotaku.twidere.util.api.UserAgentExtraHeaders; -import java.io.IOException; import java.util.List; -import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -267,108 +247,6 @@ public class MicroBlogAPIFactory implements TwidereConstants { || '0' <= codePoint && codePoint <= '9'; } - public static class TwidereHttpRequestFactory implements HttpRequest.Factory { - - @Nullable - private final ExtraHeaders extraHeaders; - - public TwidereHttpRequestFactory(final @Nullable ExtraHeaders extraHeaders) { - this.extraHeaders = extraHeaders; - } - - @Override - public HttpRequest create(@NonNull Endpoint endpoint, @NonNull RestRequest info, - @Nullable Authorization authorization, - RestConverter.Factory converterFactory) - throws IOException, RestConverter.ConvertException, E { - final String restMethod = info.getMethod(); - final String url = Endpoint.constructUrl(endpoint.getUrl(), info); - MultiValueMap headers = info.getHeaders(); - if (headers == null) { - headers = new MultiValueMap<>(); - } - - if (authorization != null && authorization.hasAuthorization()) { - headers.add("Authorization", RestFuUtils.sanitizeHeader(authorization.getHeader(endpoint, info))); - } - if (extraHeaders != null) { - for (final Pair pair : extraHeaders.get()) { - headers.add(pair.getFirst(), RestFuUtils.sanitizeHeader(pair.getSecond())); - } - } - return new HttpRequest(restMethod, url, headers, info.getBody(converterFactory), null); - } - } - - public static class TwidereExceptionFactory implements ExceptionFactory { - - private final TwitterConverterFactory converterFactory; - - public TwidereExceptionFactory(TwitterConverterFactory converterFactory) { - this.converterFactory = converterFactory; - } - - @Override - public MicroBlogException newException(Throwable cause, HttpRequest request, HttpResponse response) { - final MicroBlogException te; - if (cause != null) { - te = new MicroBlogException(cause); - } else { - te = parseTwitterException(response); - } - te.setHttpRequest(request); - te.setHttpResponse(response); - return te; - } - - - public MicroBlogException parseTwitterException(HttpResponse resp) { - try { - return (MicroBlogException) converterFactory.forResponse(MicroBlogException.class).convert(resp); - } catch (JsonParseException e) { - return new MicroBlogException("Malformed JSON Data", e); - } catch (IOException e) { - return new MicroBlogException("IOException while throwing exception", e); - } catch (RestConverter.ConvertException e) { - return new MicroBlogException(e); - } catch (MicroBlogException e) { - return e; - } - } - } - - public static class TwidereRestRequestFactory implements RestRequest.Factory { - @Nullable - private final Map extraRequestParams; - - public TwidereRestRequestFactory(@Nullable Map extraRequestParams) { - this.extraRequestParams = extraRequestParams; - } - - @Override - public RestRequest create(RestMethod restMethod, - RestConverter.Factory factory, - ValueMap valuePool) throws RestConverter.ConvertException, IOException, MicroBlogException { - final HttpMethod method = restMethod.getMethod(); - final String path = restMethod.getPath(); - final MultiValueMap headers = restMethod.getHeaders(valuePool); - final MultiValueMap queries = restMethod.getQueries(valuePool); - final MultiValueMap params = restMethod.getParams(factory, valuePool); - final RawValue rawValue = restMethod.getRawValue(); - final BodyType bodyType = restMethod.getBodyType(); - final Map extras = restMethod.getExtras(); - - if (queries != null && extraRequestParams != null) { - for (Map.Entry entry : extraRequestParams.entrySet()) { - queries.add(entry.getKey(), entry.getValue()); - } - } - - return new RestRequest(method.value(), method.allowBody(), path, headers, queries, - params, rawValue, bodyType, extras); - } - } - public interface ExtraHeaders { @NonNull List> get(); diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt index 8852ad8ca..73afd01d2 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/activity/ComposeActivity.kt @@ -1520,6 +1520,7 @@ class ComposeActivity : BaseActivity(), OnMenuItemClickListener, OnClickListener val textAndMentions = extractor.extractReplyTextAndMentions(text, inReplyTo) if (textAndMentions.replyToOriginalUser) { hintLabel.visibility = View.GONE + editable.clearSpans(MentionColorSpan::class.java) editable.setSpan(MentionColorSpan(mentionColor), 0, textAndMentions.replyStartIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE) } else { diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/CredentialsExtensions.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/CredentialsExtensions.kt index 63b573aad..9309c4a2d 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/CredentialsExtensions.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/extension/model/CredentialsExtensions.kt @@ -9,7 +9,6 @@ import org.mariotaku.microblog.library.fanfou.FanfouStream import org.mariotaku.microblog.library.twitter.* import org.mariotaku.microblog.library.twitter.auth.BasicAuthorization import org.mariotaku.microblog.library.twitter.auth.EmptyAuthorization -import org.mariotaku.microblog.library.twitter.util.TwitterConverterFactory import org.mariotaku.restfu.RestAPIFactory import org.mariotaku.restfu.RestRequest import org.mariotaku.restfu.http.Authorization @@ -29,11 +28,13 @@ import org.mariotaku.twidere.util.MicroBlogAPIFactory import org.mariotaku.twidere.util.MicroBlogAPIFactory.sFanfouConstantPool import org.mariotaku.twidere.util.MicroBlogAPIFactory.sTwitterConstantPool import org.mariotaku.twidere.util.TwitterContentUtils -import org.mariotaku.twidere.util.api.UserAgentExtraHeaders +import org.mariotaku.twidere.util.api.* import org.mariotaku.twidere.util.dagger.DependencyHolder import org.mariotaku.twidere.util.media.TwidereMediaDownloader /** + * Creates [MicroBlog] instances + * * Created by mariotaku on 2016/12/3. */ fun Credentials.getAuthorization(cls: Class<*>?): Authorization { @@ -175,11 +176,10 @@ fun newMicroBlogInstance(context: Context, endpoint: Endpoint, auth: Authori } } } - val converterFactory = TwitterConverterFactory() - factory.setRestConverterFactory(converterFactory) - factory.setRestRequestFactory(MicroBlogAPIFactory.TwidereRestRequestFactory(extraRequestParams)) - factory.setHttpRequestFactory(MicroBlogAPIFactory.TwidereHttpRequestFactory(extraHeaders)) - factory.setExceptionFactory(MicroBlogAPIFactory.TwidereExceptionFactory(converterFactory)) + factory.setRestConverterFactory(TwitterConverterFactory) + factory.setExceptionFactory(TwidereExceptionFactory) + factory.setRestRequestFactory(TwidereRestRequestFactory(extraRequestParams)) + factory.setHttpRequestFactory(TwidereHttpRequestFactory(extraHeaders)) return factory.build(cls) } diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereExceptionFactory.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereExceptionFactory.kt new file mode 100644 index 000000000..8accb6e07 --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereExceptionFactory.kt @@ -0,0 +1,66 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.util.api + +import com.fasterxml.jackson.core.JsonParseException +import org.mariotaku.microblog.library.MicroBlogException +import org.mariotaku.restfu.ExceptionFactory +import org.mariotaku.restfu.RestConverter +import org.mariotaku.restfu.http.HttpRequest +import org.mariotaku.restfu.http.HttpResponse +import java.io.IOException + +/** + * Create `MicroBlogException` + * + * Created by mariotaku on 2017/4/11. + */ +object TwidereExceptionFactory : ExceptionFactory { + + override fun newException(cause: Throwable?, request: HttpRequest?, response: HttpResponse?): MicroBlogException { + val te = if (cause != null) { + MicroBlogException(cause) + } else if (response != null) { + parseTwitterException(response) + } else { + MicroBlogException() + } + te.httpRequest = request + te.httpResponse = response + return te + } + + + fun parseTwitterException(resp: HttpResponse): MicroBlogException { + try { + val converter = TwitterConverterFactory.forResponse(MicroBlogException::class.java) + return converter.convert(resp) as MicroBlogException + } catch (e: JsonParseException) { + return MicroBlogException("Malformed JSON Data", e) + } catch (e: IOException) { + return MicroBlogException("IOException while throwing exception", e) + } catch (e: RestConverter.ConvertException) { + return MicroBlogException(e) + } catch (e: MicroBlogException) { + return e + } + + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereHttpRequestFactory.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereHttpRequestFactory.kt new file mode 100644 index 000000000..ccd9aa6ab --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereHttpRequestFactory.kt @@ -0,0 +1,58 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.util.api + +import org.mariotaku.microblog.library.MicroBlogException +import org.mariotaku.restfu.RestConverter +import org.mariotaku.restfu.RestFuUtils +import org.mariotaku.restfu.RestRequest +import org.mariotaku.restfu.http.Authorization +import org.mariotaku.restfu.http.Endpoint +import org.mariotaku.restfu.http.HttpRequest +import org.mariotaku.restfu.http.MultiValueMap +import org.mariotaku.twidere.util.MicroBlogAPIFactory + +/** + * Created by mariotaku on 2017/4/11. + */ +class TwidereHttpRequestFactory( + private val extraHeaders: MicroBlogAPIFactory.ExtraHeaders? +) : HttpRequest.Factory { + + override fun create(endpoint: Endpoint, info: RestRequest, authorization: Authorization?, + converterFactory: RestConverter.Factory): HttpRequest { + val restMethod = info.method + val url = Endpoint.constructUrl(endpoint.url, info) + var headers: MultiValueMap? = info.headers + if (headers == null) { + headers = MultiValueMap() + } + + if (authorization != null && authorization.hasAuthorization()) { + headers.add("Authorization", RestFuUtils.sanitizeHeader(authorization.getHeader(endpoint, info))) + } + if (extraHeaders != null) { + for (pair in extraHeaders.get()) { + headers.add(pair.first, RestFuUtils.sanitizeHeader(pair.second)) + } + } + return HttpRequest(restMethod, url, headers, info.getBody(converterFactory), null) + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereRestRequestFactory.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereRestRequestFactory.kt new file mode 100644 index 000000000..e6a40d87c --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwidereRestRequestFactory.kt @@ -0,0 +1,57 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.util.api + +import org.mariotaku.microblog.library.MicroBlogException +import org.mariotaku.restfu.RestConverter +import org.mariotaku.restfu.RestMethod +import org.mariotaku.restfu.RestRequest +import org.mariotaku.restfu.http.ValueMap + +/** + * Convert rest method to rest request + * + * Created by mariotaku on 2017/4/11. + */ +class TwidereRestRequestFactory( + private val extraRequestParams: Map? +) : RestRequest.Factory { + + override fun create(restMethod: RestMethod, + factory: RestConverter.Factory, valuePool: ValueMap): RestRequest { + val method = restMethod.method + val path = restMethod.path + val headers = restMethod.getHeaders(valuePool) + val queries = restMethod.getQueries(valuePool) + val params = restMethod.getParams(factory, valuePool) + val rawValue = restMethod.rawValue + val bodyType = restMethod.bodyType + val extras = restMethod.extras + + if (queries != null && extraRequestParams != null) { + for ((key, value) in extraRequestParams) { + queries.add(key, value) + } + } + + return RestRequest(method.value, method.allowBody, path, headers, queries, + params, rawValue, bodyType, extras) + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwitterConverterFactory.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwitterConverterFactory.kt new file mode 100644 index 000000000..373ed305b --- /dev/null +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/api/TwitterConverterFactory.kt @@ -0,0 +1,78 @@ +/* + * Twidere - Twitter client for Android + * + * Copyright (C) 2012-2017 Mariotaku Lee + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package org.mariotaku.twidere.util.api + +import android.support.v4.util.SimpleArrayMap + +import org.mariotaku.microblog.library.MicroBlogException +import org.mariotaku.microblog.library.twitter.model.ResponseCode +import org.mariotaku.microblog.library.twitter.model.TwitterResponse +import org.mariotaku.microblog.library.twitter.util.OAuthTokenResponseConverter +import org.mariotaku.restfu.RestConverter +import org.mariotaku.restfu.http.HttpResponse +import org.mariotaku.restfu.http.mime.Body +import org.mariotaku.restfu.http.mime.SimpleBody +import org.mariotaku.restfu.logansqaure.LoganSquareConverterFactory +import org.mariotaku.restfu.oauth.OAuthToken + +import java.lang.reflect.Type + +/** + * Convert JSON responses + * + * Created by mariotaku on 15/5/5. + */ +object TwitterConverterFactory : LoganSquareConverterFactory() { + + private val responseConverters = SimpleArrayMap>() + private val sBodyConverters = SimpleArrayMap>() + + init { + responseConverters.put(ResponseCode::class.java, ResponseCode.ResponseConverter()) + responseConverters.put(OAuthToken::class.java, OAuthTokenResponseConverter()) + } + + @Throws(RestConverter.ConvertException::class) + override fun forResponse(type: Type): RestConverter { + val converter = responseConverters.get(type) + if (converter != null) { + return converter + } + return super.forResponse(type) + } + + @Throws(RestConverter.ConvertException::class) + override fun forRequest(type: Type): RestConverter<*, Body, MicroBlogException> { + val converter = sBodyConverters.get(type) + if (converter != null) { + return converter + } + if (SimpleBody.supports(type)) { + return SimpleBodyConverter(type) + } + return super.forRequest(type) + } + + override fun processParsedObject(obj: Any, httpResponse: HttpResponse) { + if (obj is TwitterResponse) { + obj.processResponseHeader(httpResponse) + } + } +} diff --git a/twidere/src/main/kotlin/org/mariotaku/twidere/util/qr/QrCodeData.kt b/twidere/src/main/kotlin/org/mariotaku/twidere/util/qr/QrCodeData.kt index ed901bfdd..db8ada3b2 100644 --- a/twidere/src/main/kotlin/org/mariotaku/twidere/util/qr/QrCodeData.kt +++ b/twidere/src/main/kotlin/org/mariotaku/twidere/util/qr/QrCodeData.kt @@ -23,19 +23,15 @@ import io.nayuki.qrcodegen.QrCode import org.mariotaku.uniqr.QrData /** + * Wrapper for `QrCode` + * * Created by mariotaku on 2017/4/10. */ data class QrCodeData(private val qrCode: QrCode) : QrData { - override fun getSize(): Int { - return qrCode.size - } + override fun getSize() = qrCode.size - override fun getVersion(): Int { - return qrCode.version - } + override fun getVersion() = qrCode.version - override fun get(x: Int, y: Int): Boolean { - return qrCode.getModule(x, y) == 1 - } + override fun get(x: Int, y: Int) = qrCode.getModule(x, y) == 1 }