All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.polyapi.client.internal.model.PolyContext Maven / Gradle / Ivy

The newest version!
package io.polyapi.client.internal.model;

import io.polyapi.client.api.model.function.AudienceTokenAuthFunction;
import io.polyapi.client.api.model.function.PolyApiFunction;
import io.polyapi.client.api.model.function.PolyCustomFunction;
import io.polyapi.client.api.model.function.PolyServerFunction;
import io.polyapi.client.api.model.function.SubresourceAuthFunction;
import io.polyapi.client.api.model.function.TokenAuthFunction;
import io.polyapi.client.api.model.variable.ServerVariableHandler;
import io.polyapi.client.api.model.websocket.PolyTrigger;
import io.polyapi.client.internal.proxy.PolyProxyFactory;
import io.polyapi.client.internal.service.InvocationServiceImpl;
import io.polyapi.client.internal.service.VariableInjectionServiceImpl;
import io.polyapi.commons.api.error.PolyApiException;
import io.polyapi.commons.api.http.HttpClient;
import io.polyapi.commons.api.json.JsonParser;
import io.polyapi.commons.api.model.PolyErrorEvent;
import io.polyapi.commons.api.websocket.Handle;
import io.polyapi.commons.api.websocket.WebSocketClient;
import io.polyapi.commons.internal.http.DefaultHttpClient;
import io.polyapi.commons.internal.http.HardcodedTokenProvider;
import io.polyapi.commons.internal.http.HttpClientConfiguration;
import io.polyapi.commons.internal.json.JacksonJsonParser;
import io.polyapi.commons.internal.websocket.SocketIOWebSocketClient;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Optional;
import java.util.Properties;
import java.util.function.Consumer;

public class PolyContext {
    private final PolyProxyFactory proxyFactory;
    private final WebSocketClient webSocketClient;

    public PolyContext() {
        this(Optional.of(new Properties())
                .map(properties -> {
                    try {
                        properties.load(PolyContext.class.getResourceAsStream("/poly.properties"));
                        return new PolyContextConfiguration(properties);
                    } catch (IOException e) {
                        throw new PolyApiException(e);
                    }
                }).orElseThrow(PolyApiException::new), new JacksonJsonParser());
    }

    private PolyContext(PolyContextConfiguration config, JsonParser jsonParser) {
        this(config.getHost(), config.getPort(), config.getClientId(), new DefaultHttpClient(HttpClientConfiguration.builder(config.getApiKey())
                .withConnectTimeoutMillis(config.getConnectionTimeoutMillis())
                .withReadTimeoutMillis(config.getReadTimeoutMillis())
                .withWriteTimeoutMillis(config.getWriteTimeoutMillis())
                .build()), new SocketIOWebSocketClient(config.getUrl(), config.getClientId(), new HardcodedTokenProvider(config.getApiKey()), jsonParser, config.getConnectionTimeoutMillis()), jsonParser);
    }

    private PolyContext(String host, Integer port, String clientId, HttpClient httpClient, WebSocketClient webSocketClient, JsonParser jsonParser) {
        this(new PolyProxyFactory(new InvocationServiceImpl(httpClient, jsonParser, host, port, clientId, webSocketClient, new VariableInjectionServiceImpl()), webSocketClient), webSocketClient);
    }

    public PolyContext(PolyProxyFactory proxyFactory, WebSocketClient webSocketClient) {
        this.proxyFactory = proxyFactory;
        this.webSocketClient = webSocketClient;
    }

    protected  T createServerFunctionProxy(Class polyInterface) {
        return proxyFactory.createServerFunctionProxy(polyInterface);
    }

    protected  T createApiFunctionProxy(Class polyInterface) {
        return proxyFactory.createApiFunctionProxy(polyInterface);
    }

    protected  T createCustomFunctionProxy(Class polyInterface) {
        return proxyFactory.createCustomVariableProxy(polyInterface);
    }

    protected > H createServerVariableHandler(Class polyInterface) {
        return proxyFactory.createServerVariableHandler(polyInterface);
    }

    protected  T createServerVariableProxy(String type, String packageName) {
        return proxyFactory.createServerVariableProxy(type, packageName);
    }

    protected  T createPolyTriggerProxy(Class polyInterface) {
        return proxyFactory.createPolyTrigger(polyInterface);
    }

    protected  T createTokenAuthFunction(Class polyInterface) {
        return proxyFactory.createTokenAuthProxy(polyInterface);
    }

    protected  T createAudienceTokenAuthFunction(Class polyInterface) {
        return proxyFactory.createAudienceTokenAuthProxy(polyInterface);
    }

    protected  T createSubresourceAuthFunction(Class polyInterface) {
        return proxyFactory.createSubresourceAuthProxy(polyInterface);
    }

    protected  T createSubContext(Class polyContextType) {
        try {
            return polyContextType.getDeclaredConstructor(PolyProxyFactory.class, WebSocketClient.class).newInstance(proxyFactory, webSocketClient);
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException |
                 NoSuchMethodException e) {
            // FIXME: Throw appropriate exception.
            throw new PolyApiException(e);
        }
    }

    protected Handle addErrorListener(String path, Consumer errorListener) {
        return webSocketClient.registerErrorHandler(path, errorListener);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy