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

Adds user agent header for requests to Green Web Foundation APIs #184

Merged
merged 19 commits into from
Jan 28, 2024

Conversation

sfishel18
Copy link
Contributor

the version from package.json is used to inject an environment variable when esbuild runs, which the running code can read to create the correct user agent string when making requests.

fixes #181

the version from package.json is used to inject an environment variable
when esbuild runs, which the running code can read to create the correct
user agent string when making requests.

fixes thegreenwebfoundation#181
@sfishel18
Copy link
Contributor Author

@fershad let me know if this looks like the right direction. there were a couple of things i wasn't sure of:

  1. should the requests from the node.js version of the library also include the user agent header? i went ahead and added it there but can easily revert it

  2. afaict some of the unit tests in hosting.test.js are sending actual network requests. i didn't change that behavior since i'm not sure if it's intentional. but since i added logic to spy on https.get, i could easily modify it to mock out the responses if desired.

@fershad
Copy link
Contributor

fershad commented Jan 8, 2024

@sfishel18 thanks for this. I'll go through it in the next week or two with a view to merge it into the v0.14.1 release

@fershad fershad added this to the 0.14 milestone Jan 9, 2024
commit 0246400
Author: fershad <[email protected]>
Date:   Tue Jan 9 16:49:57 2024 +0800

    update changelog

commit fcd392c
Author: fershad <[email protected]>
Date:   Tue Jan 9 14:29:30 2024 +0800

    0.14.1

commit 6555fcf
Author: fershad <[email protected]>
Date:   Tue Jan 9 14:12:01 2024 +0800

    build with latest grid intensity figures

commit 27e2ed4
Merge: b22bf09 e53da02
Author: fershad <[email protected]>
Date:   Mon Jan 8 12:14:09 2024 +0800

    Use type definitions from DefinitelyTyped.

commit e53da02
Author: fershad <[email protected]>
Date:   Mon Jan 8 12:09:31 2024 +0800

    change title

commit bf834fe
Author: fershad <[email protected]>
Date:   Mon Jan 8 12:06:51 2024 +0800

    add links to npm and DT

commit b5d8828
Author: fershad <[email protected]>
Date:   Mon Jan 8 12:03:02 2024 +0800

    add guidance on installing type definitions

commit ff6f767
Author: fershad <[email protected]>
Date:   Mon Jan 8 12:02:46 2024 +0800

    remove index.d.ts file

commit b22bf09
Author: fershad <[email protected]>
Date:   Mon Jan 8 11:52:53 2024 +0800

    update test constants

commit dbd9d7d
Merge: 49da1c4 d7eca01
Author: fershad <[email protected]>
Date:   Mon Jan 8 11:47:56 2024 +0800

    Reduce NPM package size

commit 49da1c4
Merge: 51b85e2 84384da
Author: fershad <[email protected]>
Date:   Thu Jan 4 16:34:48 2024 +0800

    [AUTOMATED] Update average annual grid intensities

commit 84384da
Author: fershad <[email protected]>
Date:   Wed Jan 3 10:09:01 2024 +0000

    Update average annual grid intensities

commit d7eca01
Author: fershad <[email protected]>
Date:   Mon Jan 1 17:42:28 2024 +0800

    commit iife bundle

commit f4a6834
Author: fershad <[email protected]>
Date:   Mon Jan 1 17:09:36 2024 +0800

    add file to ignore when packaging for npm
@fershad
Copy link
Contributor

fershad commented Jan 23, 2024

@sfishel18 thanks for this one. I went through it and made some changes:

  • I added an optional param (comment) to the request headers. This allows a space for users to pass in the name of their app, site, or company which can give us a better insight into where requests are coming from.
  • Hitting the actual API is as designed in the tests. I have removed nock in some tests & replaced it with spying on the https.get request headers.

There's one hurdle for this update though. Right now, the package version gets written to the code when the build is run. However, when we publish to NPM we use the np package to smooth out this process.

