Home

Awesome

English | 中文版

License: Apache 2 Version: 1.0.0

The connector framework maps cloud APIs to local APIs based on simple configurations and flexible extension mechanisms. You can subscribe to the distribution of cloud messages as local events. You can put all the focus on business logic without taking care of server-side programming nor relational databases. The OpenAPI or message subscription process is simplified, so you can focus on service logic and promote development efficiency.

Quick start (tuya-spring-boot-starter recommended for cloud development)

Integrate Spring Boot (recommended)

  1. Configure API data source.
connector.api.base-url=https://www.xxx.com
  1. Define the Connector API, add a scanning path, and then inject Connector.
@ConnectorScan(basePackages = "com.xxx.connector")
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

public interface Connector {
    @GET("/test/{s}")
    String test(@Path("s") String s);
}

@Service
public class Service {
    @Autowired
    private Connector connector;
    
    public String test(String s) {
    	return connector.test(s);	
    }
}

Configure a generic Java project

Create a ConnectorFactory based on the data source configurations, load Connector with ConnectorFactory to get the Connector proxy, and then make API requests by using the Connector proxy class.

public interface Connector {
    @GET("/test/{s}")
    String test(@Path("s") String s);
}

public class Demo {
	public static void main(String[] args) {
        // initialize configuration
        Configuration config = new Configuration();
        ApiDataSource dataSource = ApiDataSource.DEFAULT_BUILDER.build();
        dataSource.setBaseUrl("https://www.xxx.com");
        config.setApiDataSource(dataSource);

        // create ConnectorFactory
        ConnectorFactory connectorFactory = new DefaultConnectorFactory(config);

        // Load connector and create connector proxy
        connector = connectorFactory.loadConnector(Connector.class);

        // call API
        String result = connector.test("hello");
    }
}

Features

Design description

  1. The cloud RESTful API is mapped to the local Connector API. The local Connector API is declared with HTTP annotations. The framework creates a proxy for the Connector API at runtime to process calls to the cloud RESTful API.
  2. Retrofit 2 is used to process underlying requests. Similar to Retrofit 2 projects, cloud APIs are called by API operations and annotated methods.
  3. Flexible extension mechanisms are supported: i. ContextManager ii. TokenManager iii. HeaderProcessor iv. ErrorProcessor v. ConnectorInterceptor
  4. The framework can be used to sequentially subscribe to the unified cloud messaging model, parse message data, identify and construct specific message events, and then distribute local events.

Architecture of the framework

Integration of the framework Integration and extensions

Core module design

API connector model

API connector model

Includes the configuration items and integrated extensions, such as the URL connections, access key ID (AK) and secret access key (SK), timeout, connection pool, logs, TokenManager, HeaderProcessor, and ContextManager.

Implements cloud development with the Connector framework and provides the logic to process and respond to cloud RESTful API requests.

RESTful API annotations and parsing for cloud development. The following annotations are available: GET, POST, PUT, DELETE, Header, HeaderMap, Headers, Body, Query, QueryMap, Path, and Url.

AnnotationDescriptionExample
GETHTTP GET method@GET("/test/annotations/get")<br />Boolean get();
POSTHTTP POST method@POST("/test/annotations/post")<br />Boolean post();
PUTHTTP PUT method@PUT("/test/annotations/put")<br />Boolean put();
DELETEHTTP DELETE method@DELETE("/test/annotations/delete")<br />Boolean delete();
PathMapping of the path parameter in the request@GET("/test/annotations/path/{path_param}")<br />String path(@Path("path_param") String pathParam);
QueryMapping between the method parameter and the query string of the request URL@GET("/test/annotations/query")<br />String query(@Query("param") String param);
QueryMapMapping between the method parameter and the query string of the request URL@GET("/test/annotations/queryMap")<br />String queryMap(@QueryMap Map<String, Object> map);
HeaderMapping between the method parameter and the request header@GET("/test/annotations/header")<br />String header(@Header("headerKey") String header);
HeadersMapping between the annotations parameter and the request headers@Headers("headerKey:headerValue")<br />@GET("/test/annotations/headers")<br />String headers();
HeaderMapMapping between the method parameter and the request header@GET("/test/annotations/headerMap")<br />String headerMap(@HeaderMap Map<String, String> headerMap);
UrlMapping between the method parameter and the request URL@GET <br />String url(@Url String url);
BodyMapping between the method parameter and the request body@POST("/test/annotations/body")<br />String body(@Body Object body);

The extensions to the framework, including ConnectorInterceptor and ErrorProcessor.

Messaging connector model

Messaging connector model

Modules

Function plan