<commit_before>/*
 * Copyright(c) Sophist Solutions, Inc. 1990-2017.  All rights reserved
 */
#ifndef _Stroika_Framework_WebService_Basic_h_
#define _Stroika_Framework_WebService_Basic_h_ 1

#include "../StroikaPreComp.h"

#include "../../Foundation/Containers/Sequence.h"
#include "../../Foundation/Containers/Set.h"
#include "../../Foundation/DataExchange/InternetMediaType.h"
#include "../../Foundation/DataExchange/VariantValue.h"

#include "../WebServer/Request.h"
#include "../WebServer/RequestHandler.h"
#include "../WebServer/Response.h"

#include "../../Foundation/DataExchange/ObjectVariantMapper.h"

/*
 */

/*
 * TODO:
 *
 *  \version    <a href="code_status.html#Alpha">Alpha</a>
 *
 */

namespace Stroika {
    namespace Frameworks {
        namespace WebService {

            using namespace Stroika::Foundation;

            using Characters::String;
            using Containers::Sequence;
            using Containers::Set;
            using DataExchange::InternetMediaType;
            using DataExchange::VariantValue;
            using Memory::Optional;

            using WebServer::Request;
            using WebServer::Response;

            ////// SUPER DUPER ROUGH DRAFT

            VariantValue GetWebServiceArgsAsVariantValue (Request* request, const Optional<String>& fromInMessage);

            struct WebServiceMethodDescription {
                String            fOperation;
                Set<String>       fAllowedMethods; // e.g. GET
                InternetMediaType fResponseType;   // typically DataExchange::PredefinedInternetMediaType::JSON_CT ()

                Optional<String>           fOneLineDocs;
                Optional<Sequence<String>> fCurlExample;
                Optional<Sequence<String>> fDetailedDocs;
            };

            void ExpectedMethod (const Request* request, const Set<String>& methods, const Optional<String>& fromInMessage);
            void ExpectedMethod (const Request* request, const WebServiceMethodDescription& wsMethodDescription);

            void WriteResponse (Response* response, const WebServiceMethodDescription& webServiceDescription, const VariantValue& responseValue);

            void WriteDocsPage (Response* response, const Sequence<WebServiceMethodDescription>& operations, const String& h1Text = L"Operations");

            namespace cvt2Obj {
                // @todo eventually find a way to make this owrk with JSON or XML in/ out and in can be GET query args (depending on WebServiceMethodDescription properties)
                template <typename IN_ARGS, typename OUT_ARGS>
                WebServer::RequestHandler mkRequestHandler (const WebServiceMethodDescription& webServiceDescription, const DataExchange::ObjectVariantMapper& objVarMapper, const function<OUT_ARGS (IN_ARGS)>& f)
                {
                    return [=](WebServer::Message* m) {
                        WriteResponse (m->PeekResponse (), webServiceDescription, objVarMapper.FromObject (f (objVarMapper.ToObject<IN_ARGS> (GetWebServiceArgsAsVariantValue (m->PeekRequest (), {})))));
                    };
                }
            }
        }
    }
}

/*
 ********************************************************************************
 ***************************** Implementation Details ***************************
 ********************************************************************************
 */
#include "Basic.inl"

#endif /*_Stroika_Framework_WebService_Basic_h_*/<commit_msg>minor tweaks with Frameworks/WebService/Basic<commit_after>/*
 * Copyright(c) Sophist Solutions, Inc. 1990-2017.  All rights reserved
 */
#ifndef _Stroika_Framework_WebService_Basic_h_
#define _Stroika_Framework_WebService_Basic_h_ 1

#include "../StroikaPreComp.h"

#include "../../Foundation/Containers/Sequence.h"
#include "../../Foundation/Containers/Set.h"
#include "../../Foundation/DataExchange/InternetMediaType.h"
#include "../../Foundation/DataExchange/VariantValue.h"

#include "../WebServer/Request.h"
#include "../WebServer/RequestHandler.h"
#include "../WebServer/Response.h"

#include "../../Foundation/DataExchange/ObjectVariantMapper.h"

/*
 */

/*
 * TODO:
 *
 *  \version    <a href="code_status.html#Alpha">Alpha</a>
 *
 */

namespace Stroika {
    namespace Frameworks {
        namespace WebService {

            using namespace Stroika::Foundation;

            using Characters::String;
            using Containers::Sequence;
            using Containers::Set;
            using DataExchange::InternetMediaType;
            using DataExchange::VariantValue;
            using Memory::Optional;

            using WebServer::Request;
            using WebServer::Response;

            ////// SUPER DUPER ROUGH DRAFT

            VariantValue GetWebServiceArgsAsVariantValue (Request* request, const Optional<String>& fromInMessage);

            struct WebServiceMethodDescription {
                String            fOperation;
                Set<String>       fAllowedMethods; // e.g. GET
                InternetMediaType fResponseType;   // typically DataExchange::PredefinedInternetMediaType::JSON_CT ()

                Optional<String>           fOneLineDocs;
                Optional<Sequence<String>> fCurlExample;
                Optional<Sequence<String>> fDetailedDocs;
            };

            void ExpectedMethod (const Request* request, const Set<String>& methods, const Optional<String>& fromInMessage);
            void ExpectedMethod (const Request* request, const WebServiceMethodDescription& wsMethodDescription);

            void WriteResponse (Response* response, const WebServiceMethodDescription& webServiceDescription, const VariantValue& responseValue);

            void WriteDocsPage (Response* response, const Sequence<WebServiceMethodDescription>& operations, const String& h1Text = L"Operations");

            namespace cvt2Obj {
                // @todo eventually find a way to make this owrk with JSON or XML in/ out and in can be GET query args (depending on WebServiceMethodDescription properties)
                template <typename OUT_ARGS, typename IN_ARGS>
                WebServer::RequestHandler mkRequestHandler (const WebServiceMethodDescription& webServiceDescription, const DataExchange::ObjectVariantMapper& objVarMapper, const function<OUT_ARGS (IN_ARGS)>& f)
                {
                    return [=](WebServer::Message* m) {
                        WriteResponse (m->PeekResponse (), webServiceDescription, objVarMapper.FromObject (f (objVarMapper.ToObject<IN_ARGS> (GetWebServiceArgsAsVariantValue (m->PeekRequest (), {})))));
                    };
                }
                template <typename OUT_ARGS>
                WebServer::RequestHandler mkRequestHandler (const WebServiceMethodDescription& webServiceDescription, const DataExchange::ObjectVariantMapper& objVarMapper, const function<OUT_ARGS (void)>& f)
                {
                    return [=](WebServer::Message* m) {
                        WriteResponse (m->PeekResponse (), webServiceDescription, objVarMapper.FromObject (f ()));
                    };
                }
            }
        }
    }
}

/*
 ********************************************************************************
 ***************************** Implementation Details ***************************
 ********************************************************************************
 */
#include "Basic.inl"

#endif /*_Stroika_Framework_WebService_Basic_h_*/
{'fraction_non_alphanumeric': 0.15205772314270444, 'fraction_numerical': 0.002939604489577766, 'mean_word_length': 2.856259659969088, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7584968', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1756, 'n_words': 409}