Created
November 12, 2013 18:39
-
-
Save GLitchfield/7436374 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public Observable<AccountBlob> accounts(final Identifier userID, | |
final GatewayID gatewayID) { | |
/* Create RestResponse Observable */ | |
final Observable<RestResponse<byte[]>> obs = send(authRequest(Method.GET, | |
"/" + gatewayID.toString() + "/" + userID.toString() + "/accounts")); | |
/* We map each RestResponse to multiple AccountBlobs | |
* | |
* Each Observable is create from an Iterable<AccountBlob> */ | |
final Func1<RestResponse<byte[]>, Observable<AccountBlob>> func = | |
new Func1<RestResponse<byte[]>, Observable<AccountBlob>>() { | |
@Override | |
public Observable<AccountBlob> call(RestResponse<byte[]> response) { | |
final Iterable<AccountBlob> iter = actIter(response.content()); | |
final Observable<AccountBlob> actObs = Observable.from(iter); | |
final Observable.OnSubscribeFunc<AccountBlob> func2 = | |
new Observable.OnSubscribeFunc<AccountBlob>() { | |
@Override | |
public Subscription onSubscribe( | |
Observer<? super AccountBlob> t1) { | |
return actObs.subscribe(t1); | |
} | |
}; | |
return Observable.create(func2); | |
} | |
}; | |
return obs.mapMany(func); | |
} | |
private final Iterable<AccountBlob> actIter(final byte[] bytes) { | |
return new Iterable<AccountBlob>() { | |
private final CodedInputStream in = | |
CodedInputStream.newInstance(bytes); | |
@Override | |
public Iterator<AccountBlob> iterator() { | |
return new Iterator<AccountBlob>(){ | |
@Override | |
public boolean hasNext() { | |
try { | |
return !in.isAtEnd(); | |
} catch (IOException e) { // Not sure how to handle checked exceptions | |
e.printStackTrace(); | |
} | |
return false; | |
} | |
@Override | |
public AccountBlob next() { | |
if(!hasNext()) { | |
return AccountBlob.NULL; | |
} | |
try { | |
final int len = in.readRawVarint32(); | |
return blobs.account(in.readRawBytes(len)); | |
} catch (IOException e) { // Not sure how to handle checked exceptions | |
e.printStackTrace(); | |
} | |
return AccountBlob.NULL; | |
} | |
@Override | |
public void remove() { | |
// Does nothing | |
} | |
}; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment