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

feat: custom headers #76

Merged
merged 2 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,19 @@ In case of Google Cloud Identity-Aware Proxy, please specify these env variables
- `IAP_AUTH_CLIENT_ID` - # pick [client ID of the application](https://console.cloud.google.com/apis/credentials) you are connecting to
- `IAP_AUTH_SERVICE_ACCOUNT_JSON` - # generate in [Actions](https://console.cloud.google.com/iam-admin/serviceaccounts) -> Create key -> JSON

#### Custom headers

Using `headers` configuration:

```json
{
"index_name": "my_index",
"headers": {
"Authorization": "Bearer <my_token>"
}
},
```

### Installing Chrome Headless

Websites that need JavaScript for rendering are passed through ChromeDriver.
Expand Down
1 change: 1 addition & 0 deletions scraper/src/config/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ConfigLoader:
index_name_tmp = None
js_wait = 0
js_render = False
headers = None
keep_tags = []
min_indexed_level = 0
remove_get_params = False
Expand Down
3 changes: 3 additions & 0 deletions scraper/src/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ def run_config(config):
"Accept-Language": "en",
} # Defaults for scrapy https://docs.scrapy.org/en/latest/topics/settings.html#default-request-headers

if config.headers is not None:
headers.update(config.headers)

# Cloudflare Zero Trust (CF)
if (os.getenv("CF_ACCESS_CLIENT_ID") and
os.getenv("CF_ACCESS_CLIENT_SECRET")):
Expand Down
38 changes: 38 additions & 0 deletions scraper/src/tests/config_loader/headers_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# coding: utf-8
from ...config.config_loader import ConfigLoader
from .abstract import config
import pytest


class TestInit:
def test_header(self):
""" Should have a header """
# Given
c = config({
'headers': {
'Authorization': 'Bearer xyz'
}
})

config_loaded = ConfigLoader(c)

assert config_loaded.headers == {
'Authorization': 'Bearer xyz'
}

def test_multiple_header(self):
""" Should have headers """
# Given
c = config({
'headers': {
'Authorization': 'Bearer xyz',
'Custom': 1,
}
})

config_loaded = ConfigLoader(c)

assert config_loaded.headers == {
'Authorization': 'Bearer xyz',
'Custom': 1,
}