use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class StoreLoginManager method isValidLogin.
/**
* Calls the permissions web service to determine if the user provided has editing capabilities
*
* @return editing capabilities or not (false also if an retrofit error occurred)
*/
@Override
public boolean isValidLogin(final String login, final String password) {
try {
Call<OsmDto> callPerms;
OsmDto permissions;
Map<String, String> oAuthParams = loginPreferences.retrieveOAuthParams();
// OAuth connection
if (oAuthParams != null) {
String requestUrl = BuildConfig.BASE_OSM_URL + "permissions";
OAuthRequest oAuthRequest = new OAuthRequest(oAuthParams.get(CONSUMER_PARAM), oAuthParams.get(CONSUMER_SECRET_PARAM));
oAuthRequest.initParam(OAuthParams.getOAuthParams().put(TOKEN_PARAM, oAuthParams.get(TOKEN_PARAM)).toMap());
oAuthRequest.setOAuthToken(oAuthParams.get(TOKEN_PARAM));
oAuthRequest.setOAuthTokenSecret(oAuthParams.get(TOKEN_SECRET_PARAM));
oAuthRequest.setRequestUrl(requestUrl);
oAuthRequest.signRequest(Verb.GET);
oAuthRequest.encodeParams();
String headerRequest = oAuthRequest.getOAuthHeader();
callPerms = osmRestClient.getPermissions(headerRequest);
} else {
// Basic Auth connection
String authorization = "Basic " + Base64.encodeToString((login.trim() + ":" + password).getBytes(), Base64.NO_WRAP);
callPerms = osmRestClient.getPermissions(authorization);
}
Response<OsmDto> response = callPerms.execute();
if (response.isSuccessful()) {
permissions = response.body();
if (permissions != null) {
if (permissions.getPermissionsDto() != null && permissions.getPermissionsDto().getPermissionDtoList() != null) {
for (PermissionDto permissionDto : permissions.getPermissionsDto().getPermissionDtoList()) {
if ("allow_write_api".equals(permissionDto.getName())) {
return true;
}
}
}
}
}
} catch (IOException e) {
Timber.e("Couldn't request permissions " + e);
}
return false;
}
use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class OSMSyncWayManager method getPoiWaysToUpdate.
/**
* Download from backend all the NodeRefs to update as POIs.
*
* @return The list of POIs to update.
*/
private List<Poi> getPoiWaysToUpdate() {
final List<Long> ids = poiNodeRefDao.queryAllUpdated();
List<Poi> pois = new ArrayList<>();
if (ids != null && !ids.isEmpty()) {
Call<OsmDto> callOsm = osmRestClient.getNode(formatIdList(ids));
try {
Response<OsmDto> response = callOsm.execute();
if (response.isSuccessful()) {
OsmDto osmDto = response.body();
if (osmDto != null) {
List<NodeDto> nodeDtoList = osmDto.getNodeDtoList();
pois = poiMapper.convertDtosToPois(nodeDtoList, false);
}
return pois;
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
Timber.w("The poi with id %s couldn't be found on OSM", 1);
}
return pois;
}
use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class OsmBackend method initializeTransaction.
/**
* {@inheritDoc}
*/
@Override
public String initializeTransaction(String comment) {
final OsmDto osmDto = new OsmDto();
ChangeSetDto changeSetDto = new ChangeSetDto();
List<TagDto> tagDtos = new ArrayList<>();
osmDto.setChangeSetDto(changeSetDto);
tagDtos.add(new TagDto(comment, "comment"));
tagDtos.add(new TagDto(BuildConfig.APP_NAME + " " + BuildConfig.VERSION_NAME, "created_by"));
changeSetDto.setTagDtoList(tagDtos);
Call<ResponseBody> callChangeSet;
if (loginPreferences.retrieveOAuthParams() != null) {
callChangeSet = osmRestClient.addChangeSet(AuthenticationRequestInterceptor.getOAuthRequest(loginPreferences, BuildConfig.BASE_OSM_URL + "changeset/create", Verb.PUT).getOAuthHeader(), osmDto);
} else {
callChangeSet = osmRestClient.addChangeSet(osmDto);
}
try {
Response<ResponseBody> response = callChangeSet.execute();
if (response.isSuccessful() && response.body() != null) {
return response.body().string();
}
} catch (IOException e) {
Timber.e("Retrofit error, couldn't create Changeset!");
}
bus.post(new SyncUploadRetrofitErrorEvent(-1L));
return null;
}
use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class OSMSyncNoteManager method syncDownloadNotesInBox.
/**
* {@inheritDoc}
*/
@Override
public List<Note> syncDownloadNotesInBox(final Box box) {
Timber.d("Requesting osm for notes download");
String strBox = box.getWest() + "," + box.getSouth() + "," + box.getEast() + "," + box.getNorth();
Call<OsmDto> osmDtoCall = osmRestClient.getNotes(strBox);
try {
Response<OsmDto> response = osmDtoCall.execute();
if (response.isSuccessful()) {
OsmDto osmDto = response.body();
if (osmDto != null) {
List<NoteDto> noteDtos = osmDto.getNoteDtoList();
if (noteDtos != null && !noteDtos.isEmpty()) {
Timber.d("Updating %d note(s)", noteDtos.size());
bus.post(new SyncDownloadRetrofitErrorEvent());
return noteMapper.convertNoteDtosToNotes(noteDtos);
}
}
}
} catch (IOException e) {
Timber.e(e, e.getMessage());
}
return null;
}
use of io.jawg.osmcontributor.rest.dtos.osm.OsmDto in project osm-contributor by jawg.
the class OSMSyncNoteManager method remoteAddComment.
/**
* {@inheritDoc}
*/
public Note remoteAddComment(final Comment comment) {
Call<OsmDto> osmDtoCall;
switch(comment.getAction()) {
case Comment.ACTION_CLOSE:
osmDtoCall = osmRestClient.closeNote(comment.getNote().getBackendId(), comment.getText(), "");
break;
case Comment.ACTION_REOPEN:
osmDtoCall = osmRestClient.reopenNote(comment.getNote().getBackendId(), comment.getText(), "");
break;
case Comment.ACTION_OPEN:
osmDtoCall = osmRestClient.addNote(comment.getNote().getLatitude(), comment.getNote().getLongitude(), comment.getText(), "");
break;
default:
osmDtoCall = osmRestClient.addComment(comment.getNote().getBackendId(), comment.getText(), "");
break;
}
try {
Response<OsmDto> response = osmDtoCall.execute();
if (response.isSuccessful()) {
OsmDto osmDto = response.body();
if (osmDto != null) {
NoteDto noteDto = osmDto.getNoteDtoList().get(0);
Note note = noteMapper.convertNoteDtoToNote(noteDto);
note.setUpdated(false);
note.setId(comment.getNote().getId());
return note;
}
} else if (response.code() == 409) {
bus.post(new SyncConflictingNoteErrorEvent(comment.getNote()));
}
} catch (IOException e) {
Timber.e(e, "Retrofit error, couldn't create comment !");
}
bus.post(new SyncUploadNoteRetrofitErrorEvent(comment.getNote().getId()));
return null;
}
Aggregations