|
| 1 | + |
| 2 | +== Streamdata.io Java SDK |
| 3 | + |
| 4 | + |
| 5 | +=== What does the API do ? |
| 6 | + |
| 7 | +It remove the plumbing between you and streamdata.io once you have an API ready to use, you can use the code above. |
| 8 | + |
| 9 | +It connects to the server with some your API URL and your credentials (API key) and you get notified using either |
| 10 | + |
| 11 | +* Some callback you provide |
| 12 | +* Trough an RxJava2 `Flowable` |
| 13 | + |
| 14 | +You can choose to receive snapshots only or one snapshot and then patches you don't even know SSE is used, you just use your data. |
| 15 | + |
| 16 | +=== Prerequisite |
| 17 | + |
| 18 | +* You have to create an account on streamdata.io and create an app using your API or one of demo sample API provided by streamdata.io |
| 19 | +* You must use Java8+ to use this API as to takes advantage |
| 20 | +of lambda expression and some specific features of java8. |
| 21 | + |
| 22 | +=== How to integrate the API into your project |
| 23 | + |
| 24 | +*Maven* |
| 25 | + |
| 26 | +[xml] |
| 27 | +---- |
| 28 | +<dependency> |
| 29 | + <groupId>io.streamdata</groupId> |
| 30 | + <artifactId>java-sdk</artifactId> |
| 31 | + <version>1.0</version> |
| 32 | +</dependency> |
| 33 | +---- |
| 34 | + |
| 35 | +*Gradle* |
| 36 | + |
| 37 | +---- |
| 38 | +dependencies { |
| 39 | + compile("io.streamdata:java-sdk:1.0") |
| 40 | +} |
| 41 | +---- |
| 42 | + |
| 43 | +Notable transitive dependencies : |
| 44 | + |
| 45 | +* Glassfish Jersey as EventSource client |
| 46 | +* Slf4j with a default configuration |
| 47 | +* RxJava 2 |
| 48 | + |
| 49 | +=== Callback based API |
| 50 | + |
| 51 | +This API is based on callbacks and is very similar to our Node.js API. |
| 52 | + |
| 53 | +[java] |
| 54 | +---- |
| 55 | +
|
| 56 | +String apiURL = "http://api.mycompany.com/prices"; |
| 57 | +String appKey = "YOUR OWN APP KEY"; # <1> |
| 58 | +
|
| 59 | +EventSourceClient client = StreamdataClient.createClient(apiURL, appKey); |
| 60 | +client.addHeader("X-MYAPI-POLLER", "Polled By SD.io") # <2> |
| 61 | + .incrementalCache(true) # <3> |
| 62 | + .onSnapshot(data -> this::processData) # <4> |
| 63 | + .onPatch(patch -> this.processPatch(patch, client.getCurrentSnaphot())) # <5> |
| 64 | + .onOpen(() -> logger.info("And we are... live!")) # <6> |
| 65 | + .onClose(() -> logger.info("And we are... live!")) # <7> |
| 66 | + .onError(err -> logger.error("An error occured {}", err)) # <8> |
| 67 | + .onException(ex -> logger.error("Unexpected error", ex)) # <9> |
| 68 | + .open(); # <10> |
| 69 | +---- |
| 70 | + |
| 71 | +<1> This the key you when you created your app in the web portal. |
| 72 | +<2> Headers to be added when polling your API (Typically some auth header) [_Optional_ ] |
| 73 | +<3> This allows you let streamdata send snapshots only (set `false` to do so) or snapshot then patch (default behaviour if you don't call this method) [_Optional_ ] |
| 74 | +<4> A callback that will consume the snaphost |
| 75 | +<5> A callback that will consume the patch (you can access the snaphost anyways) [_Optional_ ] |
| 76 | +<6> Call when successfully open the connection [_Optional_ ] |
| 77 | +<7> Call the connection is actually closed [_Optional_ ] |
| 78 | +<6> Call when the stream return an error, by default the error is logged usually it is recommended to close the connection : ```client.close()``` [_Optional_ ] |
| 79 | +<7> Call when an error occurs, mainly when opening the event source, by default the error is recommended to close the connection : ```client.close()``` [_Optional_ ] |
| 80 | +<10> Open the connection with the server |
| 81 | + |
| 82 | + |
| 83 | +=== RxJava based API |
| 84 | + |
| 85 | +This API relies on the previous one but expose a rx's Flowable interface to handle data coming from the server. |
| 86 | +Data is wrapped in a simple Event class allowing you to know is the data is an error a patch or a snapshot. |
| 87 | + |
| 88 | + |
| 89 | +[java] |
| 90 | +---- |
| 91 | +
|
| 92 | +String apiURL = "http://api.mycompany.com/prices"; |
| 93 | +String appKey = "YOUR OWN APP KEY"; # <1> |
| 94 | +
|
| 95 | +
|
| 96 | +StreamdataClient.createRxJavaClient(apiURL, appKey) |
| 97 | + .addHeader("X-MYAPI-POLLER", "Polled By SD.io") # <2> |
| 98 | + .incrementalCache(true) # <3> |
| 99 | + .toFlowable() # <4> |
| 100 | + // here you can add operator to manipulate the flow |
| 101 | + .subscribe(event -> { # <5> |
| 102 | + if (event.isSnapshot()) { |
| 103 | + this.processData(event.getSnapshot()) |
| 104 | + } else if (event.isPatch()) { |
| 105 | + this.processData(event.getPatch(), event.getSnapshot()) |
| 106 | + } else if (event.isError()) { |
| 107 | + throw new RuntimeException(event.getError()); # <6> |
| 108 | + } |
| 109 | + }, err -> logger.error(err.getMessage(), err)); |
| 110 | +
|
| 111 | +---- |
| 112 | +<1> Same as above |
| 113 | +<2> Same as above |
| 114 | +<3> Same as above |
| 115 | +<4> Once configured you can start manipulating you data |
| 116 | +<5> An example without using any rx operators before show you available methods on event |
| 117 | +<6> Shis will stop the flowable and disconnect the event source |
0 commit comments