Golang swagger client with multiple responses - go

How can I generate a client from swagger to use it in a golang project that handles different response codes? Now I can get one that handle just success response (200).
My swagger file contents:
post:
summary: sending messages
description: send messages
operationId: SendMessage
parameters:
- in: body
required: true
name: body
schema:
$ref: '#/definitions/Request'
responses:
200:
description: OK
schema:
$ref: '#/definitions/Response1'
400:
description: Bad request
schema:
$ref: '#/definitions/Response2'
generate with:
docker run --rm -it -e GOPATH=/go -v "$(pwd):/work" -w /work quay.io/goswagger/swagger:latest generate client -f "./api/swagger-sender.yaml" -A Sender -
t internal/service/sender/client/gen
and all I get in client code is just one method to send message:
func (a *Client) SendMessage(params *SendMessageParams, opts ...ClientOption) (*SendMessageOK, error)
As you see only one type returned: *SendMessageOK and no one *SendMessageBadRequest
It looks like I can`t use go-swagger to generate desired client?

SendMessageBadRequest is an error too and SendMessage will return it as the error
The only you need is to check the type of error and if it is SendMessageBadRequest then you`ve got 400

Related

Redocly OpenAPI structure error. Property `openapi` is not expected here

I am trying to create an api documentation using redocly.
On my openapi.yaml it is linking to a yaml that has the api docs called kpi-documentation.yaml
link/$ref in openapi.yaml
/kpiDocumentation:
$ref: paths/kpi-documentation.yaml
I have an error in my visual studio code redocly preview extension that says
We found structural problems in your definition, please check the files below before running the preview.
File: /Users/xx/Desktop/Projects/api-docs/openapi/paths/kpi-documentation.yaml
Problem: Property `openapi` is not expected here.
File: /Users/xx/Desktop/Projects/api-docs/openapi/paths/kpi-documentation.yaml
Problem: Property `info` is not expected here.
File: /Users/xx/Desktop/Projects/api-docs/openapi/paths/kpi-documentation.yaml
Problem: Property `paths` is not expected here.
File: /Users/xx/Desktop/Projects/api-docs/openapi/paths/kpi-documentation.yaml
Problem: Property `components` is not expected here.
Part of code that I have in the kpi-documentation.yaml that appears to be throwing the error is
openapi: "3.1"
info:
title: KPI API
version: '1.0'
description: Documentation of API endpoints of KPI
servers:
- url: https://api.redocly.com
paths:
I have checked the documentation on the redocly website and it looks like my yaml structure is fine.
Also to note the kpi-documentation previews fine by itself when using the preview, but not when I preview the openapi.yaml which is the root file that needs to work.
https://redocly.com/docs/openapi-visual-reference/openapi/#OAS-3.0
rootfile
openapi.yaml
openapi: 3.1.0
info:
version: 1.0.0
title: KPI API documentation
termsOfService: 'https://example.com/terms/'
contact:
name: Brendan
url: 'http://example.com/contact'
license:
name: Apache 2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
x-logo:
url: 'https://www.feedgy.solar/wp-content/uploads/2022/07/Sans-titre-1.png'
tags:
- name: Insert Service 1
description: Example echo operations.
- name: Insert Service 2
description: Operations about users.
- name: Insert Service 3
description: This is a tag description.
- name: Insert Service 4
description: This is a tag description.
servers:
- url: 'https://{tenant}/api/v1'
variables:
tenant:
default: www
description: Your tenant id
- url: 'https://example.com/api/v1'
paths:
'/users/{username}':
$ref: 'paths/users_{username}.yaml'
/echo:
$ref: paths/echo.yaml
/pathItem:
$ref: paths/path-item.yaml
/pathItemWithExamples:
$ref: paths/path-item-with-examples.yaml
/kpiDocumentation:
$ref: 'paths/kpi-documentation.yaml'
pathitem file
kpi-documentation.yaml
openapi: "3.1"
info:
title: KPI API
version: '1.0'
description: Documentation of API endpoints of KPI
servers:
- url: https://api.redocly.com
paths:
"/api/v1/corrected-performance-ratio/plants/{id}":
get:
summary: Same as the Performance Ratio, but the ratio is done using Corrected Reference Yield, so it considers thermal losses in the panels as normal. The WCPR represents the losses in the BoS (balance of system), so everything from the panel DC output to the AC output.
operationId: corrected_performance_ratio_plants_retrieve
parameters:
- in: query
name: date_end
schema:
type: string
format: date
required: true
- in: query
name: date_start
schema:
type: string
format: date
required: true
- in: query
name: frequency
schema:
enum:
- H
- D
- M
- Y
type: string
default: H
minLength: 1
- in: path
name: id
schema:
type: integer
description: A unique integer value identifying this plant.
required: true
- in: query
name: threshold
schema:
type: integer
default: 50
tags:
- corrected-performance-ratio
security:
- tokenAuth: []
- cookieAuth: []
- {}
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/KPIResponse"
description: ""
"/api/v1/corrected-performance-ratio/plants/{id}/inverters":

