Skip to content

Instantly share code, notes, and snippets.

@pedropombeiro
Last active December 16, 2016 06:37
Show Gist options
  • Save pedropombeiro/4804a508974d325080d9 to your computer and use it in GitHub Desktop.
Save pedropombeiro/4804a508974d325080d9 to your computer and use it in GitHub Desktop.
Spark Retrofit REST interface
RequestInterceptor requestInterceptor = new RequestInterceptor() {
@Override
public void intercept(RequestFacade request) {
String authenticationToken = ...;
if (authenticationToken != "")
request.addHeader("Authorization", String.format("Bearer %s", authenticationToken));
}
};
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint("https://api.spark.io")
.setRequestInterceptor(requestInterceptor)
.build();
this.sparkService = restAdapter.create(SparkService.class);
import java.util.List;
import retrofit.Callback;
import retrofit.client.Response;
import retrofit.http.Field;
import retrofit.http.FormUrlEncoded;
import retrofit.http.GET;
import retrofit.http.Header;
import retrofit.http.Multipart;
import retrofit.http.POST;
import retrofit.http.PUT;
import retrofit.http.Part;
import retrofit.http.Path;
import retrofit.mime.TypedOutput;
public interface SparkService {
@GET("/v1/devices")
void getDevices(Callback<List<SparkDevice>> callback);
@GET("/v1/devices/{deviceId}/{variable}")
void getVariable(@Path("variable") String variable, @Path("deviceId") String deviceId, Callback<SparkVariable> callback);
@FormUrlEncoded
@POST("/v1/devices/{deviceId}/{function}")
void invokeFunction(@Path("deviceId") String deviceId, @Path("function") String function, @Field("args") String args, Callback<Response> callback);
@Multipart
@PUT("/v1/devices/{deviceId}")
void flashFirmware(@Part("file") TypedOutput firmware, @Path("deviceId") String deviceId, Callback<UploadSparkFirmwareResponse> callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment