add the weather

This commit is contained in:
Asim Aslam
2021-06-21 14:45:53 +01:00
parent 59b0cc6ecb
commit c77fdbd424
13 changed files with 781 additions and 0 deletions

2
weather/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
weather

3
weather/Dockerfile Normal file
View File

@@ -0,0 +1,3 @@
FROM alpine
ADD weather /weather
ENTRYPOINT [ "/weather" ]

28
weather/Makefile Normal file
View File

@@ -0,0 +1,28 @@
GOPATH:=$(shell go env GOPATH)
.PHONY: init
init:
go get -u github.com/golang/protobuf/proto
go get -u github.com/golang/protobuf/protoc-gen-go
go get github.com/micro/micro/v3/cmd/protoc-gen-micro
go get github.com/micro/micro/v3/cmd/protoc-gen-openapi
.PHONY: api
api:
protoc --openapi_out=. --proto_path=. proto/weather.proto
.PHONY: proto
proto:
protoc --proto_path=. --micro_out=. --go_out=:. proto/weather.proto
.PHONY: build
build:
go build -o weather *.go
.PHONY: test
test:
go test -v ./... -cover
.PHONY: docker
docker:
docker build . -t weather:latest

7
weather/README.md Normal file
View File

@@ -0,0 +1,7 @@
Real time weather API
# Weather Service
Get real time weather information including historic and future forecast data.
Powered by [Weather API](https://www.weatherapi.com/)

29
weather/examples.json Normal file
View File

@@ -0,0 +1,29 @@
{
"price": [{
"title": "Get the current weather",
"description": "Returns the current weather for a location",
"request": {
"location": "los angeles"
},
"response": {
"location": "Los Angeles",
"region": "California",
"country": "California",
"latitude": 34.05,
"longitude": -118.24,
"timezone": "America/Los_Angeles",
"local_time": "2021-06-21 6:44",
"temp_c": 18.3,
"temp_f": 64.9,
"feels_like_c": 18.3,
"feels_like_f": 64.9,
"humidity": 81,
"cloud": 100,
"daytime": true,
"condition": "Overcast",
"icon_url": "//cdn.weatherapi.com/weather/64x64/day/122.png",
"wind_direction": "N"
}
}]
}

2
weather/generate.go Normal file
View File

@@ -0,0 +1,2 @@
package main
//go:generate make proto

106
weather/handler/weather.go Normal file
View File

@@ -0,0 +1,106 @@
package handler
import (
"context"
"encoding/json"
"io/ioutil"
"fmt"
"net/http"
"github.com/micro/micro/v3/service/logger"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/errors"
pb "github.com/micro/services/weather/proto"
)
type Weather struct{
Api string
Key string
}
func New() *Weather {
// TODO: look for "weather.provider" to determine the handler
v, err := config.Get("weatherapi.api")
if err != nil {
logger.Fatalf("weatherapi.api config not found: %v", err)
}
api := v.String("")
if len(api) == 0 {
logger.Fatal("weatherapi.api config not found")
}
v, err = config.Get("weatherapi.key")
if err != nil {
logger.Fatalf("weatherapi.key config not found: %v", err)
}
key := v.String("")
if len(key) == 0 {
logger.Fatal("weatherapi.key config not found")
}
return &Weather{
Api: api,
Key: key,
}
}
func (w *Weather) Now(ctx context.Context, req *pb.NowRequest, rsp *pb.NowResponse) error {
if len(req.Location) <= 0 {
return errors.BadRequest("weather.current", "invalid location")
}
uri := fmt.Sprintf("%scurrent.json?aqi=no&key=%s&q=%s", w.Api, w.Key, req.Location)
resp, err := http.Get(uri)
if err != nil {
logger.Errorf("Failed to get current weather: %v\n", err)
return errors.InternalServerError("weather.current", "failed to get current weather")
}
defer resp.Body.Close()
b, _ := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
logger.Errorf("Failed to get current weather (non 200): %d %v\n", resp.StatusCode, string(b))
return errors.InternalServerError("weather.current", "failed to get current weather")
}
var respBody map[string]interface{}
if err := json.Unmarshal(b, &respBody); err != nil {
logger.Errorf("Failed to unmarshal current: %v\n", err)
return errors.InternalServerError("weather.current", "failed to get current")
}
location := respBody["location"].(map[string]interface{})
current := respBody["current"].(map[string]interface{})
// set the location
rsp.Location = location["name"].(string)
rsp.Region = location["region"].(string)
rsp.Country = location["region"].(string)
rsp.Latitude = location["lat"].(float64)
rsp.Longitude = location["lon"].(float64)
rsp.Timezone = location["tz_id"].(string)
rsp.LocalTime = location["localtime"].(string)
// set the time of day
if current["is_day"].(float64) == 1.0 {
rsp.Daytime = true
}
rsp.TempC = current["temp_c"].(float64)
rsp.TempF = current["temp_f"].(float64)
rsp.FeelsLikeC = current["feelslike_c"].(float64)
rsp.FeelsLikeF = current["feelslike_f"].(float64)
rsp.Humidity = int32(current["humidity"].(float64))
rsp.Cloud = int32(current["cloud"].(float64))
rsp.Condition = current["condition"].(map[string]interface{})["text"].(string)
rsp.IconUrl = current["condition"].(map[string]interface{})["icon"].(string)
rsp.WindMph = current["wind_mph"].(float64)
rsp.WindKph = current["wind_kph"].(float64)
rsp.WindDirection = current["wind_dir"].(string)
rsp.WindDegree = int32(current["wind_degree"].(float64))
return nil
}

24
weather/main.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import (
"github.com/micro/services/weather/handler"
pb "github.com/micro/services/weather/proto"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/logger"
)
func main() {
// Create service
srv := service.New(
service.Name("weather"),
service.Version("latest"),
)
// Register handler
pb.RegisterWeatherHandler(srv.Server(), handler.New())
// Run service
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}

1
weather/micro.mu Normal file
View File

@@ -0,0 +1 @@
service weather

419
weather/proto/weather.pb.go Normal file
View File

@@ -0,0 +1,419 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.26.0
// protoc v3.15.6
// source: proto/weather.proto
package weather
import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
// Get the current weather report for a location by postcode, city, zip code, ip address
type NowRequest struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// location to get weather e.g postcode, city
Location string `protobuf:"bytes,1,opt,name=location,proto3" json:"location,omitempty"`
}
func (x *NowRequest) Reset() {
*x = NowRequest{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_weather_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *NowRequest) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*NowRequest) ProtoMessage() {}
func (x *NowRequest) ProtoReflect() protoreflect.Message {
mi := &file_proto_weather_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use NowRequest.ProtoReflect.Descriptor instead.
func (*NowRequest) Descriptor() ([]byte, []int) {
return file_proto_weather_proto_rawDescGZIP(), []int{0}
}
func (x *NowRequest) GetLocation() string {
if x != nil {
return x.Location
}
return ""
}
type NowResponse struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
// location of the request
Location string `protobuf:"bytes,1,opt,name=location,proto3" json:"location,omitempty"`
// region related to the location
Region string `protobuf:"bytes,2,opt,name=region,proto3" json:"region,omitempty"`
// country of the request
Country string `protobuf:"bytes,3,opt,name=country,proto3" json:"country,omitempty"`
// e.g 37.55
Latitude float64 `protobuf:"fixed64,4,opt,name=latitude,proto3" json:"latitude,omitempty"`
// e.g -77.46
Longitude float64 `protobuf:"fixed64,5,opt,name=longitude,proto3" json:"longitude,omitempty"`
// timezone of the location
Timezone string `protobuf:"bytes,6,opt,name=timezone,proto3" json:"timezone,omitempty"`
// the local time
LocalTime string `protobuf:"bytes,7,opt,name=local_time,json=localTime,proto3" json:"local_time,omitempty"`
// temperature in celsius
TempC float64 `protobuf:"fixed64,8,opt,name=temp_c,json=tempC,proto3" json:"temp_c,omitempty"`
// temperature in fahrenheit
TempF float64 `protobuf:"fixed64,9,opt,name=temp_f,json=tempF,proto3" json:"temp_f,omitempty"`
// feels like in celsius
FeelsLikeC float64 `protobuf:"fixed64,10,opt,name=feels_like_c,json=feelsLikeC,proto3" json:"feels_like_c,omitempty"`
// feels like in fahrenheit
FeelsLikeF float64 `protobuf:"fixed64,11,opt,name=feels_like_f,json=feelsLikeF,proto3" json:"feels_like_f,omitempty"`
// the humidity percentage
Humidity int32 `protobuf:"varint,12,opt,name=humidity,proto3" json:"humidity,omitempty"`
// cloud cover percentage
Cloud int32 `protobuf:"varint,13,opt,name=cloud,proto3" json:"cloud,omitempty"`
// whether its daytime
Daytime bool `protobuf:"varint,14,opt,name=daytime,proto3" json:"daytime,omitempty"`
// the weather condition
Condition string `protobuf:"bytes,15,opt,name=condition,proto3" json:"condition,omitempty"`
// the related icon
IconUrl string `protobuf:"bytes,16,opt,name=icon_url,json=iconUrl,proto3" json:"icon_url,omitempty"`
// wind in mph
WindMph float64 `protobuf:"fixed64,17,opt,name=wind_mph,json=windMph,proto3" json:"wind_mph,omitempty"`
// wind in kph
WindKph float64 `protobuf:"fixed64,18,opt,name=wind_kph,json=windKph,proto3" json:"wind_kph,omitempty"`
// wind direction
WindDirection string `protobuf:"bytes,19,opt,name=wind_direction,json=windDirection,proto3" json:"wind_direction,omitempty"`
// wind degree
WindDegree int32 `protobuf:"varint,20,opt,name=wind_degree,json=windDegree,proto3" json:"wind_degree,omitempty"`
}
func (x *NowResponse) Reset() {
*x = NowResponse{}
if protoimpl.UnsafeEnabled {
mi := &file_proto_weather_proto_msgTypes[1]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *NowResponse) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*NowResponse) ProtoMessage() {}
func (x *NowResponse) ProtoReflect() protoreflect.Message {
mi := &file_proto_weather_proto_msgTypes[1]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use NowResponse.ProtoReflect.Descriptor instead.
func (*NowResponse) Descriptor() ([]byte, []int) {
return file_proto_weather_proto_rawDescGZIP(), []int{1}
}
func (x *NowResponse) GetLocation() string {
if x != nil {
return x.Location
}
return ""
}
func (x *NowResponse) GetRegion() string {
if x != nil {
return x.Region
}
return ""
}
func (x *NowResponse) GetCountry() string {
if x != nil {
return x.Country
}
return ""
}
func (x *NowResponse) GetLatitude() float64 {
if x != nil {
return x.Latitude
}
return 0
}
func (x *NowResponse) GetLongitude() float64 {
if x != nil {
return x.Longitude
}
return 0
}
func (x *NowResponse) GetTimezone() string {
if x != nil {
return x.Timezone
}
return ""
}
func (x *NowResponse) GetLocalTime() string {
if x != nil {
return x.LocalTime
}
return ""
}
func (x *NowResponse) GetTempC() float64 {
if x != nil {
return x.TempC
}
return 0
}
func (x *NowResponse) GetTempF() float64 {
if x != nil {
return x.TempF
}
return 0
}
func (x *NowResponse) GetFeelsLikeC() float64 {
if x != nil {
return x.FeelsLikeC
}
return 0
}
func (x *NowResponse) GetFeelsLikeF() float64 {
if x != nil {
return x.FeelsLikeF
}
return 0
}
func (x *NowResponse) GetHumidity() int32 {
if x != nil {
return x.Humidity
}
return 0
}
func (x *NowResponse) GetCloud() int32 {
if x != nil {
return x.Cloud
}
return 0
}
func (x *NowResponse) GetDaytime() bool {
if x != nil {
return x.Daytime
}
return false
}
func (x *NowResponse) GetCondition() string {
if x != nil {
return x.Condition
}
return ""
}
func (x *NowResponse) GetIconUrl() string {
if x != nil {
return x.IconUrl
}
return ""
}
func (x *NowResponse) GetWindMph() float64 {
if x != nil {
return x.WindMph
}
return 0
}
func (x *NowResponse) GetWindKph() float64 {
if x != nil {
return x.WindKph
}
return 0
}
func (x *NowResponse) GetWindDirection() string {
if x != nil {
return x.WindDirection
}
return ""
}
func (x *NowResponse) GetWindDegree() int32 {
if x != nil {
return x.WindDegree
}
return 0
}
var File_proto_weather_proto protoreflect.FileDescriptor
var file_proto_weather_proto_rawDesc = []byte{
0x0a, 0x13, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x77, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x07, 0x77, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x22, 0x28,
0x0a, 0x0a, 0x4e, 0x6f, 0x77, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08,
0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08,
0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x22, 0xc5, 0x04, 0x0a, 0x0b, 0x4e, 0x6f, 0x77,
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x6c, 0x6f, 0x63, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6c, 0x6f, 0x63, 0x61,
0x74, 0x69, 0x6f, 0x6e, 0x12, 0x16, 0x0a, 0x06, 0x72, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x72, 0x65, 0x67, 0x69, 0x6f, 0x6e, 0x12, 0x18, 0x0a, 0x07,
0x63, 0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63,
0x6f, 0x75, 0x6e, 0x74, 0x72, 0x79, 0x12, 0x1a, 0x0a, 0x08, 0x6c, 0x61, 0x74, 0x69, 0x74, 0x75,
0x64, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x6c, 0x61, 0x74, 0x69, 0x74, 0x75,
0x64, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18,
0x05, 0x20, 0x01, 0x28, 0x01, 0x52, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65,
0x12, 0x1a, 0x0a, 0x08, 0x74, 0x69, 0x6d, 0x65, 0x7a, 0x6f, 0x6e, 0x65, 0x18, 0x06, 0x20, 0x01,
0x28, 0x09, 0x52, 0x08, 0x74, 0x69, 0x6d, 0x65, 0x7a, 0x6f, 0x6e, 0x65, 0x12, 0x1d, 0x0a, 0x0a,
0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09,
0x52, 0x09, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x15, 0x0a, 0x06, 0x74,
0x65, 0x6d, 0x70, 0x5f, 0x63, 0x18, 0x08, 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x74, 0x65, 0x6d,
0x70, 0x43, 0x12, 0x15, 0x0a, 0x06, 0x74, 0x65, 0x6d, 0x70, 0x5f, 0x66, 0x18, 0x09, 0x20, 0x01,
0x28, 0x01, 0x52, 0x05, 0x74, 0x65, 0x6d, 0x70, 0x46, 0x12, 0x20, 0x0a, 0x0c, 0x66, 0x65, 0x65,
0x6c, 0x73, 0x5f, 0x6c, 0x69, 0x6b, 0x65, 0x5f, 0x63, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x01, 0x52,
0x0a, 0x66, 0x65, 0x65, 0x6c, 0x73, 0x4c, 0x69, 0x6b, 0x65, 0x43, 0x12, 0x20, 0x0a, 0x0c, 0x66,
0x65, 0x65, 0x6c, 0x73, 0x5f, 0x6c, 0x69, 0x6b, 0x65, 0x5f, 0x66, 0x18, 0x0b, 0x20, 0x01, 0x28,
0x01, 0x52, 0x0a, 0x66, 0x65, 0x65, 0x6c, 0x73, 0x4c, 0x69, 0x6b, 0x65, 0x46, 0x12, 0x1a, 0x0a,
0x08, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x18, 0x0c, 0x20, 0x01, 0x28, 0x05, 0x52,
0x08, 0x68, 0x75, 0x6d, 0x69, 0x64, 0x69, 0x74, 0x79, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6c, 0x6f,
0x75, 0x64, 0x18, 0x0d, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x63, 0x6c, 0x6f, 0x75, 0x64, 0x12,
0x18, 0x0a, 0x07, 0x64, 0x61, 0x79, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x0e, 0x20, 0x01, 0x28, 0x08,
0x52, 0x07, 0x64, 0x61, 0x79, 0x74, 0x69, 0x6d, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x63, 0x6f, 0x6e,
0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x0f, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x63, 0x6f,
0x6e, 0x64, 0x69, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x19, 0x0a, 0x08, 0x69, 0x63, 0x6f, 0x6e, 0x5f,
0x75, 0x72, 0x6c, 0x18, 0x10, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x69, 0x63, 0x6f, 0x6e, 0x55,
0x72, 0x6c, 0x12, 0x19, 0x0a, 0x08, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x6d, 0x70, 0x68, 0x18, 0x11,
0x20, 0x01, 0x28, 0x01, 0x52, 0x07, 0x77, 0x69, 0x6e, 0x64, 0x4d, 0x70, 0x68, 0x12, 0x19, 0x0a,
0x08, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x6b, 0x70, 0x68, 0x18, 0x12, 0x20, 0x01, 0x28, 0x01, 0x52,
0x07, 0x77, 0x69, 0x6e, 0x64, 0x4b, 0x70, 0x68, 0x12, 0x25, 0x0a, 0x0e, 0x77, 0x69, 0x6e, 0x64,
0x5f, 0x64, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x13, 0x20, 0x01, 0x28, 0x09,
0x52, 0x0d, 0x77, 0x69, 0x6e, 0x64, 0x44, 0x69, 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12,
0x1f, 0x0a, 0x0b, 0x77, 0x69, 0x6e, 0x64, 0x5f, 0x64, 0x65, 0x67, 0x72, 0x65, 0x65, 0x18, 0x14,
0x20, 0x01, 0x28, 0x05, 0x52, 0x0a, 0x77, 0x69, 0x6e, 0x64, 0x44, 0x65, 0x67, 0x72, 0x65, 0x65,
0x32, 0x3d, 0x0a, 0x07, 0x57, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x12, 0x32, 0x0a, 0x03, 0x4e,
0x6f, 0x77, 0x12, 0x13, 0x2e, 0x77, 0x65, 0x61, 0x74, 0x68, 0x65, 0x72, 0x2e, 0x4e, 0x6f, 0x77,
0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x77, 0x65, 0x61, 0x74, 0x68, 0x65,
0x72, 0x2e, 0x4e, 0x6f, 0x77, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42,
0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x77, 0x65, 0x61, 0x74, 0x68,
0x65, 0x72, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_proto_weather_proto_rawDescOnce sync.Once
file_proto_weather_proto_rawDescData = file_proto_weather_proto_rawDesc
)
func file_proto_weather_proto_rawDescGZIP() []byte {
file_proto_weather_proto_rawDescOnce.Do(func() {
file_proto_weather_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_weather_proto_rawDescData)
})
return file_proto_weather_proto_rawDescData
}
var file_proto_weather_proto_msgTypes = make([]protoimpl.MessageInfo, 2)
var file_proto_weather_proto_goTypes = []interface{}{
(*NowRequest)(nil), // 0: weather.NowRequest
(*NowResponse)(nil), // 1: weather.NowResponse
}
var file_proto_weather_proto_depIdxs = []int32{
0, // 0: weather.Weather.Now:input_type -> weather.NowRequest
1, // 1: weather.Weather.Now:output_type -> weather.NowResponse
1, // [1:2] is the sub-list for method output_type
0, // [0:1] is the sub-list for method input_type
0, // [0:0] is the sub-list for extension type_name
0, // [0:0] is the sub-list for extension extendee
0, // [0:0] is the sub-list for field type_name
}
func init() { file_proto_weather_proto_init() }
func file_proto_weather_proto_init() {
if File_proto_weather_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_proto_weather_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*NowRequest); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
file_proto_weather_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*NowResponse); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_proto_weather_proto_rawDesc,
NumEnums: 0,
NumMessages: 2,
NumExtensions: 0,
NumServices: 1,
},
GoTypes: file_proto_weather_proto_goTypes,
DependencyIndexes: file_proto_weather_proto_depIdxs,
MessageInfos: file_proto_weather_proto_msgTypes,
}.Build()
File_proto_weather_proto = out.File
file_proto_weather_proto_rawDesc = nil
file_proto_weather_proto_goTypes = nil
file_proto_weather_proto_depIdxs = nil
}

View File

@@ -0,0 +1,93 @@
// Code generated by protoc-gen-micro. DO NOT EDIT.
// source: proto/weather.proto
package weather
import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
math "math"
)
import (
context "context"
api "github.com/micro/micro/v3/service/api"
client "github.com/micro/micro/v3/service/client"
server "github.com/micro/micro/v3/service/server"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
// Reference imports to suppress errors if they are not otherwise used.
var _ api.Endpoint
var _ context.Context
var _ client.Option
var _ server.Option
// Api Endpoints for Weather service
func NewWeatherEndpoints() []*api.Endpoint {
return []*api.Endpoint{}
}
// Client API for Weather service
type WeatherService interface {
Now(ctx context.Context, in *NowRequest, opts ...client.CallOption) (*NowResponse, error)
}
type weatherService struct {
c client.Client
name string
}
func NewWeatherService(name string, c client.Client) WeatherService {
return &weatherService{
c: c,
name: name,
}
}
func (c *weatherService) Now(ctx context.Context, in *NowRequest, opts ...client.CallOption) (*NowResponse, error) {
req := c.c.NewRequest(c.name, "Weather.Now", in)
out := new(NowResponse)
err := c.c.Call(ctx, req, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for Weather service
type WeatherHandler interface {
Now(context.Context, *NowRequest, *NowResponse) error
}
func RegisterWeatherHandler(s server.Server, hdlr WeatherHandler, opts ...server.HandlerOption) error {
type weather interface {
Now(ctx context.Context, in *NowRequest, out *NowResponse) error
}
type Weather struct {
weather
}
h := &weatherHandler{hdlr}
return s.Handle(s.NewHandler(&Weather{h}, opts...))
}
type weatherHandler struct {
WeatherHandler
}
func (h *weatherHandler) Now(ctx context.Context, in *NowRequest, out *NowResponse) error {
return h.WeatherHandler.Now(ctx, in, out)
}

View File

@@ -0,0 +1,59 @@
syntax = "proto3";
package weather;
option go_package = "./proto;weather";
service Weather {
rpc Now(NowRequest) returns (NowResponse) {}
}
// Get the current weather report for a location by postcode, city, zip code, ip address
message NowRequest {
// location to get weather e.g postcode, city
string location = 1;
}
message NowResponse {
// location of the request
string location = 1;
// region related to the location
string region = 2;
// country of the request
string country = 3;
// e.g 37.55
double latitude = 4;
// e.g -77.46
double longitude = 5;
// timezone of the location
string timezone = 6;
// the local time
string local_time = 7;
// temperature in celsius
double temp_c = 8;
// temperature in fahrenheit
double temp_f = 9;
// feels like in celsius
double feels_like_c = 10;
// feels like in fahrenheit
double feels_like_f = 11;
// the humidity percentage
int32 humidity = 12;
// cloud cover percentage
int32 cloud = 13;
// whether its daytime
bool daytime = 14;
// the weather condition
string condition = 15;
// the related icon
string icon_url = 16;
// wind in mph
double wind_mph = 17;
// wind in kph
double wind_kph = 18;
// wind direction
string wind_direction = 19;
// wind degree
int32 wind_degree = 20;
}

8
weather/publicapi.json Normal file
View File

@@ -0,0 +1,8 @@
{
"name": "weather",
"icon": "☀️",
"category": "climate",
"pricing": {
"Weather.Now": 1
}
}