yq add property to object somewhere in the tree

I'm trying to add a response to all objects with the key responses in an open api yaml file. I tried a lot and failed hard because I think I miss some basic understanding but I don't know what exactly.
File
openapi: 3.0.1
info:
title: my service
description: API for my service
version: 1.0.0
security:
- jwt:
- read
paths:
'/foo/{fooId}/firmware/update-request':
post:
parameters:
- name: fooId
in: path
description: Foo Id
required: true
schema:
type: string
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/FooRequest'
example:
firmwareVersion: "0.1"
responses:
'202':
description: Foo has been done
'400':
$ref: '#/components/responses/BadRequest'
'409':
$ref: '#/components/responses/Conflict'
'401':
$ref: '#/components/responses/Unauthorized'
'403':
$ref: '#/components/responses/Forbidden'
'404':
$ref: '#/components/responses/NotFound'
'500':
$ref: '#/components/responses/ServerError'
'502':
$ref: '#/components/responses/BadGateway'
'/foo/{fooId}/firmware/auto-update':
put:
parameters:
- name: fooId
in: path
description: Foo Id
required: true
schema:
type: string
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/FooUpdate'
example:
enabled: true
responses:
'204':
description: Foo update
'400':
$ref: '#/components/responses/BadRequest'
'401':
$ref: '#/components/responses/Unauthorized'
'403':
$ref: '#/components/responses/Forbidden'
'404':
$ref: '#/components/responses/NotFound'
'500':
$ref: '#/components/responses/ServerError'
'502':
$ref: '#/components/responses/BadGateway'
# goes on forever
Goal is that every response looks like
responses:
'204':
description: Foo update
# ...
'xxx':
description: yyy
while keeping the file structure intact.
What I tried:
yq -i '.. | select(has("responses")).responses | . += {"xxx": {"description": yyy}}' my.yaml
This was the idea, that came the closet my my expected result. But now every node from root to responses are missing.
What is the correct syntax to tell yq "Do this transformation in place on every node with this name without knowing it's level and without affecting every other node"?
You were almost there. Just wrap the whole search on the LHS into parentheses to eventually retain the outer context:
(.. | select(has("responses")).responses) += {"xxx": {"description": "yyy"}}
Note: I have also wrapped yyy in quotes and simplified | . += to just +=.
You could also just set the xxx field directly:
(.. | select(has("responses")).responses).xxx = {"description": "yyy"}
Both of these assume to be executed with mikefarah/yq. For a kislyuk/yq solution, add a ? to get has("responses")? as .. will also capture non-objects.
This was tested on mikefarah/yq version 4.20.2, and on kislyuk/yq version 3.0.2.

Send message to sqs while catch error in stepfunctions

i am using serverless framework with serverless-step-functions plugin. I want to check any errors in my stepfunction workflow and send this error to sqs queue.
Currently I want to pass all input as message to the queue(MessageBody: $). But if I get the data from the queue, message is $ (dollar sign) and not actual input. How can I send to queue the error message from the previous step?
States:
state1:
Type: Task
Resource:
Fn::GetAtt: [function1, Arn]
Next: state2
Catch:
- ErrorEquals: [States.ALL]
Next: sendErrorToDLQ
ResultPath: $.error
state2:
Type: Task
Resource:
Fn::GetAtt: [function2, Arn]
Next: done
Catch:
- ErrorEquals: [ States.ALL ]
Next: sendErrorToDLQ
ResultPath: $.error
sendErrorToDLQ:
Type: Task
Resource: arn:aws:states:::sqs:sendMessage
Parameters:
QueueUrl:
Ref: ServiceDeadLetterQueue
MessageBody: $ # <== how to pass input to sqs message
Next: fail
fail:
Type: Fail
done:
Type: Succeed
I have got the same when connecting SNS. As per the AWS doc, we have to follow the below structure to send the parameters
"MessageBody.$": "$"
Reference: https://docs.aws.amazon.com/step-functions/latest/dg/connect-sqs.html

How do I make that my hasura actions are ready to be used for my ci / cd tests?

