Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bc16f6f
default object attributes
Jay-ponda-improwised Feb 15, 2025
80c0ccc
Merge branch 'release/0.0.1'
Jay-ponda-improwised Feb 15, 2025
780b4ab
Merge tag '0.0.1' into develop
Jay-ponda-improwised Feb 15, 2025
efcd1dc
rename project
Jay-ponda-improwised Feb 15, 2025
2cc0f94
added error attribute
Jay-ponda-improwised Feb 15, 2025
f708fda
Revert "rename project"
Jay-ponda-improwised Feb 15, 2025
596abb3
Merge branch 'release/0.0.2'
Jay-ponda-improwised Feb 15, 2025
6a82eb8
Merge tag '0.0.2' into develop
Jay-ponda-improwised Feb 15, 2025
51097bf
mock xml api call
Jay-ponda-improwised Feb 16, 2025
d3f75fb
Merge branch 'release/0.0.3'
Jay-ponda-improwised Feb 16, 2025
a9e1122
Merge tag '0.0.3' into develop
Jay-ponda-improwised Feb 16, 2025
53ab025
fix whitespace probleam and typos and put log
Jay-ponda-improwised Feb 17, 2025
99275ea
Merge branch 'release/0.0.4'
Jay-ponda-improwised Feb 17, 2025
9847ad8
Merge tag '0.0.4' into develop
Jay-ponda-improwised Feb 17, 2025
aa45421
fix whitespace probleam
Jay-ponda-improwised Feb 17, 2025
35a4215
Merge branch 'release/0.0.5'
Jay-ponda-improwised Feb 17, 2025
23b9892
Merge tag '0.0.5' into develop
Jay-ponda-improwised Feb 17, 2025
6e78a3f
remove deprication warning
Jay-ponda-improwised Sep 8, 2025
793a5af
Merge branch 'Local-Data-Exchange:master' into develop
Jay-ponda-improwised Sep 8, 2025
67a4d69
Merge pull request #2 from Jay-ponda-improwised/feature/remove-depric…
Jay-ponda-improwised Sep 8, 2025
42b1ce7
fix docker file
Jay-ponda-improwised Sep 8, 2025
4844dd8
fix whitespace issue
Jay-ponda-improwised Sep 8, 2025
fc15a23
remove unwanted items from dockerignore
Jay-ponda-improwised Sep 8, 2025
d82fa59
Merge branch 'feature/added-testcase-and-coverage' into develop
Jay-ponda-improwised Sep 9, 2025
08c3c58
Merge branch 'Local-Data-Exchange:master' into master
Jay-ponda-improwised Sep 9, 2025
1d13562
rename files to have develop prefix
Jay-ponda-improwised Sep 9, 2025
a515ac5
added direct alias for test and coverage, so without going to image; …
Jay-ponda-improwised Sep 9, 2025
2f5a98a
Merge pull request #5 from Jay-ponda-improwised/feature/constistant-d…
Jay-ponda-improwised Sep 9, 2025
ce93f2a
Merge branch 'release/0.0.6'
Jay-ponda-improwised Sep 9, 2025
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
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
coverage
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.phpunit.result.cache

vendor/
vendor/
coverage
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@




