# Copyright 2017 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # NOTE: This class is auto generated by the swagger code generator program. # https://github.com/swagger-api/swagger-codegen.git # Do not edit the class manually. defmodule GoogleApi.Calendar.V3.RequestBuilder do @moduledoc """ Helper functions for building Tesla requests """ @path_template_regex ~r/{(\+?[^}]+)}/i @successful_request_response 200..299 defmodule DataWrapper do defstruct [:data] end @doc """ Specify the request method when building a request ## Parameters - request (Map) - Collected request options - m (String) - Request method ## Returns Map """ @spec method(map(), atom()) :: map() def method(request, m) do Map.put_new(request, :method, m) end @doc """ Specify the request method when building a request ## Parameters - request (Map) - Collected request options - u (String) - Request URL ## Returns Map """ @spec url(map(), String.t(), map()) :: map() def url(request, u, replacements) do url(request, replace_path_template_vars(u, replacements)) end def url(request, u) do Map.put_new(request, :url, u) end def replace_path_template_vars(u, replacements) do Regex.replace(@path_template_regex, u, fn _, var -> replacement_value(var, replacements) end) end defp replacement_value("+" <> name, replacements) do URI.decode(replacement_value(name, replacements)) end defp replacement_value(name, replacements) do Map.get(replacements, name, "") end @doc """ Add optional parameters to the request ## Parameters - request (Map) - Collected request options - definitions (Map) - Map of parameter name to parameter location. - options (KeywordList) - The provided optional parameters ## Returns Map """ @spec add_optional_params(map(), map(), keyword()) :: map() def add_optional_params(request, _, []), do: request def add_optional_params(request, definitions, [{key, value} | tail]) do case definitions do %{^key => location} -> request |> add_param(location, key, value) |> add_optional_params(definitions, tail) _ -> add_optional_params(request, definitions, tail) end end @doc """ Add optional parameters to the request ## Parameters - request (Map) - Collected request options - location (atom) - Where to put the parameter - key (atom) - The name of the parameter - value (any) - The value of the parameter ## Returns Map """ @spec add_param(map(), atom(), atom(), any()) :: map() def add_param(request, :body, :body, value), do: Map.put(request, :body, value) def add_param(request, :body, key, value) do request |> Map.put_new_lazy(:body, &Tesla.Multipart.new/0) |> Map.update!( :body, &Tesla.Multipart.add_field( &1, key, Poison.encode!(value), headers: [{:"Content-Type", "application/json"}] ) ) end def add_param(request, :file, name, path) do request |> Map.put_new_lazy(:body, &Tesla.Multipart.new/0) |> Map.update!(:body, &Tesla.Multipart.add_file(&1, path, name: name)) end def add_param(request, :form, name, value) do Map.update(request, :body, %{name => value}, &Map.put(&1, name, value)) end def add_param(request, location, key, value) do Map.update(request, location, [{key, value}], &(&1 ++ [{key, value}])) end @doc """ Handle the response for a Tesla request ## Parameters - env (Tesla.Env) - The response object - struct (struct | false) - The shape of the struct to deserialize into. If false, returns the Tesla response. - opts (KeywordList) - [optional] Optional parameters - :dataWrapped (boolean()): If true, the remove the wrapping "data" field. ## Returns {:ok, struct} on success {:error, info} on failure """ @spec decode(Tesla.Env.t(), struct() | false | nil, keyword()) :: {:ok, struct()} | {:error, Tesla.Env.t()} def decode(env, struct \\ nil, opts \\ []) def decode(%Tesla.Env{status: status} = env, _struct, _opts) when status not in @successful_request_response do {:error, env} end def decode(%Tesla.Env{} = env, false, _opts) do {:ok, env} end def decode(%Tesla.Env{body: nil}, _struct, _opts) do {:ok, nil} end def decode(%Tesla.Env{body: body}, struct, dataWrapped: true) do Poison.decode(body, as: %GoogleApi.Calendar.V3.RequestBuilder.DataWrapper{}, struct: struct) |> case do {:ok, %{data: data}} -> {:ok, data} error -> error end end def decode(%Tesla.Env{body: body}, struct, _opts) do Poison.decode(body, as: struct) end end defimpl Poison.Decoder, for: GoogleApi.Calendar.V3.RequestBuilder.DataWrapper do import GoogleApi.Calendar.V3.Deserializer def decode(value, options) do deserialize(value, :data, :struct, options[:struct], options) end end