Skip to content

Commit 0b3b148

Browse files
committed
Header params, Request body
Added request body to OAOperation, header parameters
1 parent a1057be commit 0b3b148

File tree

9 files changed

+110
-19
lines changed

9 files changed

+110
-19
lines changed

Assets/PetStore/PetshopConsumeExample.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEngine;
44
using UnityOpenApi;
55
using PetStore;
6+
using Newtonsoft.Json;
67

78
public class PetshopConsumeExample : MonoBehaviour
89
{
@@ -15,6 +16,9 @@ public class PetshopConsumeExample : MonoBehaviour
1516
[SerializeField] PathItemAsset pet = null;
1617
[SerializeField] string petIdToGet = "5";
1718

19+
[Header("New pet")]
20+
[SerializeField] NewPet newPet;
21+
1822
[ContextMenu("Get Pets")]
1923
public void GetPets()
2024
{
@@ -42,4 +46,17 @@ public void GetPet()
4246
Debug.Log("Pet ID: " + pet.Id + ", type: " + pet.Type + ", price: " + pet.Price);
4347
});
4448
}
49+
50+
[ContextMenu("Create Pet")]
51+
public void CreatePet()
52+
{
53+
var operation = pets.GetOperation(AOOperationType.Post);
54+
var serialized = JsonConvert.SerializeObject(newPet);
55+
operation.SetRequestBody(serialized);
56+
57+
pets.ExecuteOperation<Pet>(operation, pet =>
58+
{
59+
Debug.Log("Pet ID: " + pet.Id + ", type: " + pet.Type + ", price: " + pet.Price);
60+
});
61+
}
4562
}

Assets/PetStore/_pets.asset

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ MonoBehaviour:
2020
- OperationId: findPets
2121
OperationType: 0
2222
Deprecated: 0
23+
RequestBody:
24+
Description:
25+
Required: 0
26+
LastRequestBody:
2327
Parameters:
2428
- Name: tags
2529
Required: 0
@@ -126,6 +130,10 @@ MonoBehaviour:
126130
- OperationId: addPet
127131
OperationType: 2
128132
Deprecated: 0
133+
RequestBody:
134+
Description: Pet to add to the store
135+
Required: 1
136+
LastRequestBody:
129137
Parameters: []
130138
ParametersValues: []
131139
Description: Creates a new pet in the store. Duplicates are allowed

Assets/PetStore/_pets_{id}.asset

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ MonoBehaviour:
2020
- OperationId: find pet by id
2121
OperationType: 0
2222
Deprecated: 0
23+
RequestBody:
24+
Description:
25+
Required: 0
26+
LastRequestBody:
2327
Parameters:
2428
- Name: id
2529
Required: 1
@@ -57,7 +61,7 @@ MonoBehaviour:
5761
IsLocal: 0
5862
Reference:
5963
UnresolvedReference: 0
60-
value: 2
64+
value:
6165
Description: Returns a user based on a single ID, if the user does not have access
6266
to the pet
6367
Summary:
@@ -67,6 +71,10 @@ MonoBehaviour:
6771
- OperationId: deletePet
6872
OperationType: 3
6973
Deprecated: 0
74+
RequestBody:
75+
Description:
76+
Required: 0
77+
LastRequestBody:
7078
Parameters:
7179
- Name: id
7280
Required: 1
@@ -104,7 +112,7 @@ MonoBehaviour:
104112
IsLocal: 0
105113
Reference:
106114
UnresolvedReference: 0
107-
value: 2
115+
value:
108116
Description: deletes a single pet based on the ID supplied
109117
Summary:
110118
Tags: []

Assets/PetStore/data_model/Pet.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
using System.Collections;
1+
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using UnityEngine;
45

56
namespace PetStore
67
{
7-
8+
[Serializable]
89
public class Pet
910
{
10-
public int Id { get; set; }
11-
public PetType Type { get; set; }
12-
public float Price { get; set; }
11+
public int Id;
12+
public PetType Type;
13+
public float Price;
14+
}
15+
16+
[Serializable]
17+
public class NewPet
18+
{
19+
public string Name;
20+
public PetType Type;
21+
public float Price;
1322
}
1423

24+
[Serializable]
1525
public enum PetType
1626
{
1727
dog, cat, fish, bird, gecko

Assets/Scenes/ApiTesting.unity

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ MonoBehaviour:
189189
limit: 3
190190
pet: {fileID: 11400000, guid: 86e050ea5e7107a4c99652da82e45aa4, type: 2}
191191
petIdToGet: 5
192+
newPet:
193+
Name: Birdy
194+
Type: 3
195+
Price: 0
192196
--- !u!4 &929404285
193197
Transform:
194198
m_ObjectHideFlags: 0

Assets/UnityOpenApi/OA Models/OAOperation.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ public class OAOperation
1515
public bool Deprecated;
1616
//public IDictionary<string, OpenApiCallback> Callbacks;
1717
//public OpenApiResponses Responses;
18-
//public OpenApiRequestBody RequestBody;
18+
public OARequestBody RequestBody;
1919
public List<OAParameter> Parameters;
2020
public List<ParameterValue> ParametersValues;
21-
//public OpenApiExternalDocs ExternalDocs;
21+
public OAExternalDocs ExternalDocs;
2222
public string Description;
2323
public string Summary;
2424
public List<OATag> Tags;
2525
public List<OAServer> Servers;
2626
public PathItemAsset pathAsset;
2727

28-
public OAOperation(OperationType operationType, OpenApiOperation openApiOperation, PathItemAsset pathItemAsset)
28+
public OAOperation(OperationType operationType, OpenApiOperation op, PathItemAsset pathItemAsset)
2929
{
3030
pathAsset = pathItemAsset;
31-
OperationId = openApiOperation.OperationId;
31+
OperationId = op.OperationId;
3232
OperationType = (AOOperationType)operationType;
33-
Summary = openApiOperation.Summary;
34-
Description = openApiOperation.Description;
35-
Deprecated = openApiOperation.Deprecated;
33+
Summary = op.Summary;
34+
Description = op.Description;
35+
Deprecated = op.Deprecated;
3636

37-
if (openApiOperation.Parameters.Count > 0)
37+
if (op.Parameters.Count > 0)
3838
{
39-
Parameters = openApiOperation.Parameters.Select(p => new OAParameter(p)).ToList();
39+
Parameters = op.Parameters.Select(p => new OAParameter(p)).ToList();
4040
}
4141
else
4242
{
@@ -45,9 +45,13 @@ public OAOperation(OperationType operationType, OpenApiOperation openApiOperatio
4545

4646
ParametersValues = Parameters.Select(p => new ParameterValue { parameter = p }).ToList();
4747

48-
Servers = openApiOperation.Servers.Select(s => new OAServer(s)).ToList();
48+
Servers = op.Servers.Select(s => new OAServer(s)).ToList();
4949

50-
Tags = openApiOperation.Tags.Select(t => new OATag(t)).ToList();
50+
Tags = op.Tags.Select(t => new OATag(t)).ToList();
51+
52+
RequestBody = new OARequestBody(op.RequestBody);
53+
54+
ExternalDocs = new OAExternalDocs(op.ExternalDocs);
5155
}
5256

5357
public void SetParameterValue(string parameterName, string val)
@@ -59,5 +63,10 @@ public void SetParameterValue(string parameterName, string val)
5963
}
6064
parVal.value = val;
6165
}
66+
67+
public void SetRequestBody(string body)
68+
{
69+
RequestBody.LastRequestBody = body;
70+
}
6271
}
6372
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.OpenApi.Models;
2+
using System;
3+
4+
namespace UnityOpenApi
5+
{
6+
[Serializable]
7+
public class OARequestBody
8+
{
9+
public string Description;
10+
public bool Required;
11+
12+
public string LastRequestBody;
13+
14+
public OARequestBody(OpenApiRequestBody requestBody)
15+
{
16+
if (requestBody == null) return;
17+
Description = requestBody.Description;
18+
Required = requestBody.Required;
19+
}
20+
}
21+
}

Assets/UnityOpenApi/OA Models/OARequestBody.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/UnityOpenApi/OpenApiAssets/ApiAsset.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ internal void ExecutePathOperation(OAOperation operation, Action<HttpRequestResu
5858
var pathParams = paramsWithValues.Where(p => p.parameter.In == OAParameterLocation.Path)
5959
.ToDictionary(p => p.parameter.Name, p => p.value);
6060

61+
var headerParams = paramsWithValues.Where(p => p.parameter.In == OAParameterLocation.Header)
62+
.ToDictionary(p => p.parameter.Name, p => p.value);
63+
6164
string operationPath = BuildPathWithParams(operation.pathAsset.Path, pathParams);
6265

6366
StringBuilder urlSb = new StringBuilder(BaseUrl);
@@ -69,7 +72,7 @@ internal void ExecutePathOperation(OAOperation operation, Action<HttpRequestResu
6972
switch (operation.OperationType)
7073
{
7174
case AOOperationType.Get:
72-
Http.HttpMono.Get(url, null, response);
75+
Http.HttpMono.Get(url, headerParams, response);
7376
break;
7477
case AOOperationType.Put:
7578
break;

0 commit comments

Comments
 (0)