Coder Social home page Coder Social logo

grpc_requests's Introduction

grpc requests

PyPI - Python Version PyPI PyPI download month codecov Views

GRPC for Humans

from grpc_requests import Client

client = Client.get_by_endpoint("localhost:50051")
assert client.service_names == ["helloworld.Greeter"]

request_data = {"name": 'sinsky'} 
result = client.request("helloworld.Greeter", "SayHello", request_data)
print(result) # {"message":"Hellow sinsky"}

Feature

  • connect server using reflection
  • no need stub class request grpc
  • support all unary & stream method
  • support tls & compression connect

install

pip install grpc_requests

use it like RPC!

from grpc_requests import Client

client = Client.get_by_endpoint("localhost:50051")
# if you want connect tls
# client = Client.get_by_endpoint("localhost:443",ssl=True)
# or if you want Compression connect
# client = Client.get_by_endpoint("localhost:443",compression=grpc.Compression.Gzip)
assert client.service_names == ["helloworld.Greeter",'grpc.health.v1.Health']

health = client.service('grpc.health.v1.Health')
assert health.method_names == ('Check', 'Watch')

result = health.Check()
assert result == {'status': 'SERVING'}

greeter = client.service("helloworld.Greeter")

request_data = {"name": 'sinsky'}
result = greeter.SayHello(request_data)
results = greeter.SayHelloGroup(request_data)

requests_data = [{"name": 'sinsky'}]
result = greeter.HelloEveryone(requests_data)
results = greeter.SayHelloOneByOne(requests_data)

example

request unary-unary

service = "helloworld.Greeter"
unary_unary_method = 'SayHello'

request_data = {"name": 'sinsky'} # You Don't Need Stub!
result = client.request(service, unary_unary_method, request_data)
assert dict == type(result) # result is dict Type!!! not Stub Object!
assert {"message":"Hellow sinsky"} == result

# or

request_data = {"name": 'sinsky'} # You Don't Need Stub!
# any one know this method is unary-unary
result = client.unary_unary(service, unary_unary_method, request_data) 
assert dict == type(result) # result is dict Type!!! not Stub Object!
assert {"message":"Hellow sinsky"} == result

request unary-stream

unary_stream_method = 'SayHelloGroup'
unary_stream_results = client.request(service, unary_unary_method, request_data)
assert all([dict == type(result) for result in unary_stream_results])
assert [{"message":"Hellow sinsky"}] == list(unary_stream_results)

# or

unary_stream_results = client.unary_stream(service, unary_unary_method, request_data)
assert all([dict == type(result) for result in unary_stream_results])
assert [{"message":"Hellow sinsky"}] == list(unary_stream_results)

request stream-unary

requests_data = [request_data] # iterator
stream_unary_method = 'HelloEveryone'

result_stream_unary = client.request(service, stream_unary_method, requests_data)
assert dict == type(result) # result is dict Type!!! not Stub Object!

# or

result_stream_unary = client.stream_unary(service, stream_unary_method, requests_data)
assert dict == type(result) # result is dict Type!!! not Stub Object!

request stream-stream

requests_data = [request_data] # iterator
stream_stream_method = 'SayHelloOneByOne'

result = client.request(service, stream_stream_method,requests_data )
assert all([dict == type(result) for result in unary_stream_results])

# or

result = client.stream_stream(service, stream_stream_method,requests_data )
assert all([dict == type(result) for result in unary_stream_results])

using Stub

from grpc_requests import Client
from helloworld_pb2 import HelloRequest

port = '50051'
host = "localhost"
endpoint = f"{host}:{port}"

client = Client.get_by_endpoint(endpoint)
print(client.service_names) # ["helloworld.Greeter"]

service = "helloworld.Greeter"
method = 'SayHello'

result = client.unary_unary(service, method, HelloRequest(name='sinsky'))
print(type(result)) # result is dict Type!!! not Stub Object!
print(result) # {"message":"Hellow sinsky"}

# or get raw response data
result = client.unary_unary(service, method, HelloRequest(name='sinsky'),raw_output=True)
print(type(result)) # HelloReply stub class

Road map

  • support no reflection server

Relation Project

  • homi : micro grpc framework like flask. easy to use!

Change Logs

  • 0.0.7

    • Feature
      • support Compression
  • 0.0.6

    • Feature
      • support tls connect
  • 0.0.5

    • Change
      • response filed get orginal proto field(before returend lowerCamelCase)
  • 0.0.3

    • Feature
      • dynamic request method
      • service client
  • 0.0.2

    • support all method type
    • add request test case
  • 0.0.1

    • sync proto using reflection
    • auto convert request(response) from(to) dict
    • support unary-unary

grpc_requests's People

Contributors

wesky93 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.