Pure Java but has support for Android • Built on the premise of annotated Interfaces • Delegate construction of concrete API client to Retrofit • Built on top of OKIO and OKHttp (optionally) Link: http://square.github.io/retrofit/
Powerful reflection based library • API convention is at the core of the library • Not every library is perfect but GSON is flexible enough to correct your ways Link: https://github.com/google/gson (New home on Github)
private final ZendeskCallback<T> callback; private static final String LOG_TAG = "SafeZendeskCallback"; /** * Constructor to wrap a {@link ZendeskCallback} so that it can be cancelled * * @param callback The callback to wrap */ public SafeZendeskCallback(ZendeskCallback<T> callback) { this.callback = callback; this.cancelled = false; }
!= null) { callback.onSuccess(result); } else { Logger.w(LOG_TAG, "Operation was a success but callback is null or was cancelled"); } } @Override public void onError(ErrorResponse error) { if (!cancelled && callback != null) { callback.onError(error); } else { Logger.e(LOG_TAG, error); } } /** * This cancels the callback. This means that the wrapped callback will not be called if this method was called. */ public void cancel() { this.cancelled = true; }
a {@linkplain SafeZendeskCallback} * * @param callback The callback to wrap * @param <T> The type of object that the onSuccess method will return * @return The SafeZendeskCallback which wraps the supplied ZendeskCallback */ public static <T> SafeZendeskCallback<T> from(ZendeskCallback<T> callback) { return new SafeZendeskCallback<>(callback); }
type {@code E} received over the network * and convert it to type {@code F} * * @param <E> type of data received through the network * @param <F> type of data that is needed by a {@link ZendeskCallback} */ public interface RequestExtractor<E, F> { F extract(E data); }
= callback; this.mExtractor = extractor; } /** * More convenient version of {@link #RetrofitZendeskCallbackAdapter(ZendeskCallback, RequestExtractor)}. * Can be used if {@code E} and {@code F} are of the same type. A {@link #DEFAULT_EXTRACTOR} will be used. * * @param callback a {@link ZendeskCallback} that should be notified if * {@link #success(Object, Response)} or * {@link #failure(RetrofitError)} was called */ public RetrofitZendeskCallbackAdapter(ZendeskCallback<F> callback) { //noinspection unchecked this(callback, DEFAULT_EXTRACTOR); }
com.google.gson.GsonBuilder builder = new com.google.gson.GsonBuilder(); builder.registerTypeAdapterFactory(new LowercaseEnumTypeAdapterFactory()); builder.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT); builder.setFieldNamingStrategy(new LocaleAgnosticFieldNamingStrategy()); Type auditListType = new TypeToken<List<AuditEvent>>(){}.getType(); builder.registerTypeAdapter(auditListType, new AuditEventDeserializer()); builder.registerTypeAdapter(TicketChangeAuditEvent.class, new TicketChangeAuditEventDeserializer()); builder.registerTypeAdapter(TicketCreationAuditEvent.class, new TicketCreateAuditEventDeserializer()); builder.registerTypeAdapter(SearchResult.class, new SearchResultDeserializer()); builder.registerTypeAdapter(Date.class, new DateTypeAdapter()); builder.registerTypeAdapter(NullableLong.class, new NullableLongTypeAdapter()); builder.registerTypeAdapter(MacroAction.class, new MacroActionDeserialiser()); builder.registerTypeAdapter(Via.class, new ViaSerializer()); return builder.create(); }
issue: https://github.com/google/gson/pull/652 */ private static class LocaleAgnosticFieldNamingStrategy implements FieldNamingStrategy { @Override public String translateName(Field field) { return separateCamelCase(field.getName(), "_").toLowerCase(Locale.ENGLISH); } /** * Converts the field name that uses camel-case define word separation into * separate words that are separated by the provided {@code separatorString}. */ private String separateCamelCase(String name, String separator) { StringBuilder translation = new StringBuilder(); for (int i = 0; i < name.length(); i++) { char character = name.charAt(i); if (Character.isUpperCase(character) && translation.length() != 0) { translation.append(separator); } translation.append(character); } return translation.toString(); } }
on the shoulder of giants • Developer Performance increased 300x ◦ Productivity has increased significantly ◦ New developers productive in a day • Serviceability ◦ Easier to debug customer issues ◦ Retrofit logging is awesome