Skip to content

Communication with External Services

To help you build your own solution, find below an example YAML model configured with Http Clients to enable secure communication between APIs via Http calls.

In this use case, the Order service takes the shipping cost from an external service, outside the Beatrix-generated service mesh and before data persistence. To do this, the Order service must perform an HTTP call (with a valid access token) to the external service.


YAML Example:

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Name: HttpCommunication # Solution name
RootNamespace: Sample.HttpCommunication # Solution root namespace

# Business entities

Entities:
  - Name: Order
    KeyType: Guid
    Properties:
      - Name: Number
        Type: int
      - Name: Price
        Type: double
      - Name: ShippingCost
        Type: double

# Database instance types

DbInstances:
  - Name: PostgreSqlDbInstance
    Type: PostgreSql

Databases:
  - Name: OrderDatabase
    DbInstanceName: PostgreSqlDbInstance

  - Name: IdentityDatabase
    DbInstanceName: PostgreSqlDbInstance

# Identity Server configuration section

IdentityConfig:
  DatabaseName: IdentityDatabase # The database for Identity Server data storage

# Database data contexts

DbContexts:
  - Name: OrderDbContext
    DatabaseName: OrderDatabase
    DefaultSchema: sample

# Data access repository pattern

Repositories:
  - Name: OrderRepository
    EntityName: Order
    DbContextName: OrderDbContext

# Http message handlers (Middleware)

HttpHandlers:
  - Name: ClientAppTokenHandler
    Type: AddClientAccessToken # Adds a ClientApp Access Token to all requests of the Http Client
    BackChannelName: IdentityBackChannel
    AddClientAccessTokenParams:
      Scopes:
        - OrderService # The Token will only have the scope of the origin service, decreasing the attack surface

# Http Clients to allow communications between services

HttpCommunications:
  - Name: IdentityBackChannel
    BaseAddress: https://identity-domain/

  - Name: ShippingApiCommunication
    BaseAddress: https://company-domain/shippingservice/
    Headers:
      Accept: application/json
    Handlers:
      - ClientAppTokenHandler  # Run the HttpHandler for each request

# Services and Controllers (CRUD)

Services:
  - Name: OrderService
    Type: WebApi
    Controllers:
      - Name: Order
        Type: Crud
        EntityName: Order
        RepositoryName: OrderRepository
        Contexts:
          - Context: Create
            Rules: # Business rules performed only for the "Create" context
              - Name: CalculateShippingCost # Business rule that must be implemented to obtain shipping cost. Here the HttpClient configured in the service must be injected.
                Type: BlankRule
                Order: 1
              - Name: AddInRepository
                Type: WriteOperationRule # Executes a business rule for persisting data in the database
                Order: 2

          - Context: Update
            Rules:
              - Name: UpdateInRepository
                Type: WriteOperationRule
                Order: 1

          - Context: UpdatePartial
            Rules:
              - Name: UpdateInRepository
                Type: WriteOperationRule
                Order: 1

          - Context: Delete
            Rules:
              - Name: DeleteInRepository
                Type: WriteOperationRule
                Order: 1

          - Context: ReadAll
            Rules:
              - Name: ReadInRepository
                Type: ReadOperationRule # Executes a business rule for reading data from the database
                Order: 1

          - Context: ReadByKey
            Rules:
              - Name: ReadInRepository
                Type: ReadOperationRule
                Order: 1
    HttpCommunications: # Configure HttpCommunication as a http client in the Startup class
      - ShippingApiCommunication

Back to top