I have started building up a backend with hasura. That backend is validated on my CI / CD service with api tests, among other things.
Within my hasura backend, I have implemented openfaas functions. I am deploying everything on a kubernetes cluster. Before running the tests, I wait until all jobs and all deployments are done. I am deploying with devspace which deploys everything through helm charts. So, at the end of the deployment, I am dead-sure the deployments are all done (ultimately, I've checked directly on the k8s cluster). Even the openfaas functions are deployed and ready to use.
Yet, when I run my acceptance tests, I run into issues. If I don't wait long enough, then my actions are not working properly. They return some strange errors that e.g. the response returned invalid json
Error: GraphQL error: not a valid json response from webhook
or the mutation is not in the mutation root
Error: GraphQL error: field "login" not found in type: 'mutation_root'
However, the openfaas functions themselves log only success. There is no error there. They are called and they apparently throw no error.
Waiting 3-5 minutes after hasura deployment or trying to call the actions until they return something relevant works fine, however. My current work-around is to wait an additional 5 minutes after my deployments have been done and only then run my api tests.
Is that normal? Is there a more efficient way to get feedback on when hasura really is ready to accept calls to its actions? I am currently working with version 1.2.1.
EDIT
After re-verification, waiting "long enough" does not help. What, however, helps, is calling some actions until they return successful answer. Currently, what I am doing is
#! /bin/sh
if [ "$#" -lt "3" ] ; then
echo "Usage: $0 <hasura-endpoint> <profile> <auth-app-id> [<timeout-in-sec> <deltat-in-sec>]"
exit 1
fi
ENDPOINT=$1
PROFILE=$2
AUTH_APP_ID=$3
TIMEOUT=${4:-300}
DELTA_T=${5:-5}
FIXTURES_FILE=./shared/fixtures/${PROFILE}/database/Users/auth.json
username=$(jq -r '.[1].email' $FIXTURES_FILE)
password=$(jq -r '.[1].password' $FIXTURES_FILE)
user_id=$(jq -r '.[1].id' $FIXTURES_FILE)
echo "Trying to login with $username / $password / $AUTH_APP_ID"
for iteration in `seq 1 $TIMEOUT`; do
result=$(gq $ENDPOINT -q 'mutation($username: String!, $password: String!, $appId: uuid!) { login(username: $username, password: $password, appId: $appId) { userId }}' -v "username=$username" -v "password=$password" -v "appId=$AUTH_APP_ID" | jq -r '.data.login.userId')
if [ "$result" == "$user_id" ] ; then
exit 0
else
sleep $DELTA_T
fi
done
echo "Hasura actions availability timed out" && exit 1
That performs logins with valid credentials until the action returns the right user id, and not an error. The log of this script on my ci / cd is something like
$ ./scripts/login_until_it_works.sh ${API_ENDPOINT}/v1/graphql $PROFILE $AUTH_ADMIN_APP_ID
Trying to login with nathalie.droz#test-vtxnet.ch / yl2YOuSrz_ / [MASKED]
Executing query... error
Error: ApolloError: GraphQL error: not a valid json response from webhook
at new ApolloError (/usr/local/lib/node_modules/graphqurl/node_modules/apollo-client/bundle.umd.js:92:26)
at Object.next (/usr/local/lib/node_modules/graphqurl/node_modules/apollo-client/bundle.umd.js:1297:31)
at notifySubscription (/usr/local/lib/node_modules/graphqurl/node_modules/zen-observable/lib/Observable.js:135:18)
at onNotify (/usr/local/lib/node_modules/graphqurl/node_modules/zen-observable/lib/Observable.js:179:3)
at SubscriptionObserver.next (/usr/local/lib/node_modules/graphqurl/node_modules/zen-observable/lib/Observable.js:235:7)
at /usr/local/lib/node_modules/graphqurl/node_modules/apollo-client/bundle.umd.js:1102:36
at Set.forEach (<anonymous>)
at Object.next (/usr/local/lib/node_modules/graphqurl/node_modules/apollo-client/bundle.umd.js:1101:21)
at notifySubscription (/usr/local/lib/node_modules/graphqurl/node_modules/zen-observable/lib/Observable.js:135:18)
at onNotify (/usr/local/lib/node_modules/graphqurl/node_modules/zen-observable/lib/Observable.js:179:3) {
graphQLErrors: [
{
extensions: [Object],
message: 'not a valid json response from webhook'
}
],
networkError: null,
message: 'GraphQL error: not a valid json response from webhook',
extraInfo: undefined
}
Executing query... done
Notice that the second query, 5 seconds after the first, is successful. My action is defined as follows:
- args:
enums: []
input_objects: []
objects:
- description: null
fields:
- description: null
name: token
type: String!
- description: null
name: refreshToken
type: String!
- description: null
name: userId
type: uuid!
name: LoginResponse
scalars: []
type: set_custom_types
- args:
comment: null
definition:
arguments:
- description: null
name: username
type: String!
- description: null
name: password
type: String!
- description: null
name: appId
type: uuid!
forward_client_headers: false
handler: http://gateway.openfaas:8080/function/login.{{FUNCTION_NAMESPACE}}
headers: []
kind: synchronous
output_type: LoginResponse
type: mutation
name: login
type: create_action
- args:
action: login
definition:
select:
filter: {}
role: incognito
type: create_action_permission
When you deploy via Helm, it creates the Deployments and everything else you've defined and tells you it's done. That doesn't mean that whatever you deployed is ready to serve requests. That's because each service may have its own boot time, especially the services who advertise High Availability.
Kubernetes is designed to address this issue with the help of "liveness/readiness probes". Basically, in your yaml/helm files you instruct K8s what it needs to check before it returns that a pod is ready. This could be for example a 200 HTTP status code from /live endpoint in your app or whatever.
Check this out: https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-startup-probes/

What kind of data structure is this?

I am pulling recent commits from github and trying to parse it using ruby. I know that I can parse it manually but I wanted to see if there was some package that could turn this into a hash or another data structure.
commits:
- parents:
- id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
author:
name: This guy
login: tguy
email: tguy#tguy.com
url: a url
id: e466354edb31f243899051e2119f4ce72bafd5f3
committed_date: "2010-07-19T13:44:43-07:00"
authored_date: "2010-07-19T13:33:26-07:00"
message: |-
message
- parents:
- id: c3c349ec3e9a3990cac4d256c308b18fd35d9606
author:
name: Other Guy
login: oguy
email: oguy#gmail.com
url: another url
id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
committed_date: "2010-07-19T13:44:11-07:00"
authored_date: "2010-07-19T13:44:11-07:00"
message: this is another message
This is YAML http://ruby-doc.org/core/classes/YAML.html. You can do something like obj = YAML::load yaml_string (and a require 'yaml' at the top of your file, its in the standard libs), and then access it like a nested hash.
YAML is basically used in the ruby world the way people use XML in the java/c# worlds.
Looks like YAML to me. There are parsers for a lot of languages. For example, with the YAML library included with Ruby:
data = <<HERE
commits:
- parents:
- id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
author:
name: This guy
login: tguy
email: tguy#tguy.com
url: a url
id: e466354edb31f243899051e2119f4ce72bafd5f3
committed_date: "2010-07-19T13:44:43-07:00"
authored_date: "2010-07-19T13:33:26-07:00"
message: |-
message
- parents:
- id: c3c349ec3e9a3990cac4d256c308b18fd35d9606
author:
name: Other Guy
login: oguy
email: oguy#gmail.com
url: another url
id: 202fb79e8686ee127fe49497c979cfc9c9d985d5
committed_date: "2010-07-19T13:44:11-07:00"
authored_date: "2010-07-19T13:44:11-07:00"
message: this is another message
HERE
pp YAML.load data
It prints:
{"commits"=>
[{"author"=>{"name"=>"This guy", "login"=>"tguy", "email"=>"tguy#tguy.com"},
"parents"=>[{"id"=>"202fb79e8686ee127fe49497c979cfc9c9d985d5"}],
"url"=>"a url",
"id"=>"e466354edb31f243899051e2119f4ce72bafd5f3",
"committed_date"=>"2010-07-19T13:44:43-07:00",
"authored_date"=>"2010-07-19T13:33:26-07:00",
"message"=>"message"},
{"author"=>
{"name"=>"Other Guy", "login"=>"oguy", "email"=>"oguy#gmail.com"},
"parents"=>[{"id"=>"c3c349ec3e9a3990cac4d256c308b18fd35d9606"}],
"url"=>"another url",
"id"=>"202fb79e8686ee127fe49497c979cfc9c9d985d5",
"committed_date"=>"2010-07-19T13:44:11-07:00",
"authored_date"=>"2010-07-19T13:44:11-07:00",
"message"=>"this is another message"}]}
This format is YAML, but you can get the same information in XML or JSON, see General API Information. I'm sure there are libraries to parse those formats in Ruby.
Although this isn't exactly what you're looking for, here's some more info on pulling commits. http://develop.github.com/p/commits.html. Otherwise, I think you may just need to manually parse it.

Resources