Skip to content

move duplication in services to Connection.get and Connection.post#3

Merged
bedrock-adam merged 3 commits intomasterfrom
connection/get/add_element_name
Apr 29, 2020
Merged

move duplication in services to Connection.get and Connection.post#3
bedrock-adam merged 3 commits intomasterfrom
connection/get/add_element_name

Conversation

@bedrock-adam
Copy link
Contributor

@bedrock-adam bedrock-adam commented Apr 27, 2020

Motivation (Why)

Tony and I will soon need to raise AccessTokenRejected and limiting the duplication to the Connection methods helps a lot with that.

Later we can reduce the duplication in Connection too - just not yet.

Consequence

Screenshot from 2020-04-30 08-52-14

No change in terms of callers as Connection is encapsulated.

@bedrock-adam bedrock-adam changed the title Connection.get - add element_name [spike] Connection.get(element_name:) [spike] Apr 27, 2020
def get(endpoint:, element_name:)
response = Faraday.new(url(endpoint: endpoint), headers: headers).get

case response.status
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering how much similarity there will be in the PUT, POST and DELETE actions and if we could maybe abstract this out into a generic Response object, e.g.
response = Response.new(Faraday.new(url(endpoint: endpoint), headers: headers).get, element_name)
but we will see when we get there.

Might be too early to abstract/generalise.

:)

Copy link
Contributor Author

@bedrock-adam bedrock-adam Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think so @abreckner :).

Like we say it's okay to duplicate some of this when we work on those endpoints. We'll be in a good position to refactor out the noise in either this or another PR when we've got a few examples going.

@bedrock-adam bedrock-adam changed the title Connection.get(element_name:) [spike] move deserialization duplication in services to Connection.get and Connection.post Apr 29, 2020
else
raise Error.new(response.status)
end
end
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned in the description we can clean this up later at least it's in once place now.

@bedrock-adam bedrock-adam changed the title move deserialization duplication in services to Connection.get and Connection.post move duplication in services to Connection.get and Connection.post Apr 29, 2020
@bedrock-adam bedrock-adam marked this pull request as ready for review April 29, 2020 22:55
@bedrock-adam bedrock-adam requested a review from abreckner April 29, 2020 22:55
@bedrock-adam
Copy link
Contributor Author

Think we made a good call resisting the temptation to DRY this early on too @abreckner 😄.

else
raise Error.new(response.status)
end
.post(endpoint: "client.api/add", data: client.to_xml(root: "Client"))["Client"]
Copy link
Contributor

@abreckner abreckner Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is great (moving the error handling into the connection which is shared). Much needed! 👍

Just a small thing, I am just wondering if it's a little more readable to still have a response variable

e.g.

response = Connection
        .new(access_token: access_token, xero_tenant_id: xero_tenant_id)
        .post(endpoint: "client.api/add", data: client.to_xml(root: "Client"))
response["Client"]

I am just wary that if we have too much method chaining the code can get a little hard to follow. We may even want a connection variable too

connection = Connection.new(access_token: access_token, xero_tenant_id: xero_tenant_id)
response = connection.post(endpoint: "client.api/add", data: client.to_xml(root: "Client"))
response["Client"]

But let me check the Ruby guidelines. This might just be a personal thing for me. ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like your first example Tony. Seems like a good compromise between too much chaining and too much assignment. I'll update.

Copy link
Contributor Author

@bedrock-adam bedrock-adam Apr 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can remove the chaining on Connection later by making it a module instead of an object which I think will also simplify things too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I was doing some reading and it appears that method chaining is ok/good practice when you are working on the same object, but as these are returning different objects it can make debugging quite confusing.

e.g. if you were working with Arel where a query is always returned then Person.where().order().etc() where it is always working on the query object then it's good practice.

If each method returns a different object it can get confusing.

Elixir has a pipe operator (|>), but Elixir is functional which means you are only doing data transformations with each pipe which is slightly different.

We can have a chat about it in stand up though.
:)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeap good points I got carried away - have updated :P

Copy link
Contributor

@abreckner abreckner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall this is great, but I would like to have a quick chat about method chaining (non blocking).
:)

@bedrock-adam bedrock-adam merged commit 73ae8af into master Apr 29, 2020
@abreckner
Copy link
Contributor

Ah I see you updated this. Great stuff!

@abreckner
Copy link
Contributor

@lankz lankz deleted the connection/get/add_element_name branch June 10, 2025 05:20
lankz pushed a commit that referenced this pull request Jun 10, 2025
* add element_name

* update based on Tony's feedback
lankz pushed a commit that referenced this pull request Jun 10, 2025
* add element_name

* update based on Tony's feedback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants