Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Client API #17583

Merged
merged 1 commit into from
Mar 8, 2018
Merged

Add Client API #17583

merged 1 commit into from
Mar 8, 2018

Conversation

Mjloturco
Copy link
Contributor

@Mjloturco Mjloturco commented Feb 28, 2018

Adds a client api for the entitlement support tool front end to call to interact with entitlement support details endpoint

To be merged into this feature branch when approved

(part of) Learner-3925
Learner-4388

@edx-status-bot
Copy link

Your PR has finished running tests.


import endpoints from './endpoints';

export function requestEntitlements(username_or_email) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the param?

import endpoints from './endpoints';

export function requestEntitlements(username_or_email) {
console.log('called requestEntitlements api function');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this console statement necessary?

Copy link
Contributor Author

@Mjloturco Mjloturco Mar 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha, no . hadn't pushed that change

);
}

export function createEntitlement(course_uuid, user, mode, reason, comments) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been finding that using objects as method parameters makes things, in some cases, easier to reason / read about when that method is called.

Here's an article that provides some examples.

// might be more readable
createEntitlement({ courseUUID: '098sfgoj5', user: 'baejadley', mode: 'baemode', reason: 'tobebae', comments: 'jaebaebae' });
// than
createEntitlement('098sfgoj5', 'baejadley', 'baemode', 'tobebae', 'jaebaebae' );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it

@@ -0,0 +1,3 @@
module.exports = {
Copy link
Contributor

@jaebradley jaebradley Mar 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason not to use a named export?

export {
  entitlementList: 'whatever',
  inTheFutureSomeOtherEndpoint: 'moar whatever',
};

it's going to cause an eslint error (maybe) if there's only one exported property, but I think it's fine to disable for now.

Copy link
Contributor

@jaebradley jaebradley Mar 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@jaebradley jaebradley Mar 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed with @cpennington that we support ES6 and CommonJS import and exporting in edx-platform - I think we should use ES6 modules where we can.

@edx-status-bot
Copy link

Your PR has finished running tests.

import 'whatwg-fetch'; // fetch polyfill
import Cookies from 'js-cookie';

import endpoints from './endpoints';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There might be added benefit importing it this way so that you're forced to specify endpoints.entitlementList later on (and make it abundantly clear that it's some endpoint value) but this file is not so big that importing the value like

import { entitlementList } from './endpoints';

would be hard to decipher.


import endpoints from './endpoints';

export function requestEntitlements({usernameOrEmail}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...maybe userIdentifier instead of usernameOrEmail?

alt-text

#namingIsHard

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to leave it as usernameOrEmail, since thats explicit, userIdentifier makes me think of a uuid.

}

export function updateEntitlement({email, reason, entitlementUuid, comments}) {
//Only requires an 'email' parameter to construct the url, not actually sent in the body
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Superfluous comment?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relevant, my team has discussed modifying the api to not be built in the url pattern.

`${endpoints.entitlementList}/${email}`, {
credentials: 'same-origin',
method: 'put',
headers:{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

POST and PUT headers seem to be identical - can we DRY here?

@@ -0,0 +1,54 @@
import 'whatwg-fetch'; // fetch polyfill
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably copy/pasted this comment from somewhere, but it seems somewhat unnecessary?

headers: headers,
body:JSON.stringify({
entitlement_uuid: entitlementUuid,
reason: reason,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hate myself for pointing this out but...indentation

credentials: 'same-origin',
method: 'put',
headers: headers,
body:JSON.stringify({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I hate myself for pointing this out but there should be a space before JSON.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lint me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alt-text

@@ -0,0 +1,4 @@
export {
entitlementList: 'whatever',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably don't want this to actually be whatever?

@edx-status-bot
Copy link

Your PR has finished running tests.

@Mjloturco Mjloturco changed the base branch from MLoTurco/learner-3925-feature to master March 5, 2018 16:17
@Mjloturco Mjloturco force-pushed the MLoTurco/learner-3925-feature-client-api branch from 852e5ce to 4712bad Compare March 5, 2018 16:27
Copy link
Contributor

@staubina staubina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good. Some minor code style comments and should be ready to :shipit:


import { entitlementList } from './endpoints';

const headers = {
Copy link
Contributor

@staubina staubina Mar 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Style Note: When defining a constant use all caps and underscores for the name (e.g. NAME_OF_CONST). It will clearly identify what in the code is constant and what is variable.

@@ -0,0 +1,3 @@
export {
entitlementList: '/support/entitlement_list',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a constant at its root as well. You might want to make this all caps as well to match the code style.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am researching this within out code style guides. This is the standard I have worked with in the past, but I am not sure where this fits in the edX style guide.

I have always found it to be more clear and helpful in the past.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was modeling this off of studio front end endpoints. I'm not sure SCREAMING_SNAKE_CASE is out standard for constants in front end. @edx/fedx-team do we have a decision/opinion on that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with what ever the style guide says :D. I have seen it different depending on the company.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think studio-frontend might have a mistake there. The JS convention is to use the SCREAMING_SNAKE_CASE 🐍 for constants.

Copy link
Contributor

@jaebradley jaebradley Mar 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got a couple of other opinions. You can disregard these recommendations. This is more a thing in Python and other languages.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, guess I'm wrong. I thought our style guide was different. Redux uses SCREAMING_SNAKE_CASE for action name constants so I thought it was the same as that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, leaving it as is.

}

export function updateEntitlement({email, reason, entitlementUuid, comments}) {
//Email param may be removable when EntitlementSupportListView
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a ticket id to this that we are planning to do this work in? It looks like this should be tracked as a Task or story and a TODO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't decided yet if its something we want to do/adds any value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I make a ticket we can talk about in grooming?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets talk about it before grooming. If we believe it is work we should do it is better to have it in a ticket than lost in the code simply in a comment.

'X-CSRFToken': Cookies.get('csrftoken'),
}

export function requestEntitlements({usernameOrEmail}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be clearer for the usernameOrEmail / user / email parameters in the three different functions to all have the one name, since they represent the same piece of information in each of the three requests.

);
}

export function createEntitlement({courseUuid, user, mode, reason, comments}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: for consistency it might be nice to have the ordering of parameters in the PUT and POST methods be fairly the same, i.e {user, reason, courseUuid, mode, comments} here to mirror below or {entitlementUuid, email, reason, comments} below to mirror what is here.

@edx-status-bot
Copy link

Your PR has finished running tests.


export function requestEntitlements({username}) {
return fetch(
`${entitlementApi}/?user=${usernameOrEmail}`, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

username?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ye


export function requestEntitlements({username}) {
return fetch(
`${entitlementApi}/?user=${usernameOrEmail}`, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently fetch doesn't come with built-in query parameter support

alt-text

It's probably overkill, but you could consider using qs or querystring

'X-CSRFToken': Cookies.get('csrftoken'),
}

export function requestEntitlements({username}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taking a step back...if this is in the entitlements/data/api/client.js file, is the entitlements part of all of these method names redundant / extraneous?

import { requestEntitlements, createEntitlement } from './entitlements/data/api/client';

import { get, create } from './entitlements/data/api/client';

import { 
  get as requestEntitlements, 
  create as createEntitlement,
} from './entitlements/data/api/client';

I get (and appreciate) the added verbosity but I wonder if it's relatively unnecessary?

Copy link
Contributor Author

@Mjloturco Mjloturco Mar 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed in person, this comment is for history. I've decided to leave this as is for now, In the future additional endpoints may be added to this client, though currently it only interacts with the entitlement api

@Mjloturco Mjloturco force-pushed the MLoTurco/learner-3925-feature-client-api branch from cdec30f to cc4e858 Compare March 7, 2018 15:31
@Mjloturco Mjloturco force-pushed the MLoTurco/learner-3925-feature-client-api branch from 44ecaa4 to aef9fb2 Compare March 7, 2018 15:55
@Mjloturco
Copy link
Contributor Author

jenkins run all

@Mjloturco Mjloturco force-pushed the MLoTurco/learner-3925-feature-client-api branch from aef9fb2 to 946caec Compare March 7, 2018 16:13
'X-CSRFToken': Cookies.get('csrftoken'),
}

export function requestEntitlements({username}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Yeah, gonna be that person again - these exports should be grouped at the end of the file.

Check out the eslint-plugin-import#groups-exports rule.

The airbnb/javascript style-guide doesn't explicitly mention this but in airbnb/javascript#1275 one of the maintainer of the style-guide who is also one of the maintainers of eslint-plugin-import said

As far as positioning, there will soon be a linter rule in eslint-plugin-import that will require that all export statements are grouped together - so I'd recommend grouping them together and putting them at the bottom of the file.

@edx-status-bot
Copy link

Your PR has finished running tests.

@Mjloturco Mjloturco force-pushed the MLoTurco/learner-3925-feature-client-api branch from 946caec to 35a305c Compare March 7, 2018 19:19
@edx-status-bot
Copy link

Your PR has finished running tests.

);
}

const updateEntitlement = ({entitlementUuid, unenrolled_run, reason, comments}) => {
Copy link
Contributor

@jaebradley jaebradley Mar 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase

alt-text

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

);
}

const updateEntitlement = ({entitlementUuid, unenrolledRun, reason, comments}) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reason is unused?

);
}

const createEntitlement = ({username, courseUuid, mode, reason, comments}) => {
Copy link
Contributor

@jaebradley jaebradley Mar 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reason is also unused here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they should both be action and should be included in the support_details object

@edx-status-bot
Copy link

Your PR has finished running tests.

package.json Outdated
@@ -53,6 +54,7 @@
"webpack": "2.7.0",
"webpack-bundle-tracker": "0.2.1",
"webpack-merge": "4.1.1",
"whatwg-fetch": "^2.0.3",
Copy link
Contributor

@jaebradley jaebradley Mar 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the pattern is to pin these to an exact version (note the lack of ^).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I just ran a command the linter told me to run. Should I just cut the ^?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@edx-status-bot
Copy link

Your PR has finished running tests.

Copy link
Contributor

@jaebradley jaebradley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should theoretically just be fixing styling at this point (maybe tests?) so 👍

alt-text

@@ -0,0 +1,3 @@
export {
entitlementApi: '/api/entitlements/v1/entitlements',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is causing you linting errors because if only one thing gets exported it wants it to be the default export consider moving the constant to client.js unless additional endpoint values are expected to be added in the near future (which, if I'm remembering correctly, is not the case?).

@Mjloturco Mjloturco force-pushed the MLoTurco/learner-3925-feature-client-api branch from a463286 to a117f6f Compare March 8, 2018 17:46
@edx-status-bot
Copy link

Your PR has finished running tests.

@Mjloturco Mjloturco merged commit 0d07a89 into master Mar 8, 2018
@edx-pipeline-bot
Copy link
Contributor

EdX Release Notice: This PR has been deployed to the staging environment in preparation for a release to production on Friday, March 09, 2018.

@edx-pipeline-bot
Copy link
Contributor

EdX Release Notice: This PR has been deployed to the production environment.

@jdmulloy jdmulloy deleted the MLoTurco/learner-3925-feature-client-api branch April 5, 2018 18:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants