Skip to content

Commit

Permalink
Rework auth order
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Spielmann <simon@MacBook-Pro.fritz.box>
  • Loading branch information
Simon Spielmann authored and Simon Spielmann committed Nov 10, 2022
1 parent af3fbfc commit e58beff
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,69 @@
import java.net.URI;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openhab.binding.icloud.internal.json.response.ICloudDeviceInformation;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
*
* TODO
* This class gives access to the find my iPhone (FMIP) service.
*
* @author Simon Spielmann
* @author Simon Spielmann Initial Contribution.
*/
public class FindMyIPhoneServiceManager {

private ICloudSession session;

private URI fmipRefreshUrl;

private URI fmipSoundUrl;

private final static String FMIP_ENDPOINT = "/fmipservice/client/web";

private final Gson gson = new GsonBuilder().create();

private final static Logger LOGGER = LoggerFactory.getLogger(FindMyIPhoneServiceManager.class);

public FindMyIPhoneServiceManager(ICloudSession session, String serviceRoot, boolean withFamily)
throws IOException, InterruptedException {

this.session = session;
this.fmipRefreshUrl = URI.create(serviceRoot + FMIP_ENDPOINT + "/refreshClient");
this.fmipSoundUrl = URI.create(serviceRoot + FMIP_ENDPOINT + "/playSound");
}

/**
* @throws InterruptedException
* @throws IOException
*
*/
public String refreshClient() throws IOException, InterruptedException {

Map<String, Object> request = Map.of("clientContext",
Map.of("fmly", true, "shouldLocate", true, "selectedDevice", "All", "deviceListVersion", 1));

return this.session.post(this.fmipRefreshUrl.toString(), this.gson.toJson(request), null);
}

public void playSound(String deviceId) throws IOException, InterruptedException {

Map<String, Object> request = Map.of("device", deviceId, "fmyl", true);
this.session.post(this.fmipSoundUrl.toString(), this.gson.toJson(request), null);
}
private ICloudSession session;

private URI fmipRefreshUrl;

private URI fmipSoundUrl;

private final static String FMIP_ENDPOINT = "/fmipservice/client/web";

private final Gson gson = new GsonBuilder().create();

/**
* The constructor.
*
* @param session {@link ICloudSession} to use for API calls.
* @param serviceRoot Root URL for FMIP service.
*/
public FindMyIPhoneServiceManager(ICloudSession session, String serviceRoot) {

this.session = session;
this.fmipRefreshUrl = URI.create(serviceRoot + FMIP_ENDPOINT + "/refreshClient");
this.fmipSoundUrl = URI.create(serviceRoot + FMIP_ENDPOINT + "/playSound");
}

/**
* Receive client informations as JSON.
*
* @return Information about all clients as JSON {@link ICloudDeviceInformation}.
*
* @throws IOException if I/O error occurred
* @throws InterruptedException if this blocking request was interrupted
*
*/
public String refreshClient() throws IOException, InterruptedException {

Map<String, Object> request = Map.of("clientContext",
Map.of("fmly", true, "shouldLocate", true, "selectedDevice", "All", "deviceListVersion", 1));

return this.session.post(this.fmipRefreshUrl.toString(), this.gson.toJson(request), null);
}

/**
* Play sound (find my iPhone) on given device.
*
* @param deviceId ID of the device to play sound on
* @throws IOException if I/O error occurred
* @throws InterruptedException if this blocking request was interrupted
*/
public void playSound(String deviceId) throws IOException, InterruptedException {

Map<String, Object> request = Map.of("device", deviceId, "fmyl", true);
this.session.post(this.fmipSoundUrl.toString(), this.gson.toJson(request), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,28 @@
*
* Exception for errors during calls of the iCloud API.
*
* @author Simon Spielmann
* @author Simon Spielmann Initial contribution
*/
public class ICloudAPIResponseException extends RuntimeException {

private int statusCode;
private int statusCode;

/**
* The constructor.
*
* @param url URL for which the exception occured
* @param statusCode HTTP status code which was reported
*/
public ICloudAPIResponseException(String url, int statusCode) {
/**
* The constructor.
*
* @param url URL for which the exception occurred
* @param statusCode HTTP status code which was reported
*/
public ICloudAPIResponseException(String url, int statusCode) {

super(String.format("Request {} failed with {}.", url, statusCode));
this.statusCode = statusCode;
}
super(String.format("Request %s failed with %s.", url, statusCode));
this.statusCode = statusCode;
}

/**
* @return statusCode HTTP status code of failed request.
*/
public int getStatusCode() {
return this.statusCode;
}
}
Loading

0 comments on commit e58beff

Please sign in to comment.