All Downloads are FREE. Search and download functionalities are using the official Maven repository.

haskell-http-client.Model.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>partial_header}}
{-|
Module : {{baseModule}}.Model
-}

{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveFoldable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DeriveTraversable #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# OPTIONS_GHC -fno-warn-unused-matches -fno-warn-unused-binds -fno-warn-unused-imports #-}

module {{baseModule}}.Model {{#x-hasImportMappings}}(module {{baseModule}}.Model, module {{baseModule}}.ImportMappings) {{/x-hasImportMappings}}where

import {{baseModule}}.Core
import {{baseModule}}.MimeTypes{{#x-hasImportMappings}}
import {{baseModule}}.ImportMappings{{/x-hasImportMappings}}

import Data.Aeson ((.:),(.:!),(.:?),(.=))

import qualified Control.Arrow as P (left)
import qualified Data.Aeson as A
import qualified Data.ByteString as B
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString.Lazy as BL
import qualified Data.Data as P (Typeable, TypeRep, typeOf, typeRep)
import qualified Data.Foldable as P
import qualified Data.HashMap.Lazy as HM
import qualified Data.Map as Map
import qualified Data.Maybe as P
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Time as TI
import qualified Lens.Micro as L
import qualified Web.FormUrlEncoded as WH
import qualified Web.HttpApiData as WH

import Control.Applicative ((<|>))
import Control.Applicative (Alternative)
import Data.Function ((&))
import Data.Monoid ((<>))
import Data.Text (Text)
import Prelude (($),(/=),(.),(<$>),(<*>),(>>=),(=<<),Maybe(..),Bool(..),Char,Double,FilePath,Float,Int,Integer,String,fmap,undefined,mempty,maybe,pure,Monad,Applicative,Functor)

import qualified Prelude as P



-- * Parameter newtypes
{{#x-allUniqueParams}}{{#x-newtype}}

-- ** {{{x-param-name-type}}}
newtype {{{x-param-name-type}}} = {{{x-param-name-type}}} { un{{{x-param-name-type}}} :: {{{x-data-type}}} } deriving (P.Eq, P.Show{{#x-isBodyParam}}, A.ToJSON{{/x-isBodyParam}}){{/x-newtype}}{{/x-allUniqueParams}}

-- * Models

{{#models}}
{{#model}}{{^isEnum}}
-- ** {{classname}}
-- | {{classname}}{{#title}}
-- {{{.}}}
-- {{/title}}{{#description}}
-- {{{.}}}{{/description}}{{#isAlias}}
newtype {{classname}} = {{classname}}
  { un{{classname}} :: {{{vendorExtensions.x-data-type}}}
  } deriving (P.Eq, P.Show, P.Typeable, A.ToJSON, A.FromJSON{{#modelDeriving}}, {{modelDeriving}}{{/modelDeriving}}){{/isAlias}}{{^isAlias}}
data {{classname}} = {{classname}}
  { {{#vars}}{{name}} :: {{#x-strictFields}}!({{/x-strictFields}}{{^required}}Maybe {{/required}}{{{vendorExtensions.x-data-type}}}{{#x-strictFields}}){{/x-strictFields}} -- ^ {{#required}}/Required/ {{/required}}{{#isReadOnly}}/ReadOnly/ {{/isReadOnly}}"{{baseName}}"{{#description}} - {{description}}{{/description}}{{^-last}}
  , {{/-last}}{{/vars}}
  } deriving (P.Show, P.Eq, P.Typeable{{#modelDeriving}}, {{modelDeriving}}{{/modelDeriving}}){{/isAlias}}

{{^isAlias}}-- | FromJSON {{classname}}
instance A.FromJSON {{classname}} where
  parseJSON = A.withObject "{{classname}}" $ \o ->
    {{^hasVars}}pure {{/hasVars}}{{classname}}
      {{#hasVars}}<$>{{/hasVars}}{{#vars}} (o {{#required}}.: {{/required}}{{^required}}{{^allowFromJsonNulls}}.:!{{/allowFromJsonNulls}}{{#allowFromJsonNulls}}.:?{{/allowFromJsonNulls}}{{/required}} "{{baseName}}"){{^-last}}
      <*>{{/-last}}{{/vars}}

-- | ToJSON {{classname}}
instance A.ToJSON {{classname}} where
  toJSON {{classname}} {{#hasVars}}{..}{{/hasVars}} =
   {{^allowToJsonNulls}}_omitNulls{{/allowToJsonNulls}}{{#allowToJsonNulls}}A.object{{/allowToJsonNulls}}
      [ {{#vars}}"{{baseName}}" .= {{name}}{{^-last}}
      , {{/-last}}{{/vars}}
      ]

{{#vendorExtensions.x-has-mime-form-url-encoded}}
-- | FromForm {{classname}}
instance WH.FromForm {{classname}} where
  fromForm f =
    {{^hasVars}}pure {{/hasVars}}{{classname}}
      {{#hasVars}}<$>{{/hasVars}}{{#vars}} ({{#required}}WH.parseUnique {{/required}}{{^required}}WH.parseMaybe {{/required}}"{{baseName}}" f){{^-last}}
      <*>{{/-last}}{{/vars}}

-- | ToForm {{classname}}
instance WH.ToForm {{classname}} where
  toForm {{classname}} {{#hasVars}}{..}{{/hasVars}} =
    WH.Form $ HM.fromList $ P.catMaybes $
      [ {{#vars}}_toFormItem "{{baseName}}" ({{#required}}Just {{/required}}{{name}}){{^-last}}
      , {{/-last}}{{/vars}}
      ]
{{/vendorExtensions.x-has-mime-form-url-encoded}}

{{#generateModelConstructors}}
-- | Construct a value of type '{{classname}}' (by applying it's required fields, if any)
mk{{classname}}
  :: {{#vars}}{{#required}}{{{vendorExtensions.x-data-type}}} -- ^ '{{name}}'{{#description}}:{{/description}} {{{description}}}
  -> {{/required}}{{/vars}}{{classname}}
mk{{classname}} {{#vars}}{{#required}}{{name}} {{/required}}{{/vars}}=
  {{classname}}
  { {{#vars}}{{#required}}{{name}}{{/required}}{{^required}}{{name}} = {{#isArray}}Nothing{{/isArray}}{{#isMap}}Nothing{{/isMap}}{{^isContainer}}Nothing{{/isContainer}}{{/required}}{{^-last}}
  , {{/-last}}{{/vars}}
  }
{{/generateModelConstructors}}{{/isAlias}}{{/isEnum}}{{/model}}{{/models}}

{{#x-hasEnumSection}}-- * Enums
{{#x-allUniqueParams}}{{#x-enum}}

-- ** {{{x-param-name-type}}}

-- | Enum of '{{{x-data-type}}}'{{#description}} .
-- {{{.}}}{{/description}}
data {{{x-param-name-type}}}
  = {{#allowableValues}}{{#enumVars}}{{{name}}} -- ^ @{{{value}}}@
  {{^-last}}| {{/-last}}{{#-last}}deriving (P.Show, P.Eq, P.Typeable, P.Ord, P.Bounded, P.Enum){{/-last}}{{/enumVars}}{{/allowableValues}}

instance A.ToJSON {{{x-param-name-type}}} where toJSON = A.toJSON . from{{{x-param-name-type}}}
instance A.FromJSON {{{x-param-name-type}}} where parseJSON o = P.either P.fail (pure . P.id) . to{{{x-param-name-type}}} =<< A.parseJSON o
instance WH.ToHttpApiData {{{x-param-name-type}}} where toQueryParam = WH.toQueryParam . from{{{x-param-name-type}}}
instance WH.FromHttpApiData {{{x-param-name-type}}} where parseQueryParam o = WH.parseQueryParam o >>= P.left T.pack . to{{{x-param-name-type}}}
instance MimeRender MimeMultipartFormData {{{x-param-name-type}}} where mimeRender _ = mimeRenderDefaultMultipartFormData

-- | unwrap '{{{x-param-name-type}}}' enum
from{{{x-param-name-type}}} :: {{{x-param-name-type}}} -> {{{x-data-type}}}
from{{{x-param-name-type}}} = \case{{#allowableValues}}{{#enumVars}}
  {{{name}}} -> {{{value}}}{{/enumVars}}{{/allowableValues}}

-- | parse '{{{x-param-name-type}}}' enum
to{{{x-param-name-type}}} :: {{{x-data-type}}} -> P.Either String {{{x-param-name-type}}}
to{{{x-param-name-type}}} = \case{{#allowableValues}}{{#enumVars}}
  {{{value}}} -> P.Right {{{name}}}{{/enumVars}}{{/allowableValues}}
  s -> P.Left $ "to{{{x-param-name-type}}}: enum parse failure: " P.++ P.show s
{{/x-enum}}{{/x-allUniqueParams}}{{/x-hasEnumSection}}

{{#authMethods}}{{#-first}}-- * Auth Methods

{{/-first}}{{#isBasic}}-- ** {{name}}
data {{name}} =
  {{name}} B.ByteString B.ByteString -- ^ username password
  deriving (P.Eq, P.Show, P.Typeable)

instance AuthMethod {{name}} where
  applyAuthMethod _ a@({{name}} user pw) req =
    P.pure $
    if (P.typeOf a `P.elem` rAuthTypes req)
      then req `setHeader` toHeader ("Authorization", T.decodeUtf8 cred)
           & L.over rAuthTypesL (P.filter (/= P.typeOf a))
      else req
    where cred = BC.append "Basic " (B64.encode $ BC.concat [ user, ":", pw ])

{{/isBasic}}{{#isApiKey}}-- ** {{name}}
data {{name}} =
  {{name}} Text -- ^ secret
  deriving (P.Eq, P.Show, P.Typeable)

instance AuthMethod {{name}} where
  applyAuthMethod _ a@({{name}} secret) req =
    P.pure $
    if (P.typeOf a `P.elem` rAuthTypes req)
      then req {{#isKeyInHeader}}`setHeader` toHeader ("{{keyParamName}}", secret){{/isKeyInHeader}}{{^isKeyInHeader}}`setQuery` toQuery ("{{keyParamName}}", Just secret){{/isKeyInHeader}}
           & L.over rAuthTypesL (P.filter (/= P.typeOf a))
      else req

{{/isApiKey}}{{#isOAuth}}-- ** {{name}}
data {{name}} =
  {{name}} Text -- ^ secret
  deriving (P.Eq, P.Show, P.Typeable)

instance AuthMethod {{name}} where
  applyAuthMethod _ a@({{name}} secret) req =
    P.pure $
    if (P.typeOf a `P.elem` rAuthTypes req)
      then req `setHeader` toHeader ("Authorization", "Bearer " <> secret) 
           & L.over rAuthTypesL (P.filter (/= P.typeOf a))
      else req

{{/isOAuth}}{{/authMethods}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy