Skip to content

Instantly share code, notes, and snippets.

View naturalwarren's full-sized avatar
🎯
Focusing

Warren Smith naturalwarren

🎯
Focusing
View GitHub Profile
@naturalwarren
naturalwarren / UIComponent.py
Last active April 8, 2023 19:19
SDUI Base Component Compatibility
class UIComponent(BaseModel, ABC):
# ...
def __init__(self, **data) -> None:
# SDUIContext is set as a context var when handling any request on the backend
current_context = SDUIContext.current()
if current_context is not None:
# Will raise an error if incompatible
current_context.validate_compatibility(
@naturalwarren
naturalwarren / DataRow.py
Last active April 18, 2023 23:05
SDUI DataRow Type
class DataRow(UIComponent):
title: str
subtitle: str
layout: Layout = Layout.HORIZONTAL
@classmethod
def component_type(cls) -> ComponentType:
return ComponentType.DATA_ROW_STACKED
class Layout(str, Enum):
@naturalwarren
naturalwarren / UIComponent.py
Last active April 8, 2023 19:17
SDUI UI Base Component Type
class UIComponent(BaseModel, ABC):
sdui_component_type: ComponentType
def __init__(self, **data) -> None:
data['sdui_component_type'] = self.component_type()
super().__init__(**data)
@classmethod
@abstractmethod
@naturalwarren
naturalwarren / KotlinRxJava2CallAdapterFactory.kt
Last active January 28, 2019 22:15
A CallAdapterFactory capable of creating NetworkResponse instances.
/**
* A [CallAdapter.Factory] which allows [NetworkResponse] objects to be
* returned from RxJava streams created by Retrofit.
*
* Adding this class to [Retrofit] allows you to write service methods like:
*
* fun getTokens(): Single<NetworkResponse<AccessToken,Error>>
*/
class KotlinRxJava2CallAdapterFactory : CallAdapter.Factory() {
@naturalwarren
naturalwarren / ApiKotlin.kt
Last active January 24, 2019 00:58
Making a Retrofit API call with RxJava with a Kotlin-esque API
authApi.getTokens()
.subscribe { response : NetworkResponse<AccessToken, Error> ->
when (response) {
is NetworkResponse.Success<AccessToken> -> {
// A 2XX response that's guaranteed to have a body of type AccessToken.
}
is NetworkResponse.ServerError<Error> -> {
// A non-2XX response that may have an Error as its error body.
}
is NetworkResponse.NetworkError -> {
@naturalwarren
naturalwarren / CoinbaseRxJava2CallAdapterFactory.kt
Last active March 16, 2022 04:58
A Kotlin-esque API for Retrofit.
/**
* Copyright 2019 Coinbase, 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,
/**
* Decorates stream emissions from [delegateAdapter] in [CoinbaseResponse].
*/
internal class CoinbaseRxJava2CallAdapter(
private val successBodyType: Type,
private val delegateAdapter: CallAdapter<Any, Any>,
private val errorConverter: Converter<ResponseBody, Any?>,
private val supportedType: SupportedType<Any, Any>
) : CallAdapter<Any, Any> {
/**
* A [CallAdapter.Factory] which allows [CoinbaseResponse] objects to be
* returned from RxJava streams. For the type
* [Observable<CoinbaseResponse<SuccessBody, ErrorBody>>], the following
* semantics are provided:
*
* 1. 2xx responses call onNext with the deserialized body set as
* [CoinbaseResponse.body]
* 2. non-2xx responses call onNext with the deserialized error body set as
* [CoinbaseResponse.errorBody]
/**
* A response from the Coinbase API.
*
* In the event that a 2xx is returned [isSuccessful] will be set to true
* and [body] will be set.
* In the event that a non-2xx response is returned [isSuccessful] will be
* set to false and [errorBody] will be set.
* In the event that a request did not result in a response from the monorail
* [networkError] will be set.
*/
@naturalwarren
naturalwarren / CoinbaseRxJavaCallAdapter.kt
Created December 2, 2018 21:42
A custom CallAdapter.
internal class CoinbaseRxJavaCallAdapter(
private val successBodyType: Type,
private val delegateAdapter: CallAdapter<Any, Any>,
private val errorConverterFactory: Converter<ResponseBody, Any?>,
private val isObservable: Boolean,
private val isFlowable: Boolean,
private val isSingle: Boolean,
private val isMaybe: Boolean
) : CallAdapter<Any, Any> {