Function Testruntypes_API(@API)
/***********************************************************************************************************************

    This program is proprietary and is not to be used by or disclosed to others, nor is it to be copied without written
    permission from SRP Computer Solutions, Inc.

    Name        :   Testruntypes_API

    Description :   API logic for the Testruntypes resource.

    Notes       :   All web APIs should include the API_SETUP insert. This will provide several useful variables:

                        HTTPMethod              - The HTTP Method (Verb) submitted by the client (e.g., GET, POST, etc.)
                        APIURL                  - The URL for the API entry point (e.g., api.mysite.com/v1).
                        FullEndpointURL         - The URL submitted by the client, including query params.
                        FullEndpointURLNoQuery  - The URL submitted by the client, excluding query params.
                        EndpointSegment         - The URL endpoint segment.
                        ParentURL               - The URL path preceeding the current endpoint.
                        CurrentAPI              - The name of this stored procedure.

    Parameters  :
        API             [in] -- Web API to process. Format is [APIPattern].[HTTPMethod]:
                                    - APIPattern must follow this structure Testruntypes[.ID.[<Property>]]
                                    - HTTPMethod can be any valid HTTP method, e.g., GET, POST, PUT, DELETE, etc.
                                Examples:
                                    - Testruntypes.POST
                                    - Testruntypes.ID.PUT
                                    - Testruntypes.ID.firstName.GET
        Response       [out] -- Response to be sent back to the Controller (HTTP_MCP) or requesting procedure. Web API
                                services do not rely upon anything being returned in the response. This is what the
                                various services like SetResponseBody and SetResponseStatus services are for. A response
                                value is only helpful if the developers want to use it for debug purposes.

    History     :   (Date, Initials, Notes)
        11/05/24    xxx     Original programmer.

***********************************************************************************************************************/

#pragma precomp SRP_PreCompiler

Declare function OI_Wizard_Services, Test_Run_Services

$insert APP_INSERTS
$insert API_SETUP
$insert HTTP_INSERTS

GoToAPI else
    // The specific resource endpoint doesn't have a API handler yet.
    HTTP_Services('SetResponseStatus', 204, 'This is a valid endpoint but a web API handler has not yet been created.')
end

Return Response OR ''


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Endpoint Handlers
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


API testruntypes.HEAD
API testruntypes.GET

    OIWizardID = ''
    Cookies    = HTTP_Services('GetHTTPCookie')
    For each Cookie in Cookies using ';'
        Key = Field(Cookie, '=', 1)
        If Key EQ 'sessionID' then
            OIWizardID = Field(Cookie, '=', 2)
        end
    Next Cookie
    If OIWizardID NE ''  then
        ValidSession = OI_Wizard_Services('ValidateSession', OIWizardID)
    end
    StatusCode = 201
    Message    = 'Success'
    GoSub CreateHALCollection


end api

CreateHALCollection:
    JSONCollection  = ''
    Abort           = False$
    TestRunTypeList = Test_Run_Services('GetAllTestRunTypes')
    hJSONCollection = ''
    If SRP_JSON(hJSONCollection, 'New', 'Object') then
    	hTestRunTypeArray = ''
        If SRP_JSON(hTestRunTypeArray, 'New', 'Array') then
            For each TestRunTypeId in TestRunTypeList<1> using @VM setting fPos
                If SRP_JSON(hTestRunTypeObj, 'New', 'Object') then
                    SRP_JSON(hTestRunTypeObj, 'SetValue', 'testRunTypeId', TestRunTypeId, 'Number')
                    SRP_JSON(hTestRunTypeObj, 'SetValue', 'testRunTypeName', TestRunTypeList<2, fPos>, 'String')
                    SRP_JSON(hTestRunTypeArray, 'Add', hTestRunTypeObj)
                    SRP_JSON(hTestRunTypeObj, 'Release')
                end else
                    Abort = True$
                end
            Until Abort
            Next TestRunTypeId
            If Abort EQ False$ then
                SRP_JSON(hJSONCollection, 'Set', 'TestRunTypes', hTestRunTypeArray)
            end
            SRP_JSON(hTestRunTypeArray, 'Release')
        end
        JSONCollection = SRP_JSON(hJSONCollection, 'Stringify', 'Styled')
        SRP_JSON(hJSONCollection, 'Release')
    end
    If Error_Services('NoError') then
        HTTP_Services('SetResponseHeaderField', 'Content-Location', FullEndpointURL)
        HTTP_Services('SetResponseBody', JSONCollection, False$, 'application/hal+json')
        If Assigned(Message) then
            HTTP_Services('SetResponseStatus', StatusCode, Message)
        end else
            HTTP_Services('SetResponseStatus', StatusCode)
        end
    end else
        Message = Error_Services('GetMessage')          
        HTTP_Services('SetResponseStatus', 500, 'Error in the ' : CurrentAPI : ' API. Message: ': Message)
    end
return
    
