Skip to content

Commit

Permalink
Add an example for optional field (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofpiyush authored Apr 24, 2022
1 parent 53a32c4 commit d4b699a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 122 deletions.
2 changes: 2 additions & 0 deletions example/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
try:
response = client.MakeHat(
ctx=Context(), request=haberdasher_pb2.Size(inches=12), server_path_prefix="/twirpy")
if not response.HasField('name'):
print("We didn't get a name!")
print(response)
except TwirpServerException as e:
print(e.code, e.message, e.meta, e.to_dict())
133 changes: 15 additions & 118 deletions example/generated/haberdasher_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/haberdasher.proto
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ message Hat {
string color = 2;

// The name of a hat is it's type. Like, 'bowler', or something.
string name = 3;
optional string name = 3;
}

// Size is passed when requesting a new hat to be made. It's always
Expand Down
9 changes: 6 additions & 3 deletions example/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ class HaberdasherService(object):
def MakeHat(self, context, size):
if size.inches <= 0:
raise InvalidArgument(argument="inches", error="I can't make a hat that small!")
return haberdasher_pb2.Hat(
response = haberdasher_pb2.Hat(
size=size.inches,
color= random.choice(["white", "black", "brown", "red", "blue"]),
name=random.choice(["bowler", "baseball cap", "top hat", "derby"])
color=random.choice(["white", "black", "brown", "red", "blue"]),
)
if random.random() > 0.5:
response.name = random.choice(["bowler", "baseball cap", "top hat", "derby"])

return response


service = haberdasher_twirp.HaberdasherServer(
Expand Down

0 comments on commit d4b699a

Please sign in to comment.