Skip to content

Commit 3a0ede0

Browse files
committed
feat: Introduce API key
Signed-off-by: Tom Kerkhove <[email protected]>
1 parent 56ef5d3 commit 3a0ede0

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

deploy/kubernetes/deploy-orders.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ spec:
2929
key: AZURESTORAGE_CONNECTIONSTRING
3030
- name: SHIPMENTS_API_URI
3131
value: http://contoso-microservices-shipments-internal-service:88/api/v1/shipments
32+
- name: SHIPMENTS_API_KEY
33+
valueFrom:
34+
secretKeyRef:
35+
name: orders-secrets
36+
key: SHIPMENTS_API_KEY
3237
livenessProbe:
3338
httpGet:
3439
path: /api/v1/health
@@ -66,4 +71,5 @@ metadata:
6671
app: contoso
6772
microservice: orders
6873
data:
69-
AZURESTORAGE_CONNECTIONSTRING: <base64 encoded connection string>
74+
AZURESTORAGE_CONNECTIONSTRING: <base64 encoded connection string>
75+
SHIPMENTS_API_KEY: <APIM API key>

src/microservices/Demo.Microservices.Orders.API/Services/ShipmentService.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ public ShipmentService(IConfiguration configuration)
2323
public async Task<ShipmentInformation> CreateAsync(Address address)
2424
{
2525
var shipmentBaseUri = _configuration["SHIPMENTS_API_URI"];
26-
26+
var request = new HttpRequestMessage(HttpMethod.Post, shipmentBaseUri);
27+
2728
var rawBody = JsonConvert.SerializeObject(address);
28-
var postBody = new StringContent(rawBody,Encoding.UTF8, "application/json");
29+
request.Content = new StringContent(rawBody,Encoding.UTF8, "application/json");
30+
31+
var shipmentApiKey = _configuration["SHIPMENTS_API_KEY"];
32+
if (!string.IsNullOrWhiteSpace(shipmentApiKey))
33+
{
34+
request.Headers.Add("API-Key", shipmentApiKey);
35+
}
2936

30-
var response = await _httpClient.PostAsync(shipmentBaseUri, postBody);
37+
var response = await _httpClient.SendAsync(request);
3138
if (response.StatusCode != HttpStatusCode.Created)
3239
{
3340
throw new Exception("Unable to initiate a shipment");

0 commit comments

Comments
 (0)