Need guidance on component that will perform an http GET on a given endpoint and fill the result in a new Profile field

bryce alvord Posted in Component Development 8 months ago

So at a high level, I created an OSSN site for my club and club members have differing dues that I want to keep track of and show in their Profile panel. The values will be pulled from a Google Sheets file using this call which is pointing to a dummy test document but uses this same schema so to speak where the name of the person is in column A as LAST,FIRST and column B is balance due in $###.## format.

GET https://sheets.googleapis.com/v4/spreadsheets/1bH6LmIsDQQdd1Beiq1s9fI-SuYRoGYmBc-_IYrpux_g/values/A1%3AB40

Sample response

{
 "range": "Sheet1!A1:B40",
 "majorDimension": "ROWS",
 "values": [
  [
   "Name",
   "Balance"
  ],
  [
   "Allred,Steve",
   "$100.00"
  ],
  [
   "Kuchner,John",
   "$400.00"
  ],
  [
   "Smith,Alex",
   "$300.00"
  ],
  [
   "Thomas,Jane",
   "$750.00"
  ],
  [
   "Williams,Sally",
   "$600.00"
  ]
 ]
}

I am going to call this API endpoint when the Profile link to take them into their profile is clicked, match the logged in user name to the name in column A and in a readonly field that will be new add the Balance amount.

Now, here is where I am struggling. I am a software engineer by trade but only typically do MSSQL and .net programming, I dont know much except very basics of PHP and I know even less of the OSSN framework. If someone could help me get started with a template or kind of a high level walk through to do this that would be amazing.

Replies
us Bansh33 - Replied 8 months ago

Since you want to display the data returned by that API call in a user's profile, I suggest you look at the "AboutUser" component that's available for free here. It adds an "About" section to each user's profile which you can customize to your needs (either by performing a live http call and displaying the result accordingly, or pulling the data from your database if you decide to go the crontab route). If all you want to do is display the result of that http call in a user's profile, it should be pretty easy to edit the component I mentioned for your needs. And as Arsalan said, the HelloWorld component is useful for learning the general structure of how components are built/packaged.

us Bryce alvord Replied 8 months ago

Got it, thanks for the reply. I will take a look at the HelloWorld component now. Thanks!

German Arsalan Shah Replied 8 months ago

Suppose there are 100K users into your website and you only need balance sheet of 40 users, and the API return data really fast then its fine on some point.

To learn component see community free components specially component starts HelloWorld. And must ask questions here on community. (Not design problem or entire component development :) )

If you are developer find out what are the basic things you need in general application development?

Like : How to add users data into database? how to get user from database? etc.... once you have these questions and you can ask them how you can achieve in OSSN.

us Bryce alvord Replied 8 months ago

Hey thanks for the reply Arsalan, if it helps, there will only ever be a max of 40 key/value pairs and really more like 25 entries for users and balances. Would you think that would be too much for a front end driven flow? Im not against storing it in the db and performing it every so often for a generic purpose but I know there are a couple of grumpy users that would complain if they look at the balance and it updates an hour later to a higher amount and they dont see that for another day if that makes sense.

On a sidenote, is the best way to learn component development to go through the github documentation or just to scan through existing components or is there some other method. I know this may not ever be useful to someone else but I do still want to make sure I code it up to par with what is expected.

German Arsalan Shah Replied 8 months ago

If your data returned by API is so large it will surely not recommended to do on front-end. I will go for the way banish33 suggested. Getting the data on server-side (and update on the x intervals) and then save those settings for user in database.

You can use example

$user = ossn_user_by_username('username-goes-here');
$user->data->balance = $api->balance;
$user->save();

After it saved you can get the data on specific page following way:

$user = ossn_user_by_username('....');
echo $user->balance;
us Bryce alvord Replied 8 months ago

Thanks for the reply! Yes it is just JSON so that decode function should work well. Because the GET call returns quickly and the array will be small, Im guessing the logic to match the logged in user to the name in the array will be fast as well so I am planning on just making this a live call that happens when you open a profile page. Im confused on the component side how to do this a bit. I know enough that I could have this working in no time if I was just modifying the core files directly but I hear that is a no no so thats where I am confused because a components structure looks dissimilar to the paths of files and I dont know how they are injected into existing core files.

us Bansh33 - Replied 8 months ago

that response data looks like it's in JSON format. This is a useful PHP function to deal with JSON data: http://php.net/json_decode - it will allow you to convert that data into a php array which you can then easily iterate over. Then, you could have your php script run whatever mysql queries you want, maybe updating certain profile fields or something so they could be displayed through user profile's on the OSSN website. one way to do this would be to run a php script that gets the json data and then does everything I described in a crontab, so that it would execute however often you want (you could set it to update once a day, or once an hour, depending on how often you expect that data to change). I'm not sure what platform you're running, but in general you can find out what's currently in your crontab with crontab -l and add new entries with crontab -e.