lazery's avatar

Laravel as a server to server platform

I try to organize my code here but I don't know what conventions are in cases like this.

I have a customer which sends authentication information to my application. My application then collects data from customers' api, process and saves it temporarily and as log as repsonse data contains new url addresses, my application needs to get response from those endpoints too. When there are no unused urls left, my application constructs a final report and sends it to the recipients api.

I have a function which takes an array of urls as a parameter, feeds it to the laravels http client making several requests at once using pooling, then foreach loop iterates the reponses through, validates them and distributes them to the data type specific handler classes. The handler clases process data forward and saves processed data temporarely to multidimensional array and returns url adresses if found any. Then all of the url addresses are merged together. Then the function calls itself recursively again with that new set of urls. This loop continues as long as there are any unused urls left. And when the urls array is empty, the report will be finally constructed and sent to the recipients api.

Are there some Laravel specific rules for this kind of task? With Laravel, I'm a beginner. I managed to use Laravel here with Request in Controller, Http Client, Log, Validator and that'sPHP about it. My classes and functions are basic PHP without features which Laravel potentially could have made my life easier.

So do you guys have experience of these kinds of tasks or can you share your thoughts about whether I haven't handled them soundly enough?

Thanks.

0 likes
1 reply
LaryAI's avatar
Level 58

Based on the description provided, it seems like you are looking for suggestions on how to handle a server-to-server communication flow in Laravel. Here are a few recommendations:

  1. Use Laravel's HTTP Client: You mentioned that you are already using Laravel's HTTP Client to make requests to the customer's API. This is a good approach as it provides a simple and convenient way to send HTTP requests. Make sure you are utilizing the pooling feature to make multiple requests concurrently.

  2. Organize your code: Since you are dealing with multiple steps and handling different types of data, it's important to organize your code properly. Consider creating separate classes or services to handle each step of the process. This will make your code more maintainable and easier to understand.

  3. Utilize Laravel's features: Laravel provides various features that can make your life easier. For example, you can use Laravel's logging system (Log) to log any important information or errors during the process. You can also use Laravel's validation (Validator) to validate the responses received from the customer's API.

  4. Consider using queues: If the process of collecting data, processing it, and sending the final report takes a significant amount of time, you might want to consider using Laravel's queue system. This will allow you to offload the processing to a background job, improving the performance and responsiveness of your application.

Here's a basic example of how you can structure your code:

// Controller
class DataController extends Controller
{
    public function process(Request $request)
    {
        // Get authentication information from the customer
        $authInfo = $request->input('authInfo');

        // Collect data from the customer's API
        $data = $this->collectData($authInfo);

        // Process the data
        $processedData = $this->processData($data);

        // Save processed data temporarily
        $this->saveProcessedData($processedData);

        // Get new URLs from the processed data
        $newUrls = $this->getNewUrls($processedData);

        // If there are new URLs, call the process method recursively
        if (!empty($newUrls)) {
            $this->process($newUrls);
        } else {
            // Construct the final report
            $report = $this->constructReport();

            // Send the report to the recipient's API
            $this->sendReport($report);
        }
    }

    private function collectData($authInfo)
    {
        // Make requests to the customer's API using Laravel's HTTP Client
        // ...

        return $data;
    }

    private function processData($data)
    {
        // Process the data and return the processed data
        // ...

        return $processedData;
    }

    private function saveProcessedData($processedData)
    {
        // Save the processed data temporarily
        // ...
    }

    private function getNewUrls($processedData)
    {
        // Extract new URLs from the processed data
        // ...

        return $newUrls;
    }

    private function constructReport()
    {
        // Construct the final report
        // ...

        return $report;
    }

    private function sendReport($report)
    {
        // Send the report to the recipient's API using Laravel's HTTP Client
        // ...
    }
}

This is just a basic example to give you an idea of how you can structure your code. You can further enhance it based on your specific requirements and business logic.

Remember to adjust the code according to your needs and make use of Laravel's features to simplify your development process.

Please or to participate in this conversation.