<!-- PROJECT LOGO -->
<br />
<p align="center">
Expand Down Expand Up @@ -34,12 +30,10 @@
* [Installation](#installation)
* [Configuration](#configuration)
* [Usage](#usage)
* [Methods](#methods)
* [Methods](#methods)
* [Response](#response)




## Getting Started

This package is useful to consume API's, here is the instruction for installation and usage.
Expand All @@ -53,7 +47,7 @@ This package is useful to consume API's, here is the instruction for installatio
composer require lde/api-helper

3. This command will install package with dependency

## Configuration

- To use this apihelper need to export config file to do so run the following command in your terminal to publish config file to config folder.
Expand Down
6 changes: 4 additions & 2 deletions dev-docker-compose.yaml → develop-docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
services:
api-helper:
image: circleci/php:7.4
build:
context: .
dockerfile: develop.Dockerfile
container_name: api-helper
working_dir: /app
command: sleep infinity
volumes:
- .:/app
- /app/vendor
62 changes: 62 additions & 0 deletions develop.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
FROM circleci/php:7.4-cli

# Install system dependencies
USER root
RUN apt-get update && apt-get install -y \
git \
curl \
libxml2-dev \
zip \
unzip

# Install PHP extensions including PCOV for code coverage
RUN pecl install pcov && \
docker-php-ext-enable pcov && \
docker-php-ext-install xml

# Install Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Set working directory
WORKDIR /app

# Copy all files (excluding vendor directory due to .dockerignore)
COPY . .

# Install dependencies (update if lock file is out of sync)
RUN composer update --no-scripts --prefer-dist --no-autoloader

# Generate autoload files
RUN composer dump-autoload --optimize

# Create a script to run composer install when container starts
RUN echo '#!/bin/bash\n\
echo "Running startup script..."\n\
git config --global --add safe.directory /app\n\
composer install\n\
echo "Starting container..."\n\
sleep infinity' > /start.sh && chmod +x /start.sh

# Create a wrapper script for running tests with coverage
RUN echo '#!/bin/bash\n\
# Disable Xdebug by default for better performance\n\
export PHP_IDE_CONFIG=\n\
\n\
# Enable Xdebug only when needed for coverage\n\
if [ "$1" = "coverage" ]; then\n\
echo "Enabling Xdebug for coverage report..."\n\
php -d xdebug.mode=coverage ./vendor/bin/phpunit --configuration develop.phpunit.xml --coverage-html=coverage\n\
else\n\
# Run tests without Xdebug for better performance\n\
echo "Running tests without Xdebug for better performance..."\n\
php -n -d extension=/usr/local/lib/php/extensions/no-debug-non-zts-20190902/pcov.so ./vendor/bin/phpunit --configuration develop.phpunit.xml\n\
fi' > /usr/local/bin/run-tests.sh && chmod +x /usr/local/bin/run-tests.sh

# Create executable scripts for easier test command execution
RUN echo '#!/bin/bash\n\
/usr/local/bin/run-tests.sh "$@"' > /usr/local/bin/test && chmod +x /usr/local/bin/test && \
echo '#!/bin/bash\n\
/usr/local/bin/run-tests.sh coverage "$@"' > /usr/local/bin/test-coverage && chmod +x /usr/local/bin/test-coverage

# Keep container running
CMD ["/start.sh"]
25 changes: 25 additions & 0 deletions develop.phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Api Helper Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<directory suffix=".php">src/Config</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
205 changes: 205 additions & 0 deletions developer.README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
<!-- PROJECT LOGO -->
<br />
<p align="center">
<a href="https://www.localdataexchange.com">
<img src="https://staging-ipromote.ldex.co/ctm/LDE_Logo-Black.png" alt="Logo" width="" height="80">
</a>

<h3 align="center">Api Helper Package - Developer Guide</h3>

<p align="center">
A package to consume api smoothly
<br />
<a href="#table-of-contents"><strong>Explore the docs »</strong></a>
<br />
<br />
<a href="https://packagist.org/packages/lde/api-helper">View Package</a>
·
<a href="https://github.com/Local-Data-Exchange/api-helper/issues">Report Bug</a>
·
<a href="https://github.com/Local-Data-Exchange/api-helper/issues">Request Feature</a>
</p>
</p>



<!-- TABLE OF CONTENTS -->
## Table of Contents

* [Getting Started](#getting-started)
* [Installation](#installation)
* [Configuration](#configuration)
* [Usage](#usage)
* [Methods](#methods)
* [Response](#response)
* [Docker Development Setup](#docker-development-setup)
* [Prerequisites](#prerequisites)
* [Getting Started](#getting-started-1)
* [Volume Mounting](#volume-mounting)
* [Running Tests](#running-tests)
* [Running Tests with Coverage](#running-tests-with-coverage)
* [Development Workflow](#development-workflow)


## Getting Started

This package is useful to consume API's, here is the instruction for installation and usage.

## Installation

1. To install this package using [Packagist](https://packagist.org/packages/lde/api-helper)

2. On the root of your project run following command

composer require lde/api-helper

3. This command will install package with dependency

## Configuration

- To use this apihelper need to export config file to do so run the following command in your terminal to publish config file to config folder.

php artisan vendor:publish --provider="Lde\ApiHelper\ApiHelperServiceProvider"

- This will publish config file naming **api_helper.php** into config folder.

## Prometheus Configuration
- Prometheus is dependent on your app so you need to provide prometheus configuration and also use below packages on your app.

- [jimdo/prometheus_client_php](https://github.com/Jimdo/prometheus_client_php)
- [superbalist/laravel-prometheus-exporter](https://github.com/Superbalist/laravel-prometheus-exporter)
- If you want to use prometheus then you should turn it on from config [api_helper](src/Config/api_helper.php)
```php
'log_stats' => true, // If you want to use prometheus then set as true otherwise false

'prometheus' => [
'labels' => [
'client_id' => 10,
'app' => 'api-helper',
'source' => 'core',
],
'histogram_bucket' => [0.1, 0.25, 0.5, 0.75, 1.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0, 7.5, 10.0],
],
```
- You can configure labels of prometheus inside `prometheus.labels` as per your need.
- `histogram_bucket` you can set inside prometheus config as array.

## Usage

- To use this package you need to add following class where you want to use this package.

use Lde\ApiHelper\ApiBuilder;


### Methods

#### addHeaders($headers)

- This method is use to add headers.

- It accept name and value as parameter, Here you can set only one header at a time.


$headers['Accept'] = "application/json";
$headers['Content-Type'] = "application/json";
app(ApiBuilder::class)->addHeaders($headers);

- We will get response in form of object of ApiBuilder.


#### api($connection)

- This method is use to set api that we are going to use from *api_helper.php* , there is httpbin and mokbin is define so you have to pass the name that you want to use.

- You can also define your own api end point at *api_helper.php* in config file.

app(ApiBuilder::class)->api('httpbin')->method_to_call();

- The snippet indicates how you can connect particular api and access their method.

- method_to_call() is the function that you have specified inside *api_helper* connection array.

- This will return object of ApiResponse.

### Response

- Here you will get object in response, In each response you will get success either true or false
- You will also get status code for more information about response please check below doc.
- http://docs.guzzlephp.org/en/latest/psr7.html#responses

## Docker Development Setup

This package includes a Docker development environment for easier testing and development.

### Prerequisites

- Docker and Docker Compose installed on your system

### Getting Started

1. Build and start the Docker container:
```bash
docker-compose -f develop-docker-compose.yaml up -d
```

2. Enter the container to work with the code:
```bash
docker exec -it api-helper bash
```

3. Inside the container, the package will automatically install dependencies using Composer on first run.

### Volume Mounting

The current directory is mounted to `/app` in the container. The `vendor` directory is excluded from the volume mount to prevent conflicts between host and container dependencies.

### Running Tests

You can run the PHPUnit tests using our optimized wrapper script:

```bash
docker exec api-helper /usr/local/bin/run-tests.sh
```

The tests run without Xdebug by default for better performance. PCOV is used for code coverage collection when needed.
The tests use the `develop.phpunit.xml` configuration file which is optimized for the Docker development environment.

### Running Tests with Coverage

To run tests with code coverage reporting:

```bash
docker exec api-helper /usr/local/bin/run-tests.sh coverage
```

This will generate an HTML coverage report in the `coverage` directory, which you can view by opening `coverage/index.html` in a web browser.

Note: Coverage reports require Xdebug to be enabled, which will slow down test execution. For regular development, use the standard test command for better performance.

### Using Test Aliases

For convenience, the Docker container includes executable scripts for running tests:

```bash
# Run tests without coverage (faster)
docker-compose -f develop-docker-compose.yaml exec api-helper test

# Run tests with coverage report
docker-compose -f develop-docker-compose.yaml exec api-helper test-coverage
```

These commands can be run directly from your host machine without entering the container.

You can also use them inside the container:

```bash
docker exec -it api-helper bash
test # Run tests without coverage
test-coverage # Run tests with coverage
```

### Development Workflow

1. Make changes to the code on your host machine
2. The changes will be immediately reflected in the container
3. Run tests or other commands inside the container as needed
2 changes: 1 addition & 1 deletion src/ApiBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ protected function maskFieldValues(array &$data, array $paths)

$string = Arr::get($data, $field);

if (stripos($string, '@') !== false) {
if ($string !== null && stripos($string, '@') !== false) {
$obfuscatedString = ObfuscationHelper::obfuscate($string, 4);
} else {
$obfuscatedString = ObfuscationHelper::obfuscate($string, 4);
Expand Down
10 changes: 10 additions & 0 deletions src/Helpers/ObfuscationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ class ObfuscationHelper

public static function obfuscate($string, $visibleChars = 4)
{
// Handle null input
if ($string === null) {
return '';
}

$len = strlen($string);
$visible = substr($string, ($visibleChars * -1));
return str_pad($visible, $len, '*', STR_PAD_LEFT);
}

public static function obfuscateEmail($email)
{
// Handle null input
if ($email === null) {
return '';
}

$em = explode("@", $email);
$name = implode('@', array_slice($em, 0, (count($em) - 1)));
$len = (strlen($name) - 1);
Expand Down