np bumps the package version just before publishing. I believe this should be solvable by using the npm lifecycle hooks which np runs (https://github.com/sindresorhus/np?tab=readme-ov-file#npm-hooks). I've added a version script to rerun the build when the version number is bumped.

@fershad fershad requested a review from mrchrisadams January 23, 2024 12:35
@fershad
Copy link
Contributor

fershad commented Jan 23, 2024

@mrchrisadams this PR is ready for a review when you've got a moment.

The changes made add a User-Agent header to fetch requests made from CO2.js to the Greencheck API. This will show the current version of CO2.js by default. There's an optional parameter where the user can pass in a string to represent the app, site, or company making the request.

The output of the check function remains unchanged.

For example:

import {hosting} from '@tgwf/co2'

hosting.check("google.com", "MyCoolApp").then((result) => {
    console.log(result,);
  });

This would send a fetch request to the Greencheck API with the User-Agent: co2js/0.14.1 MyCoolApp header.

Note, that when used in the browser the User-Agent header will be set to whatever the browser sets since I believe it's read-only in that context.

Happy to walk you through the code if you need.

Copy link
Member

@mrchrisadams mrchrisadams left a comment

Choose a reason for hiding this comment

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

Hi @fershad thanks for tagging me - this looks good, and the comments I had are small quibbles, which while being nice to address, would not stop this being merged in.

Feel free to merge in, but do please take a mo to review the comments about whether we update related issues like the pagexray one, or adding the use of jest mocks instead of just spies.

src/hosting-api.js Outdated Show resolved Hide resolved
src/hosting-api.test.js Outdated Show resolved Hide resolved
src/hosting.js Outdated Show resolved Hide resolved
src/hosting.test.js Show resolved Hide resolved
@fershad
Copy link
Contributor

fershad commented Jan 24, 2024

Thanks @mrchrisadams I've made most of those changes, including reverting the API calls to mocks. Going to let this cool for a couple of days before merging & releasing a new build.

@sfishel18 welcome to cast your eye over this as well :)

@mrchrisadams
Copy link
Member

Thanks for making those changes @fershad particularly adding the networking mocks (I've been feeling a bit uneasy about nock, as it's a library I'm not very familiar with, and was a bit scared of changing it.)

One last thing (and this is likely a response to you saying let's let it cool a couple of days so we can view it in with fresh eyes later) is that we're passing in comment as our variable name, but really it's setting a User-Agent string.

It might make sense to just name the argument userAgent, userAgentIdentifier or something along those lines to make the purpose of this extra argument really explicit - this feels like it might be a bit clearer than comment as a variable name in a few months.

I reckon this probably is worth deciding before we merge in, but I'd leave the final decision with you.

WDYT?

@fershad
Copy link
Contributor

fershad commented Jan 25, 2024

It might make sense to just name the argument userAgent, userAgentIdentifier or something along those lines to make the purpose of this extra argument really explicit ...

Makes a ton of sense. I've renamed it to userAgentIdentifier.

@sfishel18
Copy link
Contributor Author

@sfishel18 welcome to cast your eye over this as well :)

thanks @fershad! sorry for the delayed reply, just got back from a vacation. all looks good to me. let me know if there's anything in particular still missing here that i could look into.

@fershad fershad merged commit f5afb65 into thegreenwebfoundation:main Jan 28, 2024
3 checks passed
@fershad
Copy link
Contributor

fershad commented Jan 28, 2024

@sfishel18 all good mate, added you to the contributors list. Thanks for helping with this change.

@sfishel18
Copy link
Contributor Author

@sfishel18 all good mate, added you to the contributors list. Thanks for helping with this change.

my pleasure! let me know if you have any other good starter tasks top-of-mind for someone new. no worries if not, i can look over the open issues and pick one that speaks to me :)

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.

Add source headers to hosting API calls
3 participants