Skip to content

Commit

Permalink
Open API build (#776)
Browse files Browse the repository at this point in the history
* Copy from striped

* another

* replace with Stripe.Request

* delete files

* updates with base url

* Fix more core_resources

* Passing payment_methods tests

* Passing subscription tests

* Passing issuing tests

* Passing fraud tests

* most of connect tests passing

* fix

* fix after rebase

* add file upload

* passing tests

* more cleanup

* fix dupe module

* cleanup deps

* add assertion for Stripe-Account param in opts

* add test for put Stripe-Version header

* add test for exported functions

* update maintainers
  • Loading branch information
snewcomer authored Dec 30, 2022
1 parent b840c6e commit 0d9e435
Show file tree
Hide file tree
Showing 146 changed files with 128,005 additions and 12,229 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ need to set the `api_base_url` field in your config:
```
config :stripity_stripe,
api_key: "sk_test_thisisaboguskey",
api_base_url: "http://localhost:12111/v1/"
api_base_url: "http://localhost:12111"
```

# Documentation for 1.x.x
Expand Down
62 changes: 62 additions & 0 deletions lib/open_api.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
defmodule Stripe.OpenApi do
@moduledoc false

alias Stripe.OpenApi

defmacro __using__(opts) do
quote do
@pipeline Stripe.OpenApi.pipeline(unquote(opts))

{:ok, blueprint} = Stripe.OpenApi.run(@pipeline)

@version blueprint.api_version
end
end

def pipeline(options \\ []) do
[
{OpenApi.Phases.Parse, options},
{OpenApi.Phases.BuildModules, options},
{OpenApi.Phases.BuildOperations, options},
{OpenApi.Phases.BuildDocumentation, options},
{OpenApi.Phases.Compile, options},
{OpenApi.Phases.Version, options}
]
end

def run(pipeline) do
{:ok, _} =
pipeline
|> List.flatten()
|> run_phase(%OpenApiGen.Blueprint{})
end

defp run_phase(pipeline, input)

defp run_phase([], input) do
{:ok, input}
end

defp run_phase([phase_config | todo], input) do
{phase, options} = phase_invocation(phase_config)

case phase.run(input, options) do
{:ok, result} ->
run_phase(todo, result)

{:error, message} ->
{:error, message}

_ ->
{:error, "Last phase did not return a valid result tuple."}
end
end

defp phase_invocation({phase, options}) when is_list(options) do
{phase, options}
end

defp phase_invocation(phase) do
{phase, []}
end
end
4 changes: 4 additions & 0 deletions lib/openapi/blueprint.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule OpenApiGen.Blueprint do
@moduledoc false
defstruct [:components, :source, :operations, :modules, :api_version]
end
4 changes: 4 additions & 0 deletions lib/openapi/blueprint/any_of.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule OpenApiGen.Blueprint.AnyOf do
@moduledoc false
defstruct any_of: []
end
6 changes: 6 additions & 0 deletions lib/openapi/blueprint/list_of.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule OpenApiGen.Blueprint.ListOf do
@moduledoc false
defstruct [
:type_of
]
end
4 changes: 4 additions & 0 deletions lib/openapi/blueprint/module.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule OpenApiGen.Blueprint.Module do
@moduledoc false
defstruct [:name, :prefix, :description]
end
17 changes: 17 additions & 0 deletions lib/openapi/blueprint/operation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule OpenApiGen.Blueprint.Operation do
@moduledoc false
defstruct [
:id,
:method,
:path,
:name,
:description,
:parameters,
:path_parameters,
:query_parameters,
:deprecated,
:operation,
:body_parameters,
:success_response
]
end
4 changes: 4 additions & 0 deletions lib/openapi/blueprint/parameter.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule OpenApiGen.Blueprint.Parameter do
@moduledoc false
defstruct [:in, :name, :required, :schema]
end
4 changes: 4 additions & 0 deletions lib/openapi/blueprint/parameter/schema.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule OpenApiGen.Blueprint.Parameter.Schema do
@moduledoc false
defstruct [:name, :title, :type, items: [], properties: [], any_of: []]
end
6 changes: 6 additions & 0 deletions lib/openapi/blueprint/reference.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule OpenApiGen.Blueprint.Reference do
@moduledoc false
defstruct [
:name
]
end
4 changes: 4 additions & 0 deletions lib/openapi/blueprint/schema.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule OpenApiGen.Blueprint.Schema do
@moduledoc false
defstruct [:name, :description, :module, operations: [], properties: [], expandable_fields: []]
end
6 changes: 6 additions & 0 deletions lib/openapi/blueprint/search_result.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
defmodule OpenApiGen.Blueprint.SearchResult do
@moduledoc false
defstruct [
:type_of
]
end
16 changes: 16 additions & 0 deletions lib/openapi/path.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Stripe.OpenApi.Path do
@moduledoc false
def replace_path_params(path, path_param_defs, path_params_values) do
{path, []} =
path_param_defs
|> Enum.reduce({path, path_params_values}, fn path_param_def,
{path, [path_param_value | values]} ->
path_param_name = path_param_def.name

path = String.replace(path, "{#{path_param_name}}", path_param_value)
{path, values}
end)

path
end
end
29 changes: 29 additions & 0 deletions lib/openapi/phases/build_documentation.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Stripe.OpenApi.Phases.BuildDocumentation do
@moduledoc false
def run(blueprint, _options \\ []) do
operations =
Enum.map(blueprint.operations, fn {key, operation} ->
{key, build_description(operation)}
end)
|> Map.new()

{:ok, %{blueprint | operations: operations}}
end

defp build_description(operation) do
%{operation | description: do_build_description(operation)}
end

defp do_build_description(operation) do
description = operation.description

"""
#{description}
#### Details
* Method: `#{operation.method}`
* Path: `#{operation.path}`
"""
end
end
29 changes: 29 additions & 0 deletions lib/openapi/phases/build_modules.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Stripe.OpenApi.Phases.BuildModules do
@moduledoc false
def run(blueprint, _options \\ []) do
components =
for {name, map} <- blueprint.source["components"]["schemas"],
# map["x-stripeOperations"] != nil,
map["x-resourceId"] != nil || name == "api_errors",
into: %{} do
resource =
(map["x-resourceId"] || name) |> String.split(".") |> Enum.map(&Macro.camelize/1)

{name,
%OpenApiGen.Blueprint.Schema{
name: name,
description: map["description"],
operations:
(map["x-stripeOperations"] || [])
|> Enum.uniq_by(& &1["method_name"]) # see connect/account_test.exs
|> Enum.map(&%{&1 | "method_name" => Macro.underscore(&1["method_name"])}),
module: Module.concat(["Stripe" | resource]),
properties: map["properties"] || %{},
expandable_fields:
Map.get(map, "x-expandableFields", []) |> Enum.map(&String.to_atom/1)
}}
end

{:ok, %{blueprint | components: components}}
end
end
Loading

0 comments on commit 0d9e435

Please sign in to comment.