Skip to content

Commit

Permalink
Simplified linkHqWorker to take ConnectLinkedAppRecord from caller in…
Browse files Browse the repository at this point in the history
…stead of retrieving it again.
  • Loading branch information
OrangeAndGreen committed Dec 4, 2024
1 parent 93314ed commit 906137d
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions app/src/org/commcare/connect/network/ApiConnectId.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,25 @@ public class ApiConnectId {
private static final String API_VERSION_NONE = null;
private static final String API_VERSION_CONNECT_ID = "1.0";

public static void linkHqWorker(Context context, String hqUsername, String hqPassword, String connectToken) {
String seatedAppId = CommCareApplication.instance().getCurrentApp().getUniqueId();
ConnectLinkedAppRecord appRecord = ConnectDatabaseHelper.getAppData(context, seatedAppId, hqUsername);
if (appRecord != null && !appRecord.getWorkerLinked()) {
HashMap<String, String> params = new HashMap<>();
params.put("token", connectToken);
public static void linkHqWorker(Context context, String hqUsername, ConnectLinkedAppRecord appRecord, String connectToken) {
HashMap<String, String> params = new HashMap<>();
params.put("token", connectToken);

String url = ServerUrls.getKeyServer().replace("phone/keys/",
"settings/users/commcare/link_connectid_user/");
String url = ServerUrls.getKeyServer().replace("phone/keys/",
"settings/users/commcare/link_connectid_user/");

try {
ConnectNetworkHelper.PostResult postResult = ConnectNetworkHelper.postSync(context, url,
API_VERSION_NONE, new AuthInfo.ProvidedAuth(hqUsername, hqPassword), params, true, false);
if (postResult.e == null && postResult.responseCode == 200) {
postResult.responseStream.close();
try {
ConnectNetworkHelper.PostResult postResult = ConnectNetworkHelper.postSync(context, url,
API_VERSION_NONE, new AuthInfo.ProvidedAuth(hqUsername, appRecord.getPassword()), params, true, false);
if (postResult.e == null && postResult.responseCode == 200) {
postResult.responseStream.close();

//Remember that we linked the user successfully
appRecord.setWorkerLinked(true);
ConnectDatabaseHelper.storeApp(context, appRecord);
}
} catch (IOException e) {
//Don't care for now
//Remember that we linked the user successfully
appRecord.setWorkerLinked(true);
ConnectDatabaseHelper.storeApp(context, appRecord);
}
} catch (IOException e) {
//Don't care for now
}
}

Expand Down

0 comments on commit 906137d

Please sign in to comment.