0

Background: I'm working on an Android project that was handed off to me and there are some things I'm still trying to learn. The people on the project before me left no documentation, so forgive me.

Question: Basically I want to know how to get data back from our MongoDB (I think). Using AsyncHttpClient params are put sent via post

private AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("authorization", getSharedPreferences("Prefs", Context.MODE_PRIVATE).getString("authToken", ""));
params.put("name", name);
params.put("email", email);
client.post(Utilities.getBaseURL() + "session/updateProfile", params, new JsonHttpResponseHandler() {
  @Override
  public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
    HashMap<String, String> profile = new HashMap<>();
    profile.put("user_name", name);
    profile.put("user_email", email);
    Utilities.updateAllDetails(profile);
  }

  @Override
  public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, Throwable throwable, JSONObject response) {
    Utilities.showUserDialog(context, "Profile Change Error", "Attention, there was an error in updating your profile, please try again.");
  }
});

So the above code is basically updating a profile and sending the params via http post.

Later there is a get on the client:

client.get(Utilities.getBaseURL() + "session/getUserId", new JsonHttpResponseHandler() {
  @Override
  public void onSuccess(int statusCode, cz.msebera.android.httpclient.Header[] headers, JSONObject response) {
    try {
      userId = response.getString("responseString");
      startActivityForResult(intent, PICK_IMAGE);
    } catch (JSONException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void onFailure(int statusCode, cz.msebera.android.httpclient.Header[] headers, Throwable throwable, JSONObject response) {}
});

What I don't understand is where "session/updateProfile" and "session/getUserId" come from. Is this an API call, if so how can I find what is available and also how to add to it?

Update

Found this code in a JavaScript file for the website. Maybe this can lead someone to help me put everything together?

JsonRoutes.add('get', '/api/v1/session/getUserId', function(req, res, next)     {
  var authToken = req.headers["authorization"];
  var userObj = verifyUser(authToken);
  var response = {};
  if (userObj) {
response.responseString = userObj._id;
JsonRoutes.sendResult(res, 200, response);
  } else {
response.responseString = "user not found";
JsonRoutes.sendResult(res, 200, response);
  }
   });

Do I need to add to this JS file to expand the API?

Justin
  • 315
  • 2
  • 4
  • 18
  • S3 isn't Mongo. It's a filesystem, not a database. Regarding the question, you should consider the value of `Utilities.getBaseURL()` – OneCricketeer Oct 19 '16 at 03:31
  • @cricket_007 Okay, so basically that is the base URL to the website. – Justin Oct 19 '16 at 03:39
  • Okay, you edited the question to remove S3. So now, how do you know you have Mongo? The code accesses a REST API. I'm not sure the database is really important or can be determined just by this code – OneCricketeer Oct 19 '16 at 03:48
  • @cricket_007 So Digital Ocean is also used. I'm not too familiar with it, but I know for a fact they used MongoDB. – Justin Oct 19 '16 at 03:52
  • DigitalOcean, like Amazon, is just some place that you can rent some server space in the cloud. And it costs money, which drives people to use alternative databases like Firebase. Anyways, your question - you will want to get access to that DigitalOcean box since that's what's actually communicating between Mongo and Android. – OneCricketeer Oct 19 '16 at 03:58
  • @cricket_007 Yeah I know. I believe mongo is on the Digital Ocean service, it has an online console and I can run the mongo command to get a username/password prompt. Need to figure that out now. I also made a little update, sorry. I am literally trying to reverse engineer all of this. I appreciate your help. – Justin Oct 19 '16 at 04:05
  • Alright. Would you know if NodeJS is being used as well? What is `JsonRoutes`? – OneCricketeer Oct 19 '16 at 04:13
  • @cricket_007 Sorry, I should have been more descriptive. This is a fairly big project. The web application is a NodeJS application that uses meteorJS. Then there is an Android and iOS application and then a mongoDB. Not exactly sure what JsonRoutes is, I'll look more into that. – Justin Oct 19 '16 at 04:46
  • Right, so now you see how your question wasnt really about Android and Mongo, but how this "full stack" fits together. Therefore making this a broad question. I'll try to answer based on what we've discussed, though – OneCricketeer Oct 19 '16 at 04:51

1 Answers1

1

where [do] "session/updateProfile" and "session/getUserId" come from? Is this an API call

Yup, REST API call to wherever Utilities.getBaseURL() points to.

how can I find what is available?

Well, if that server is yours, then you'll need to go find some code / documentation for that. Otherwise, if it's some other external service, you'll still have to find some API documentation or contact someone who does know about it. You've mentioned in the comments that it is DigitalOcean, so it isn't an external service. There is additional server side code to inspect.

how to add to it?

If it were an external service, you probably couldn't. Since this DigitalOcean server could be running literally any service, it entirely depends on the server side programming language. You've mentioned that Javascript file along with MongoDB, so I'm guessing it's some NodeJS variant. You mention MeteorJS in the comments, so that's the documentation you need to add new features.

Do I need to add to this JS file to expand the API?

Simply: yes.

I want to know how to get data back from our MongoDB

Essentially this line is doing that. Of course, the response code shouldn't always be 200 and the content of the response string can be anything you want, but probably should be JSON, or at least something Android can parse

JsonRoutes.sendResult(res, 200, response);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245