Security News
vlt Debuts New JavaScript Package Manager and Serverless Registry at NodeConf EU
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
fetchin is an efficient library for handling HTTP requests in Python. It provides a custom logger to monitor HTTP traffic, a metrics model to track request performance, and a fetcher to simplify HTTP requests. This library is ideal for developers who want greater control and visibility over their HTTP requests and performance metrics.
The Fetcher
accepts the following parameters during instantiation:
fail_max
: Maximum number of failures before opening the Circuit Breaker.reset_timeout
: Time in seconds to wait before closing the Circuit Breaker after a failure.backoff_strategy
: Backoff strategy to determine the wait time between retries.Install via pip:
pip install fetchin
from fetchin import Fetcher, CustomLogger, PrometheusMetrics
logger = CustomLogger()
# Circuit Breaker configuration with linear backoff strategy
def linear_backoff(attempt: int):
return attempt * 2
circuit_config = {
"fail_max": 3,
"reset_timeout": 60,
"backoff_strategy": linear_backoff
}
# Create a fetcher using the logger and Prometheus as the default metrics system
fetcher = Fetcher(label="api-service", logger=logger, metrics=PrometheusMetrics(), circuit_config=circuit_config, max_retries=5)
try:
response = fetcher.get("http://localhost:8080/api/example")
print(response.json())
except Exception as e:
logger.error(f"Error during fetch: {e}")
You can also provide your own metrics implementation. Simply implement the MetricsInterface
.
from fetchin import Fetcher, CustomLogger, MetricsInterface
class CustomMetrics(MetricsInterface):
def track_request(self, method: str, status_code: int, response_time: float):
print(f"Custom Metrics -> Method: {method}, Status: {status_code}, Time: {response_time:.2f}s")
def track_retry(self, method: str):
print(f"Custom Retry -> Method: {method}")
logger = CustomLogger()
metrics = CustomMetrics()
# Create a fetcher with a custom metrics implementation
fetcher = Fetcher(label="api-service", logger=logger, metrics=metrics, max_retries=3)
try:
response = fetcher.get("http://localhost:8080/api/example")
print(response.json())
except Exception as e:
logger.error(f"Error during fetch: {e}")
With the new Fetcher
update, you can pass additional options like timeout
, headers
, or any other requests
configuration directly in the request call.
fetcher = Fetcher(label="api-service", logger=logger, metrics=PrometheusMetrics())
# Example using timeout and custom headers for a GET request
try:
response = fetcher.get(
"http://localhost:8080/api/example",
timeout=10,
headers={"Authorization": "Bearer token"}
)
print(response.json())
except Exception as e:
logger.error(f"Error during fetch: {e}")
You can use kwargs
to pass any valid argument supported by the requests
library.
To set up a Python virtual environment and install dependencies:
Create the virtual environment:
python3 -m venv venv
Activate the virtual environment:
source venv/bin/activate
.\venv\Scripts\activate
Install project dependencies:
pip install -r requirements.txt
To deactivate the virtual environment, run:
deactivate
You can use WireMock to run a mock API for testing. With Docker Compose, you can easily start and stop the mock API. Follow these steps:
Run the following command to start the mock API in the background:
make docker-up
The mock API will now be available at http://localhost:8080
. You can test an example endpoint with:
curl http://localhost:8080/api/example
This should return a mocked response like:
{
"message": "This is a mocked response from WireMock!"
}
To stop the mock API and stop the container, run:
make docker-down
To install the project dependencies, run:
make install
To check code style with flake8, run:
make lint
To format the code according to style conventions using black, run:
make format
To run project tests using pytest, run:
make test
If there are changes in the configuration files and you want to rebuild the WireMock container, run:
make docker-build
To see the logs of the WireMock container in real-time, run:
make docker-logs
To test the project execution and see usage examples in the playground, run:
make play
Contributions are welcome! Please open an issue or submit a pull request.
FAQs
Library for handling HTTP requests with circuit breaker, logs, and metrics.
We found that fetchin demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt introduced its new package manager and a serverless registry this week, innovating in a space where npm has stagnated.
Security News
Research
The Socket Research Team uncovered a malicious Python package typosquatting the popular 'fabric' SSH library, silently exfiltrating AWS credentials from unsuspecting developers.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.